⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classinspector.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*   Derby - Class org.apache.derby.iapi.services.loader.ClassInspector   Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package org.apache.derby.iapi.services.loader;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.reference.SQLState;import java.lang.reflect.*;import java.util.StringTokenizer;import java.util.List;import java.util.ArrayList;import java.util.NoSuchElementException;import java.util.Collections;/**	Methods to find out relationships between classes and methods within a class.	All class names within this interface are treated as java language class names,	e.g. int, COM.foo.Myclass, int[], java.lang.Object[]. That is java internal	class names as defined in the class file format are not understood.*/public final class ClassInspector{	private static final String[] primTypeNames =		{"boolean", "byte", "char", "short", "int", "long", "float", "double"};	// collect these as static, instead of each time allocates these new	// Strings for every method resolution	private static final String[] nonPrimTypeNames =		{"java.lang.Boolean", "java.lang.Byte", "java.lang.Character",		 "java.lang.Short", "java.lang.Integer", "java.lang.Long",		 "java.lang.Float", "java.lang.Double"};	private final ClassFactory cf;	/**		DO NOT USE! use the method in ClassFactory.	*/	public ClassInspector(ClassFactory cf) {		this.cf = cf;	}		/**	 * Is the given object an instance of the named class?	 *	 * @param className	The name of the class	 * @param obj		The object to test to see if it's an instance	 *			of the named class	 *	 * @return	true if obj is an instanceof className, false if not	 */	public boolean instanceOf(String className, Object obj)		throws ClassNotFoundException	{		Class clazz = getClass(className);		// is className an untyped null		if (clazz == null)			return false;		return clazz.isInstance(obj);	}	/**	 * Is one named class assignable to another named class or interface?	 *	 * @param fromClassName	The name of the class to be assigned	 * @param toClassName	The name of the class to be assigned to	 *	 * @return	true if an object of type fromClass can be assigned to an	 *			object of type toClass, false if not.	 */	public boolean assignableTo(String fromClassName, String toClassName)	{		try		{			Class toClass = getClass(toClassName);			// is toClass an untyped null			if (toClass == null) {				return false;			}			Class fromClass = getClass(fromClassName);			// is fromClass an untyped null			if (fromClass == null)				return !toClass.isPrimitive() || (toClass == Void.TYPE);			return toClass.isAssignableFrom(fromClass);		}		catch (ClassNotFoundException cnfe)		{			/* If either class can't be found, they can't be assigned */			return false;		}	}	/**	 * Does the named class exist, and is it accessible?	 *	 * @param className	The name of the class to test for existence	 *	 * @return	true if the class exists and is accessible, false if not	 */	public boolean accessible(String className)		throws ClassNotFoundException	{		Class theClass = getClass(className);		if (theClass == null)			return false;		/* Classes must be public to be accessible */		if (! Modifier.isPublic(theClass.getModifiers()))			return false;		return true;	}	/**	 * Get the Java name of the return type from a Member representing	 * a method or the type of a Member representing a field.	 *	 * @param method		A Member representing the method for	 *						which we want the return type.	 *	 * @return	A Java-language-style string describing the return type of	 *			the method (for example, it returns "int" instead of "I".	 */	public String getType(Member member)	{		Class type;		if (member instanceof Method)			type = ((Method) member).getReturnType();		else if (member instanceof Field)			type = ((Field) member).getType();		else if (member instanceof Constructor)			type = ((Constructor) member).getDeclaringClass();		else			type = Void.TYPE;		return ClassInspector.readableClassName(type);	}	/**	 * Find a public method that implements a given signature.	 * The signature is given using the full Java class names of the types.	 <BR>	 * A untyped null paramter is indicated by passing in an empty string ("")	 * as its class name.	 <BR>	 If receiverType respresents an interface then the methods of java.lang.Object	 arer included in the candidate list.	 <BR>	 If the caller is simply checking to see that a public method with the	 specified name exists, regardless of the signature, exists, then the	 caller should pass in a null for parmTypes.  (This is useful for checking	 the validity of a method alias when creating one.)	 <BR>	 We use a two-pass algorithm to resolve methods.  In the first pass, we	 use all "object" types to try to match a method.  If this fails, in the	 second pass, an array of "primitive" types (if the parameter has one,	 otherwise the same object type is used) is passed in, as well as the	 "object" type array.  For each parameter of a method, we try to match it	 against either the "object" type, or the "primitive" type.  Of all the	 qualified candidate methods found, we choose the closest one to the input	 parameter types.  This involves comparing methods whose parameters are	 mixed "object" and "primitive" types in the second pass.  This is	 eventually handled in classConvertableFromTo.	 *	 * @param receiverTypes	The class name of the receiver	 * @param methodName	The name of the method	 * @param parmTypes		An array of class names representing the	 *						parameter types.  Pass a zero-element array if	 *						there are no parameters.  Pass a null if it is	 *						okay to match any signature.	 * @param primParmTypes This is used in the second pass of the two-pass	 *						method resolution algorithm.  Use primitive type	 *						if it has one, otherwise use same object type	 * @param isParam		Array of booleans telling whether parameter is a ?.	 * @param staticMethod	Find a static method.	   @param repeatLastParameter If true the last parameter may be repeated any number of times (total count must be greater than one).	   If false the laste parameter is matched as usual. This also requires an exact match on the last parameter type.	 *	 * @return	A Member representing the matching method.  Returns null	 *			if no such method.	 *	 * @exception ClassNotFoundException	One or more of the classes does	 *										not exist.	 * @exception StandardException			Thrown on ambiguous method invocation.	 *	 * @see	Member	 * @see Modifier	 */	public Member findPublicMethod(String receiverType,								String methodName,								String[] parmTypes,								String[] primParmTypes,								boolean[] isParam,								boolean staticMethod,								boolean repeatLastParameter)					throws ClassNotFoundException, StandardException	{		Class receiverClass = getClass(receiverType);		if (receiverClass == null)			return null;		// primitives don't have methods		// note that arrays do since they are objects they have		// all the methods of java.lang.Object		if (receiverClass.isPrimitive()) {			return null;		}		// if parmTypes is null, then the caller is simply 		// looking to see if any public method with the		// specified name exists, regardless of its signature		if (parmTypes == null) {			Method[] methods = receiverClass.getMethods();						for (int index = 0; index < methods.length; index++) {				if (staticMethod) {					if (!Modifier.isStatic(methods[index].getModifiers())) {						continue;					}				}				if (methodName.equals(methods[index].getName())) {					// We found a match					return methods[index];				}			}			// No match			return null;		}		// convert the parameter types to classes		Class[] paramClasses = new Class[parmTypes.length];		Class[] primParamClasses = null;		if (primParmTypes != null)			primParamClasses = new Class[primParmTypes.length];		for (int i = 0; i < paramClasses.length; i++)		{			paramClasses[i] = getClass(parmTypes[i]);			if (primParmTypes == null)				continue;			if (primParmTypes[i].equals(parmTypes[i]))  // no separate primitive				primParamClasses[i] = null;			else				primParamClasses[i] = getClass(primParmTypes[i]);		}		// no overloading possible if there are no arguments, so perform		// an exact match lookup.		if (paramClasses.length == 0) {			try {				Method method = receiverClass.getMethod(methodName, paramClasses);				if (staticMethod) {					if (!Modifier.isStatic(method.getModifiers()))						return null;				}				return method;				} catch (NoSuchMethodException nsme2) {					// if we are an interface then the method could be defined on Object					if (!receiverClass.isInterface())						return null;				}		}		// now the tricky method resolution		Member[] methodList = receiverClass.getMethods();		// if we have an interface we need to add the methods of Object into the mix		if (receiverClass.isInterface()) {			Member[] objectMethods = java.lang.Object.class.getMethods();			if (methodList.length == 0) {				methodList = objectMethods;			} else {				Member[] set = new Member[methodList.length + objectMethods.length];				System.arraycopy(methodList, 0, set, 0, methodList.length);				System.arraycopy(objectMethods, 0, set, methodList.length, objectMethods.length);				methodList = set;			}		}		return resolveMethod(receiverClass, methodName, paramClasses,						primParamClasses, isParam, staticMethod, repeatLastParameter, methodList);	}	/**	 * Find a public field  for a class.	   This follows the sematics of the java compiler for locating a field.	   This means if a field fieldName exists in the class with package, private or	   protected then an error is raised. Even if the field hides a field fieldName	   in a super-class/super--interface. See the JVM spec on fields.	 *	 * @param receiverType	The class name of the receiver	 * @param fieldName		The name of the field	 * @param staticField	Find a static field	 *	 * @return	A Member representing the matching field.  	 * @exception StandardException	Class or field does not exist or is not public or a security exception.	 *	 * @see	Member	 * @see Modifier	 */	public Member findPublicField(String receiverType,								String fieldName,								boolean staticField)					throws StandardException	{		Exception e = null;		try {			Class receiverClass = getClass(receiverType);			if (receiverClass == null)				return null;			if (receiverClass.isArray() || receiverClass.isPrimitive()) {				// arrays don't have fields (the fake field 'length' is not returned here)				return null;			}  			int modifier = staticField ? (Modifier.PUBLIC | Modifier.STATIC) : Modifier.PUBLIC;			// Look for a public field first			Field publicField = receiverClass.getField(fieldName);			if ((publicField.getModifiers() & modifier) == modifier)			{				/*					If the class is an interface then we avoid looking for a declared field					that can hide a super-class's public field and not be accessable. This is because					a interface's fields are always public. This avoids a security check.				*/				if (receiverClass.isInterface() || (publicField.getDeclaringClass().equals(receiverClass)))					return publicField;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -