<h2>Changes</h2> | <h2>Changes</h2> | ||||
<p>-version 3.20 | |||||
<ul> | |||||
<li>JIRA JASSIST-241 | |||||
</ul> | |||||
</p> | |||||
<p>-version 3.19 on January 6, 2015 | <p>-version 3.19 on January 6, 2015 | ||||
<ul> | <ul> | ||||
<li>JIRA JASSIST-158, 205, 206, 207, 208, 209, 211, 212, 216, 220, 223, 224, | <li>JIRA JASSIST-158, 205, 206, 207, 208, 209, 211, 212, 216, 220, 223, 224, |
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Enumeration; | import java.util.Enumeration; | ||||
import javassist.bytecode.ClassFile; | |||||
import javassist.bytecode.Descriptor; | import javassist.bytecode.Descriptor; | ||||
/** | /** | ||||
return clazz; | return clazz; | ||||
} | } | ||||
/** | |||||
* Creates a new class (or interface) from the given class file. | |||||
* If there already exists a class with the same name, the new class | |||||
* overwrites that previous class. | |||||
* | |||||
* <p>This method is used for creating a <code>CtClass</code> object | |||||
* directly from a class file. The qualified class name is obtained | |||||
* from the class file; you do not have to explicitly give the name. | |||||
* | |||||
* @param classfile class file. | |||||
* @throws RuntimeException if there is a frozen class with the | |||||
* the same name. | |||||
* @since 3.20 | |||||
*/ | |||||
public CtClass makeClass(ClassFile classfile) | |||||
throws RuntimeException | |||||
{ | |||||
return makeClass(classfile, true); | |||||
} | |||||
/** | |||||
* Creates a new class (or interface) from the given class file. | |||||
* If there already exists a class with the same name, the new class | |||||
* overwrites that previous class. | |||||
* | |||||
* <p>This method is used for creating a <code>CtClass</code> object | |||||
* directly from a class file. The qualified class name is obtained | |||||
* from the class file; you do not have to explicitly give the name. | |||||
* | |||||
* @param classfile class file. | |||||
* @param ifNotFrozen throws a RuntimeException if this parameter is true | |||||
* and there is a frozen class with the same name. | |||||
* @since 3.20 | |||||
*/ | |||||
public CtClass makeClass(ClassFile classfile, boolean ifNotFrozen) | |||||
throws RuntimeException | |||||
{ | |||||
compress(); | |||||
CtClass clazz = new CtClassType(classfile, this); | |||||
clazz.checkModify(); | |||||
String classname = clazz.getName(); | |||||
if (ifNotFrozen) | |||||
checkNotFrozen(classname); | |||||
cacheCtClass(classname, clazz, true); | |||||
return clazz; | |||||
} | |||||
/** | /** | ||||
* Creates a new class (or interface) from the given class file. | * Creates a new class (or interface) from the given class file. | ||||
* If there already exists a class with the same name, this method | * If there already exists a class with the same name, this method |
/** | /** | ||||
* The version number of this release. | * The version number of this release. | ||||
*/ | */ | ||||
public static final String version = "3.19.0-GA"; | |||||
public static final String version = "3.20.0-GA"; | |||||
/** | /** | ||||
* Prints the version number and the copyright notice. | * Prints the version number and the copyright notice. |
qualifiedName = classfile.getName(); | qualifiedName = classfile.getName(); | ||||
} | } | ||||
CtClassType(ClassFile cf, ClassPool cp) { | |||||
this((String)null, cp); | |||||
classfile = cf; | |||||
qualifiedName = classfile.getName(); | |||||
} | |||||
protected void extendToString(StringBuffer buffer) { | protected void extendToString(StringBuffer buffer) { | ||||
if (wasChanged) | if (wasChanged) | ||||
buffer.append("changed "); | buffer.append("changed "); |
import java.util.List; | import java.util.List; | ||||
import java.util.ListIterator; | import java.util.ListIterator; | ||||
import java.util.Map; | import java.util.Map; | ||||
import javassist.CannotCompileException; | import javassist.CannotCompileException; | ||||
/** | /** | ||||
* <code>ClassFile</code> represents a Java <code>.class</code> file, which | * <code>ClassFile</code> represents a Java <code>.class</code> file, which | ||||
* consists of a constant pool, methods, fields, and attributes. | * consists of a constant pool, methods, fields, and attributes. | ||||
* | |||||
* | |||||
* <p>For example, | |||||
* <blockquote><pre> | |||||
* ClassFile cf = new ClassFile(false, "test.Foo", null); | |||||
* cf.setInterfaces(new String[] { "java.lang.Cloneable" }); | |||||
* | |||||
* FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I"); | |||||
* f.setAccessFlags(AccessFlag.PUBLIC); | |||||
* cf.addField(f); | |||||
* | |||||
* cf.write(new DataOutputStream(new FileOutputStream("Foo.class"))); | |||||
* </pre></blockquote> | |||||
* This code generates a class file <code>Foo.class</code> for the following class: | |||||
* <blockquote><pre> | |||||
* package test; | |||||
* class Foo implements Cloneable { | |||||
* public int width; | |||||
* } | |||||
* </pre></blockquote> | |||||
* </p> | |||||
* | |||||
* @see FieldInfo | |||||
* @see MethodInfo | |||||
* @see ClassFileWriter | |||||
* @see javassist.CtClass#getClassFile() | * @see javassist.CtClass#getClassFile() | ||||
* @see javassist.ClassPool#makeClass(ClassFile) | |||||
*/ | */ | ||||
public final class ClassFile { | public final class ClassFile { | ||||
int major, minor; // version number | int major, minor; // version number | ||||
* @param classname | * @param classname | ||||
* a fully-qualified class name | * a fully-qualified class name | ||||
* @param superclass | * @param superclass | ||||
* a fully-qualified super class name | |||||
* a fully-qualified super class name or null. | |||||
*/ | */ | ||||
public ClassFile(boolean isInterface, String classname, String superclass) { | public ClassFile(boolean isInterface, String classname, String superclass) { | ||||
major = MAJOR_VERSION; | major = MAJOR_VERSION; |
public static final int CONST_Utf8 = Utf8Info.tag; | public static final int CONST_Utf8 = Utf8Info.tag; | ||||
/** | /** | ||||
* <code>Cosnt_MethodHandle</code> | |||||
* <code>CONSTANT_MethodHandle</code> | |||||
*/ | */ | ||||
public static final int CONST_MethodHandle = MethodHandleInfo.tag; | public static final int CONST_MethodHandle = MethodHandleInfo.tag; | ||||
/** | |||||
* <code>CONSTANT_MethodHandle</code> | |||||
*/ | |||||
public static final int CONST_MethodType = MethodTypeInfo.tag; | |||||
/** | |||||
* <code>CONSTANT_MethodHandle</code> | |||||
*/ | |||||
public static final int CONST_InvokeDynamic = InvokeDynamicInfo.tag; | |||||
/** | /** | ||||
* Represents the class using this constant pool table. | * Represents the class using this constant pool table. | ||||
*/ | */ |
/** | /** | ||||
* <code>field_info</code> structure. | * <code>field_info</code> structure. | ||||
* | * | ||||
* <p>The following code adds a public field <code>width</code> | |||||
* of <code>int</code> type: | |||||
* <blockquote><pre> | |||||
* ClassFile cf = ... | |||||
* FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I"); | |||||
* f.setAccessFlags(AccessFlag.PUBLIC); | |||||
* cf.addField(f); | |||||
* </pre></blockquote> | |||||
* | |||||
* @see javassist.CtField#getFieldInfo() | * @see javassist.CtField#getFieldInfo() | ||||
*/ | */ | ||||
public final class FieldInfo { | public final class FieldInfo { |
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Map; | import java.util.Map; | ||||
import javassist.ClassPool; | import javassist.ClassPool; | ||||
import javassist.bytecode.stackmap.MapMaker; | import javassist.bytecode.stackmap.MapMaker; | ||||
* | * | ||||
* <p>The bytecode sequence of the method is represented | * <p>The bytecode sequence of the method is represented | ||||
* by a <code>CodeAttribute</code> object. | * by a <code>CodeAttribute</code> object. | ||||
* | |||||
* <p>The following code adds the default constructor to a class: | |||||
* of <code>int</code> type: | |||||
* <blockquote><pre> | |||||
* ClassFile cf = ... | |||||
* Bytecode code = new Bytecode(cf.getConstPool()); | |||||
* code.addAload(0); | |||||
* code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V"); | |||||
* code.addReturn(null); | |||||
* code.setMaxLocals(1); | |||||
* | |||||
* MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V"); | |||||
* minfo.setCodeAttribute(code.toCodeAttribute()); | |||||
* cf.addMethod(minfo); | |||||
* </pre></blockquote> | |||||
* | * | ||||
* @see #getCodeAttribute() | * @see #getCodeAttribute() | ||||
* @see CodeAttribute | * @see CodeAttribute | ||||
* @see Bytecode | |||||
* @see javassist.CtMethod#getMethodInfo() | * @see javassist.CtMethod#getMethodInfo() | ||||
* @see javassist.CtConstructor#getMethodInfo() | * @see javassist.CtConstructor#getMethodInfo() | ||||
*/ | */ |