001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.jxpath.util; 019 020import java.util.HashMap; 021import java.util.Map; 022import java.util.Objects; 023 024/** 025 * Port of class loading methods from {@code org.apache.commons.lang3.ClassUtils} from the Apache Commons Lang Component. Some adjustments made to remove 026 * dependency on {@code org.apache.commons.lang3.StringUtils}. Also modified to fall back on the current class loader when an attempt to load a class with the 027 * context class loader results in a {@code java.lang.ClassNotFoundException}. 028 * 029 * See org.apache.commons.lang3.ClassUtils 030 * @since 1.4.0 031 */ 032public final class ClassLoaderUtil { 033 034 /** 035 * Maps a primitive class name to its corresponding abbreviation used in array class names. 036 */ 037 private static Map<String, String> abbreviationMap = new HashMap<>(); 038 /** 039 * Feed abbreviation maps 040 */ 041 static { 042 addAbbreviation("int", "I"); 043 addAbbreviation("boolean", "Z"); 044 addAbbreviation("float", "F"); 045 addAbbreviation("long", "J"); 046 addAbbreviation("short", "S"); 047 addAbbreviation("byte", "B"); 048 addAbbreviation("double", "D"); 049 addAbbreviation("char", "C"); 050 } 051 052 /** 053 * Adds primitive type abbreviation to map of abbreviations. 054 * 055 * @param primitive Canonical name of primitive type 056 * @param abbreviation Corresponding abbreviation of primitive type 057 */ 058 private static void addAbbreviation(final String primitive, final String abbreviation) { 059 abbreviationMap.put(primitive, abbreviation); 060 } 061 062 /** 063 * Gets the class represented by {@code className} using the {@code classLoader}. This implementation supports names like "{@code java.lang.String[]}" as 064 * well as "{@code [Ljava.lang.String;}". 065 * 066 * @param <T> The expected class type. 067 * @param classLoader the class loader to use to load the class 068 * @param className the class name 069 * @param initialize whether the class must be initialized 070 * @return the class represented by {@code className} using the {@code classLoader} 071 * @throws ClassNotFoundException if the class is not found 072 */ 073 @SuppressWarnings("unchecked") // assume the call site knows what it's doing. 074 private static <T> Class<T> getClass(final ClassLoader classLoader, final String className, final boolean initialize) throws ClassNotFoundException { 075 Class<T> clazz; 076 if (abbreviationMap.containsKey(className)) { 077 final String clsName = "[" + abbreviationMap.get(className); 078 clazz = (Class<T>) Class.forName(clsName, initialize, classLoader).getComponentType(); 079 } else { 080 clazz = (Class<T>) Class.forName(toCanonicalName(className), initialize, classLoader); 081 } 082 return clazz; 083 } 084 085 /** 086 * Gets the class represented by {@code className} using the current thread's context class loader. This implementation supports names like 087 * "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}". 088 * 089 * @param <T> The expected class type. 090 * @param className the class name 091 * @param initialize whether the class must be initialized 092 * @return the class represented by {@code className} using the current thread's context class loader 093 * @throws ClassNotFoundException if the class is not found 094 */ 095 public static <T> Class<T> getClass(final String className, final boolean initialize) throws ClassNotFoundException { 096 final ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); 097 final ClassLoader currentCL = ClassLoaderUtil.class.getClassLoader(); 098 if (contextCL != null) { 099 try { 100 return getClass(contextCL, className, initialize); 101 } catch (final ClassNotFoundException ignore) { // NOPMD 102 // ignore this exception and try the current class loader 103 } 104 } 105 return getClass(currentCL, className, initialize); 106 } 107 108 /** 109 * Converts a class name to a JLS style class name. 110 * 111 * @param className the class name 112 * @return the converted name 113 */ 114 private static String toCanonicalName(String className) { 115 Objects.requireNonNull(className, "className"); 116 if (className.endsWith("[]")) { 117 final StringBuilder classNameBuffer = new StringBuilder(); 118 while (className.endsWith("[]")) { 119 className = className.substring(0, className.length() - 2); 120 classNameBuffer.append("["); 121 } 122 final String abbreviation = abbreviationMap.get(className); 123 if (abbreviation != null) { 124 classNameBuffer.append(abbreviation); 125 } else { 126 classNameBuffer.append("L").append(className).append(";"); 127 } 128 className = classNameBuffer.toString(); 129 } 130 return className; 131 } 132 133 /** 134 * New need to constructs new instances. 135 */ 136 private ClassLoaderUtil() { 137 // empty 138 } 139}