class.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,182 行 · 第 1/5 页
JAVA
2,182 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xml.utils.synthetic;import org.apache.xml.utils.synthetic.SynthesisException;import org.apache.xml.utils.synthetic.reflection.Constructor;import org.apache.xml.utils.synthetic.reflection.Method;import org.apache.xml.utils.synthetic.reflection.Field;import java.lang.reflect.Modifier;/* WORK NEEDED: Factories/Libraries: We currently have forClass and forName(request reified, complain if no real class), and declareClass (request unreified, create unreified if it doesn't exist). What about ther user expectations -- should we have a full matrix, rather than asking users to write wrappers? Reflection doesn't tell us about deprecation. If we want that info, MFC advises mousing our way into the bytecodes. (Ugh). Should we at least model that for synthetics?*//** * <meta name="usage" content="internal"/> * org.apache.xml.utils.synthetic.Class is a mutable equivalent of java.lang.Class. * Instances represent classes and interfaces in a running Java * application, or class descriptions under construction. In the * former case, org.apache.xml.utils.synthetic.Class operates as a proxy for the * "real" java.lang.Class object; in the latter, it consults * data structures defined in the org.apache.xml.utils.synthetic.reflection.* package. * <p> * Unlike java.lang.Class, org.apache.xml.utils.synthetic.Class has a pair of factories * (fromName and fromClass). It can also be switched from synthetic * to proxy operation after construction, by setting the realClass * property; this is intended to allow these definitions to be * "compiled in place". * <p> * For convenient use, org.apache.xml.utils.synthetic.Class implements an extended * version of the java.lang.Class API -- but is not a subclass * thereof, since java.lang.Class is Final (presumably for * security reasons). * <p> * DEVELOPMENT NOTE: Methods not yet implemented will throw * IllegalStateException * <p> * I've added code to convert primitive names into their TYPEs, * to accept foo[] as a synonym for [Lfoo, and to generate * the right thing on output (getJava[Short]Name). * Useful extension for code generation from Java-like * source. We may want to factor these and toSource out, making * org.apache.xml.utils.synthetic.Class addess only the JVM level and providing * subclasses or access tools that handle language syntax * (Java source, NetRexx source, etc.) * * @since 2000/2/10 */public class Class extends Object implements java.io.Serializable{ /** Class descriptions currently existing. */ private static java.util.Hashtable global_classtable = new java.util.Hashtable(); /** fully-qualified path.classname. * @serial */ private java.lang.String name; /** * Actual Java class object. When present, all interactions * are redirected to it. Allows our Class to function as a * wrapper for the Java version (in lieu of subclassing or * a shared Interface), and allows "in-place compilation" * to replace a generated description with an * directly runnable class. * @serial */ private java.lang.Class realclass = null; /** Field modifiers: Java language modifiers for this class * or interface, encoded in an integer. * @serial */ private int modifiers; /** Field isInterface: True if the Class object represents * an interface type. * @serial */ private boolean isInterface = false; /** Field superclass: If this object represents the class * Object, this is null. Otherwise, the Class object that * represents the superclass of that class. In proxy mode this * is determined when needed. In synthesis mode it's explicitly * set by the user, and if null the superclass will be assumed * to be Object. * @serial */ private Class superclass = null; /** Field declaringclass: If this object represents an inner class, * the Class object that represents the class that declared it. * Otherwise null. * @serial * */ private Class declaringclass = null; /** Field interfaces: A list of all interfaces implemented by the class * or interface represented by this object. * @serial * */ private Class[] interfaces = new Class[0]; /** Field allclasses: an array containing Class objects representing all * the public classes and interfaces that are members of the class * represented by this Class object. * @serial */ private Class[] allclasses = new Class[0]; /** Field declaredclasses: an array of Class objects reflecting all the * classes and interfaces declared as members of the class represented * by this Class object. Excludes inherited classes and interfaces. * @serial */ private Class[] declaredclasses = new Class[0]; /** Field allconstructors: an array containing Constructor objects * reflecting all the constructors of the class represented * by this Class object. An array of length 0 is returned if the * class has no public constructors. In proxy mode only public * constructors will be displayed; in synthesis mode, all declared * constructors will be displayed. * @serial * */ private Constructor[] allconstructors = new Constructor[0]; /** Field declaredconstructors: an array of Constructor objects * reflecting all the constructors declared by the class * represented by this Class object. Includes non-public * constructors, but excludes inherited ones. * @serial * */ private Constructor[] declaredconstructors = new Constructor[0]; /** Field allmethods. * @serial */ private Method[] allmethods = new Method[0]; /** Field declaredmethods. * @serial */ private Method[] declaredmethods = new Method[0]; /** Field allfields. * @serial */ private Field[] allfields = new Field[0]; /** Field declaredfields. * @serial */ private Field[] declaredfields = new Field[0]; /** Field innerclasses. * @serial */ private Class[] innerclasses = new Class[0]; /** * Construct a synthetic class as proxy/wrapper for an existing * Java Class. Non-public; most folks should use * .forName and .forClass to request these wrappers, so they * get the shared instances. * <p> * Creation date: (12-25-99 12:16:15 PM) * @param realclass java.lang.Class */ Class(java.lang.Class realclass) { this(realclass.getName()); try { setRealClass(realclass); } catch (SynthesisException e) { e.printStackTrace(); } } /** * Construct a named-but-empty synthetic Class object. * Non-public; most folks should use * .forName and .forClass to request these wrappers, so they * get the shared instances. * <p> * Creation date: (12-25-99 12:15:23 PM) * * @param fullname full name of the class that is synthetized. */ Class(String fullname) { this.name = fullname; global_classtable.put(fullname, this); } /** * Returns the synthetic Class object associated with the "real" * class specified, creating one if it didn't already exist. * <p> * For example, the following code fragment returns * the runtime Class descriptor for the class named * mypackage.MyClass. * <code> * Class t = * Class.forName(java.lang.Class.forName("mypackage.MyClass")) * </code> * <p> * Note that if the user has manually created a org.apache.xml.utils.synthetic.Class * with the same name before this call is issued, that object * will be found instead. See also the declareClass call. * <p> * We need a better way to declare/define array classes, * given a class object (synthetic or not). * * @param cls the desired Java class. * @return the synthetic Class descriptor for the specified class. */ public static Class forClass(java.lang.Class cls) { if (cls == null) return null; Class ret = (Class) (global_classtable.get(cls.getName())); if (null == ret) ret = new Class(cls); return ret; } /** * Like forName, but if the classname doesn't have a package * prefix we first attempt to look it up as one of our own * inner clases. As with forName, if this can not be resolved * we throw an exception. * * @param classname the full or partial class name. * * @return The Class name that matches the argument. * * @throws ClassNotFoundException */ public Class forNameInContext(String classname) throws ClassNotFoundException { for (int i = innerclasses.length - 1; i >= 0; --i) { if (classname.equals(innerclasses[i].getShortName())) return innerclasses[i]; } return forName(classname); } /** * Returns the synthetic Class object associated with the class * with the given fully-qualified name. If there isn't one, this * method attempts to locate, load and link the standard java Class. * If it succeeds, it returns a wrapped version of the Class object * representing the class. If it fails, the method throws a * ClassNotFoundException. * <p> * For example, the following code fragment returns * the runtime Class descriptor for the class named * mypackage.MyClass -- either as a synthetic or as * a standard Java class. * <code> * Class t = * Class.forName("mypackage.MyClass") * </code> * <p> * I've added support for arrays -- assuming any name * that ends with ']' is an array. It probably needs to be * made smarter, possibly via a subclass of org.apache.xml.utils.synthetic.Class. * * @param className the fully qualified name of the desired class. * @return the synthetic Class descriptor for the class with the specified name. * @throws ClassNotFoundException if the class could not be found. */ public static Class forName(String className) throws ClassNotFoundException { // ***** Experimental support for array syntax expressed // per Java source rather than per JVM type formalism. // Simpleminded, asssumes balanced []'s. if (className.endsWith("]")) { StringBuffer arrayname = new StringBuffer(); for (int i = className.indexOf('['); i != -1; i = className.indexOf('[', i + 1)) { arrayname.append('['); } // Convert the classname to array-formalism // Primitives have letters; objects are Lname; // (Don't ask why long is spelled with a J and // object is spelled with an L...) String classname = className.substring(0, className.indexOf('[')); if ("byte".equals(classname)) arrayname.append('B'); else if ("char".equals(classname)) arrayname.append('C'); else if ("double".equals(classname)) arrayname.append('D'); else if ("float".equals(classname)) arrayname.append('F'); else if ("int".equals(classname)) arrayname.append('I'); else if ("long".equals(classname)) arrayname.append('J'); else if ("short".equals(classname)) arrayname.append('S'); else if ("boolean".equals(classname)) arrayname.append('Z'); else arrayname.append('L').append(classname).append(';'); // Tail-call. return forName(arrayname.toString()); } Class ret = (Class) (global_classtable.get(className)); if (null == ret) { // ***** Experimental support for Java primitives // Seems to me that mapping them into the "Type" is // probably most useful if ("boolean".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Boolean.TYPE; } else if ("byte".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Byte.TYPE; } else if ("char".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Character.TYPE; } else if ("short".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Short.TYPE; } else if ("int".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Integer.TYPE; } else if ("long".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Long.TYPE; } else if ("float".equals(className)) { ret = new Class(className); ret.realclass = java.lang.Float.TYPE; } else if ("double".equals(className)) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?