class.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,182 行 · 第 1/5 页
JAVA
2,182 行
* superclass or superinterface of, the class or * interface represented by the specified Class * parameter. It returns true if so, false otherwise. If * this Class object represents a primitive type, * returns true if the specified Class parameter is * exactly this Class object, false otherwise. * <p> * Specifically, this method tests whether the type * represented by the specified Class parameter can * be converted to the type represented by this Class * object via an identity conversion or via a widening * reference conversion. See The Java Language * Specification, sections 5.1.1 and 5.1.4 , for details. * * * @param cls * * @throws NullPointerException if the specified Class parameter is null. */ public boolean isAssignableFrom(Class cls) { if (realclass != null && cls.realclass != null) return realclass.isAssignableFrom(cls.realclass); throw new java.lang.IllegalStateException(); } /** * Determines if the class or interface represented by * this Class object is either the same as, or is a * superclass or superinterface of, the class or * interface represented by the specified Class * parameter. It returns true if so, false otherwise. If * this Class object represents a primitive type, * returns true if the specified Class parameter is * exactly this Class object, false otherwise. * <p> * Specifically, this method tests whether the type * represented by the specified Class parameter can * be converted to the type represented by this Class * object via an identity conversion or via a widening * reference conversion. See The Java Language * Specification, sections 5.1.1 and 5.1.4 , for details. * * * @param cls * * @throws NullPointerException if the specified Class parameter is null. */ public boolean isAssignableFrom(java.lang.Class cls) { if (realclass != null) return realclass.isAssignableFrom((java.lang.Class) cls); throw new java.lang.IllegalStateException(); } /** * This method is the dynamic equivalent of the Java * language instanceof operator. The method * returns true if the specified Object argument is * non-null and can be cast to the reference type * represented by this Class object without raising a * ClassCastException. It returns false otherwise. * <p> * Specifically, if this Class object represents a * declared class, returns true if the specified Object * argument is an instance of the represented class (or * of any of its subclasses); false otherwise. If this * Class object represents an array class, returns true * if the specified Object argument can be converted * to an object of the array type by an identity * conversion or by a widening reference conversion; * false otherwise. If this Class object represents an * interface, returns true if the class or any superclass * of the specified Object argument implements this * interface; false otherwise. If this Class object * represents a primitive type, returns false. * * @param obj The object to check * */ public boolean isInstance(Object obj) { if (realclass != null) return realclass.isInstance(obj); // Scan inheritances? (reliable). // Check name? (not reliable). throw new java.lang.IllegalStateException(); } /** * Determines if the specified Class object represents * an interface type. * * true if this object represents an interface; * false otherwise. */ public boolean isInterface() { return (realclass != null) ? realclass.isInterface() : isInterface; } /** * Assert that the specified Class object represents * an interface type. Can't be changed after real class loaded. * * @param * true if this object represents an interface; * false otherwise. * * @param isInterface * * @throws SynthesisException */ public void isInterface(boolean isInterface) throws SynthesisException { if (realclass == null) this.isInterface = isInterface; else if (realclass.isInterface() != isInterface) throw new SynthesisException(SynthesisException.REIFIED); } /** * Determines if the specified Class object represents * a primitive Java type. * <p> * There are nine predefined Class objects to * represent the eight primitive Java types and void. * These are created by the Java Virtual Machine, and * have the same names as the primitive types that * they represent, namely boolean, byte, char, short, * int, long, float, and double, and void. * <p> * These objects may only be accessed via the * following public static final variables, and are the * only Class objects for which this method returns * true. * */ public boolean isPrimitive() { return realclass != null && realclass.isPrimitive(); } /** * Creates a new instance of a class. * * a newly allocated instance of the class * represented by this object. This is done * exactly as if by a new expression with an * empty argument list. * @throws IllegalAccessException * if the class or initializer is not accessible. * @throws InstantiationException * if an application tries to instantiate an * abstract class or an interface, or if the * instantiation fails for some other reason. */ public Object newInstance() throws InstantiationException, IllegalAccessException { throw new java.lang.IllegalStateException(); } /** * Converts the object to a string. The string * representation is the string "class" or * "interface" followed by a space and then the * fully qualified name of the class. If this Class * object represents a primitive type, returns the * name of the primitive type. * <p> * Should this say "synthetic" as well as "class" or * "interface"? Or should that be gated on whether we're proxy * to a realclass? * * @return a string representation of this class object. */ public String toString() { if (realclass != null) return realclass.toString(); else if (isInterface()) return "interface " + name; else return "class " + name; } /** * Convenience for writing to, eg, System.out * * @param out * @param depth */ public void toSource(java.io.OutputStream out, int depth) { java.io.PrintWriter writer = new java.io.PrintWriter(out); toSource(writer, depth); } /** * Converts the object to a Java code stream. The string * representation is as full a Java definition of the class * as we are able to achieve. If this Class * object represents a primitive type, returns the * name of the primitive type. * * @param out * @param depth */ public void toSource(java.io.PrintWriter out, int depth) { String tab = tabset(depth); if (realclass != null) out.println( tab + "/** Code back-generated from a \"real\" Class; accuracy limited by reflection APIs. */"); else out.println( tab + "/** Code generated via org.apache.xml.utils.synthetic.Class */"); /* Package should not be printed for inner classes */ if (getDeclaringClass() == null) out.println(tab + "package " + getPackageName() + ";"); out.print(tab + Modifier.toString(getModifiers())); if (isInterface()) out.print(" interface "); else out.print(" class "); out.println(getJavaShortName()); if (superclass != null) { out.print('\n' + tab + " extends " + superclass.getJavaName()); } Class[] ext = getInterfaces(); if (ext != null & ext.length > 0) { // Interfaces extend other interfaces, // Classes implement interfaces. out.print('\n' + tab + (isInterface ? " extends " : " implements ") + ext[0].getName()); for (int i = 1; i < ext.length; ++i) { out.print(", " + ext[i].getJavaName()); } out.print("\n"); } out.print(tab + "{\n"); tab = tabset(++depth); // Fields-------------------------------- Field[] fields = null; try { fields = getDeclaredFields(); } catch (SecurityException e) { out.println(tab + "//SecurityException retrieving fields"); } if (fields != null) { for (int i = 0; i < fields.length; ++i) { out.println(tab + fields[i].toSource()); } } // Constructors-------------------------- Constructor[] ctors = null; try { ctors = getDeclaredConstructors(); } catch (SecurityException e) { out.println(tab + "//SecurityException retrieving ctors"); } if (ctors != null) { for (int i = 0; i < ctors.length; ++i) { out.print(ctors[i].toSource(tab)); } } // Methods------------------------------- Method[] methods = null; try { methods = getDeclaredMethods(); } catch (SecurityException e) { out.println(tab + "//SecurityException retrieving methods"); } if (methods != null) { for (int i = 0; i < methods.length; ++i) { out.print('\n'); out.print(methods[i].toSource(tab)); } } // Inner classes -------------------------------- Class[] inners = getInnerClasses(); if (inners != null) { for (int i = 0; i < inners.length; ++i) { out.print('\n'); inners[i].toSource(out, depth); } } // Done------------------------------ tab = tabset(--depth); out.print(tab + "}\n"); out.flush(); } /** * Method tabset * * * @param depth * * (tabset) @return */ private String tabset(int depth) { StringBuffer t = new StringBuffer(); while (depth-- > 0) { t.append(" "); } return t.toString(); } // Ignores any keywords we don't recognize /** Field val */ static final int[] val = { Modifier.ABSTRACT, Modifier.FINAL, Modifier.INTERFACE, Modifier.NATIVE, Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC, Modifier.STATIC, Modifier.SYNCHRONIZED, Modifier.TRANSIENT, Modifier.VOLATILE }; /** Field kwd */ static final String[] kwd = { "abstract", "final", "interface", "native", "private", "protected", "public", "static", "synchronized", "transient", "volatile" }; /** * Method modifierFromString * * * @param t * * (modifierFromString) @return */ static public int modifierFromString(String t) { for (int i = 0; i < kwd.length; ++i) { if (kwd[i].equals(t)) return val[i]; } return 0; } /** * Method modifiersFromString * * * @param s * * (modifiersFromString) @return */ static public int modifiersFromString(String s) { int mods = 0; java.util.StringTokenizer parts = new java.util.StringTokenizer(s); while (parts.hasMoreTokens()) { String t = parts.nextToken(); mods |= modifierFromString(t); } return mods; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?