📄 ctclass.java
字号:
/* * Javassist, a Java-bytecode translator toolkit. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved. * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. Alternatively, the contents of this file may be used under * the terms of the GNU Lesser General Public License Version 2.1 or later. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. */package javassist;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.URL;import java.security.ProtectionDomain;import java.util.Collection;import javassist.bytecode.ClassFile;import javassist.bytecode.Descriptor;import javassist.bytecode.Opcode;import javassist.expr.ExprEditor;/* Note: * * This class is an abstract class and several methods just return null * or throw an exception. Those methods are overridden in subclasses * of this class. Read the source code of CtClassType if you are * interested in the implementation of Javassist. * * Subclasses of CtClass are CtClassType, CtPrimitiveType, and CtArray. *//** * An instance of <code>CtClass</code> represents a class. * It is obtained from <code>ClassPool</code>. * * @see ClassPool#get(String) */public abstract class CtClass { protected String qualifiedName; /** * The version number of this release. */ public static final String version = "3.4"; /** * Prints the version number and the copyright notice. * * <p>The following command invokes this method: * * <ul><pre>java -jar javassist.jar</pre></ul> */ public static void main(String[] args) { System.out.println("Javassist version " + CtClass.version); System.out.println("Copyright (C) 1999-2006 Shigeru Chiba." + " All Rights Reserved."); } static final String javaLangObject = "java.lang.Object"; /** * The <code>CtClass</code> object representing * the <code>boolean</code> type. */ public static CtClass booleanType; /** * The <code>CtClass</code> object representing * the <code>char</code> type. */ public static CtClass charType; /** * The <code>CtClass</code> object representing * the <code>byte</code> type. */ public static CtClass byteType; /** * The <code>CtClass</code> object representing * the <code>short</code> type. */ public static CtClass shortType; /** * The <code>CtClass</code> object representing * the <code>int</code> type. */ public static CtClass intType; /** * The <code>CtClass</code> object representing * the <code>long</code> type. */ public static CtClass longType; /** * The <code>CtClass</code> object representing * the <code>float</code> type. */ public static CtClass floatType; /** * The <code>CtClass</code> object representing * the <code>double</code> type. */ public static CtClass doubleType; /** * The <code>CtClass</code> object representing * the <code>void</code> type. */ public static CtClass voidType; static CtClass[] primitiveTypes; static { primitiveTypes = new CtClass[9]; booleanType = new CtPrimitiveType("boolean", 'Z', "java.lang.Boolean", "booleanValue", "()Z", Opcode.IRETURN, Opcode.T_BOOLEAN, 1); primitiveTypes[0] = booleanType; charType = new CtPrimitiveType("char", 'C', "java.lang.Character", "charValue", "()C", Opcode.IRETURN, Opcode.T_CHAR, 1); primitiveTypes[1] = charType; byteType = new CtPrimitiveType("byte", 'B', "java.lang.Byte", "byteValue", "()B", Opcode.IRETURN, Opcode.T_BYTE, 1); primitiveTypes[2] = byteType; shortType = new CtPrimitiveType("short", 'S', "java.lang.Short", "shortValue", "()S", Opcode.IRETURN, Opcode.T_SHORT, 1); primitiveTypes[3] = shortType; intType = new CtPrimitiveType("int", 'I', "java.lang.Integer", "intValue", "()I", Opcode.IRETURN, Opcode.T_INT, 1); primitiveTypes[4] = intType; longType = new CtPrimitiveType("long", 'J', "java.lang.Long", "longValue", "()J", Opcode.LRETURN, Opcode.T_LONG, 2); primitiveTypes[5] = longType; floatType = new CtPrimitiveType("float", 'F', "java.lang.Float", "floatValue", "()F", Opcode.FRETURN, Opcode.T_FLOAT, 1); primitiveTypes[6] = floatType; doubleType = new CtPrimitiveType("double", 'D', "java.lang.Double", "doubleValue", "()D", Opcode.DRETURN, Opcode.T_DOUBLE, 2); primitiveTypes[7] = doubleType; voidType = new CtPrimitiveType("void", 'V', "java.lang.Void", null, null, Opcode.RETURN, 0, 0); primitiveTypes[8] = voidType; } protected CtClass(String name) { qualifiedName = name; } /** * Converts the object to a string. */ public String toString() { StringBuffer buf = new StringBuffer(getClass().getName()); buf.append("@"); buf.append(Integer.toHexString(hashCode())); buf.append("["); extendToString(buf); buf.append("]"); return buf.toString(); } /** * Implemented in subclasses to add to the {@link #toString()} result. * Subclasses should put a space before each token added to the buffer. */ protected void extendToString(StringBuffer buffer) { buffer.append(getName()); } /** * Returns a <code>ClassPool</code> for this class. */ public ClassPool getClassPool() { return null; } /** * Returns a class file for this class. * * <p>This method is not available if <code>isFrozen()</code> * is true. */ public ClassFile getClassFile() { checkModify(); return getClassFile2(); } /** * Returns a class file for this class (read only). * Normal applications do not need calling this method. Use * <code>getClassFile()</code>. * * <p>The <code>ClassFile</code> object obtained by this method * is read only. Changes to this object might not be reflected * on a class file generated by <code>toBytecode()</code>, * <code>toClass()</code>, etc. * * <p>This method is available even if <code>isFrozen()</code> * is true. However, if the class is frozen, it might be also * pruned. * * @see CtClass#getClassFile() * @see CtClass#isFrozen() * @see CtClass#prune() */ public ClassFile getClassFile2() { return null; } /** * Undocumented method. Do not use; internal-use only. */ public javassist.compiler.AccessorMaker getAccessorMaker() { return null; } /** * Returns the uniform resource locator (URL) of the class file. */ public URL getURL() throws NotFoundException { throw new NotFoundException(getName()); } /** * Returns true if the definition of the class has been modified. */ public boolean isModified() { return false; } /** * Returns true if the class has been loaded or written out * and thus it cannot be modified any more. * * @see #defrost() * @see #detach() */ public boolean isFrozen() { return true; } void freeze() {} /* Note: this method is overridden by CtClassType */ void checkModify() throws RuntimeException { if (isFrozen()) throw new RuntimeException(getName() + " class is frozen"); // isModified() must return true after this method is invoked. } /** * Defrosts the class so that the class can be modified again. * * <p>To avoid changes that will be never reflected, * the class is frozen to be unmodifiable if it is loaded or * written out. This method should be called only in a case * that the class will be reloaded or written out later again. * * <p>If <code>defrost()</code> will be called later, pruning * must be disallowed in advance. * * @see #isFrozen() * @see #stopPruning(boolean) * @see #detach() */ public void defrost() { throw new RuntimeException("cannot defrost " + getName()); } /** * Returns <code>true</code> if this object represents a primitive * Java type: boolean, byte, char, short, int, long, float, double, * or void. */ public boolean isPrimitive() { return false; } /** * Returns <code>true</code> if this object represents an array type. */ public boolean isArray() { return false; } /** * If this object represents an array, this method returns the component * type of the array. Otherwise, it returns <code>null</code>. */ public CtClass getComponentType() throws NotFoundException { return null; } /** * Returns <code>true</code> if this class extends or implements * <code>clazz</code>. It also returns <code>true</code> if * this class is the same as <code>clazz</code>. */ public boolean subtypeOf(CtClass clazz) throws NotFoundException { return this == clazz || getName().equals(clazz.getName()); } /** * Obtains the fully-qualified name of the class. */ public String getName() { return qualifiedName; } /** * Obtains the not-qualified class name. */ public final String getSimpleName() { String qname = qualifiedName; int index = qname.lastIndexOf('.'); if (index < 0) return qname; else return qname.substring(index + 1); } /** * Obtains the package name. It may be <code>null</code>. */ public final String getPackageName() { String qname = qualifiedName; int index = qname.lastIndexOf('.'); if (index < 0) return null; else return qname.substring(0, index); } /** * Sets the class name * * @param name fully-qualified name */ public void setName(String name) { checkModify(); if (name != null) qualifiedName = name; } /** * Substitutes <code>newName</code> for all occurrences of a class * name <code>oldName</code> in the class file. * * @param oldName replaced class name * @param newName substituted class name */ public void replaceClassName(String oldName, String newName) { checkModify(); } /** * Changes class names appearing in the class file according to the * given <code>map</code>. * * <p>All the class names appearing in the class file are tested * with <code>map</code> to determine whether each class name is * replaced or not. Thus this method can be used for collecting * all the class names in the class file. To do that, first define * a subclass of <code>ClassMap</code> so that <code>get()</code> * records all the given parameters. Then, make an instance of * that subclass as an empty hash-table. Finally, pass that instance * to this method. After this method finishes, that instance would * contain all the class names appearing in the class file. * * @param map the hashtable associating replaced class names * with substituted names. */ public void replaceClassName(ClassMap map) { checkModify(); } /** * Returns a collection of the names of all the classes * referenced in this class. * That collection includes the name of this class. * * <p>This method may return <code>null</code>. */ public Collection getRefClasses() { ClassFile cf = getClassFile2(); if (cf != null) { ClassMap cm = new ClassMap() { public void put(String oldname, String newname) { put0(oldname, newname); } public Object get(Object jvmClassName) { String n = toJavaName((String)jvmClassName); put0(n, n); return null; } public void fix(String name) {} }; cf.renameClass(cm); return cm.values(); } else return null; } /** * Determines whether this object represents a class or an interface. * It returns <code>true</code> if this object represents an interface. */ public boolean isInterface() { return false; } /** * Determines whether this object represents an annotation type. * It returns <code>true</code> if this object represents an annotation type. * * @since 3.2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -