diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/javassist/ClassPool.java | 50 | ||||
-rw-r--r-- | src/main/javassist/CtClass.java | 2 | ||||
-rw-r--r-- | src/main/javassist/CtClassType.java | 6 | ||||
-rw-r--r-- | src/main/javassist/bytecode/ClassFile.java | 29 | ||||
-rw-r--r-- | src/main/javassist/bytecode/ConstPool.java | 12 | ||||
-rw-r--r-- | src/main/javassist/bytecode/FieldInfo.java | 9 | ||||
-rw-r--r-- | src/main/javassist/bytecode/MethodInfo.java | 17 |
7 files changed, 121 insertions, 4 deletions
diff --git a/src/main/javassist/ClassPool.java b/src/main/javassist/ClassPool.java index a1e46e2e..c90c2c74 100644 --- a/src/main/javassist/ClassPool.java +++ b/src/main/javassist/ClassPool.java @@ -31,6 +31,8 @@ import java.util.Hashtable; import java.util.Iterator; import java.util.ArrayList; import java.util.Enumeration; + +import javassist.bytecode.ClassFile; import javassist.bytecode.Descriptor; /** @@ -736,6 +738,54 @@ public class ClassPool { /** * 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. * If there already exists a class with the same name, this method * returns the existing class; a new class is never created from * the given class file. diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index 251cd10f..fc828847 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -69,7 +69,7 @@ public abstract class CtClass { /** * 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. diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 2f875f65..e86780d3 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -97,6 +97,12 @@ class CtClassType extends CtClass { qualifiedName = classfile.getName(); } + CtClassType(ClassFile cf, ClassPool cp) { + this((String)null, cp); + classfile = cf; + qualifiedName = classfile.getName(); + } + protected void extendToString(StringBuffer buffer) { if (wasChanged) buffer.append("changed "); diff --git a/src/main/javassist/bytecode/ClassFile.java b/src/main/javassist/bytecode/ClassFile.java index cc7cae59..f27ff4d8 100644 --- a/src/main/javassist/bytecode/ClassFile.java +++ b/src/main/javassist/bytecode/ClassFile.java @@ -23,13 +23,38 @@ import java.util.ArrayList; import java.util.List; import java.util.ListIterator; import java.util.Map; + import javassist.CannotCompileException; /** * <code>ClassFile</code> represents a Java <code>.class</code> file, which * 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.ClassPool#makeClass(ClassFile) */ public final class ClassFile { int major, minor; // version number @@ -132,7 +157,7 @@ public final class ClassFile { * @param classname * a fully-qualified class name * @param superclass - * a fully-qualified super class name + * a fully-qualified super class name or null. */ public ClassFile(boolean isInterface, String classname, String superclass) { major = MAJOR_VERSION; diff --git a/src/main/javassist/bytecode/ConstPool.java b/src/main/javassist/bytecode/ConstPool.java index f05dfd4f..78935160 100644 --- a/src/main/javassist/bytecode/ConstPool.java +++ b/src/main/javassist/bytecode/ConstPool.java @@ -94,11 +94,21 @@ public final class ConstPool { public static final int CONST_Utf8 = Utf8Info.tag; /** - * <code>Cosnt_MethodHandle</code> + * <code>CONSTANT_MethodHandle</code> */ 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. */ public static final CtClass THIS = null; diff --git a/src/main/javassist/bytecode/FieldInfo.java b/src/main/javassist/bytecode/FieldInfo.java index 485df3dd..cbba0a85 100644 --- a/src/main/javassist/bytecode/FieldInfo.java +++ b/src/main/javassist/bytecode/FieldInfo.java @@ -25,6 +25,15 @@ import java.util.ArrayList; /** * <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() */ public final class FieldInfo { diff --git a/src/main/javassist/bytecode/MethodInfo.java b/src/main/javassist/bytecode/MethodInfo.java index df436dda..4b8c1c4d 100644 --- a/src/main/javassist/bytecode/MethodInfo.java +++ b/src/main/javassist/bytecode/MethodInfo.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; + import javassist.ClassPool; import javassist.bytecode.stackmap.MapMaker; @@ -30,9 +31,25 @@ import javassist.bytecode.stackmap.MapMaker; * * <p>The bytecode sequence of the method is represented * 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 CodeAttribute + * @see Bytecode * @see javassist.CtMethod#getMethodInfo() * @see javassist.CtConstructor#getMethodInfo() */ |