Package com.tonic.analysis
Class ClassFactory
- java.lang.Object
-
- com.tonic.analysis.ClassFactory
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static MethodEntryaddDefaultClassInitializer(ClassFile cf)Adds an empty static class initializer.static MethodEntryaddDefaultConstructor(ClassFile cf)Adds a no-argument constructor whose body only invokes the superclass constructor.static intcomputeFrames(ClassFile cf)Computes StackMapTable frames for every method in a class that carries code.static voidcomputeFrames(ClassFile cf, MethodEntry method)Computes StackMapTable frames for one method, doing nothing if it carries no code.static booleancomputeFrames(ClassFile cf, String methodName, String descriptor)Computes StackMapTable frames for one method looked up by name and descriptor.static ClassFilecreateClass(ClassPool pool, String className, int accessFlags)Creates a class registered in a pool, adds a default constructor and class initializer, then rebuilds it.static ClassFilecreateClass(String className, int accessFlags)Creates a detached class carrying a default constructor and class initializer.static MethodEntrycreateMethodWithBody(ClassFile cf, int accessFlags, String methodName, String methodDescriptor)Creates a method whose generated body immediately returns a zero or null value of the declared return type.static MethodEntrygenerateGetter(ClassFile cf, FieldEntry entry, boolean isStatic)Generates a getter namedget<Field>for a field.static MethodEntrygenerateGetter(ClassFile cf, FieldEntry entry, String name, boolean isStatic)Generates a getter under a caller-chosen name for a field.static MethodEntrygenerateSetter(ClassFile cf, FieldEntry entry, boolean isStatic)Generates a setter namedset<Field>for a field.static MethodEntrygenerateSetter(ClassFile cf, FieldEntry entry, String name, boolean isStatic)Generates a setter under a caller-chosen name for a field.static voidsetFieldInitialValue(ClassFile cf, FieldEntry field, Object value)Prepends a store of a constant to a field at the top of the class initializer for a static field, or the no-argument constructor for an instance field, creating that method if absent.
-
-
-
Method Detail
-
createClass
public static ClassFile createClass(String className, int accessFlags)
Creates a detached class carrying a default constructor and class initializer.- Parameters:
className- binary name of the new classaccessFlags- class access flags- Returns:
- the new class file
-
createClass
public static ClassFile createClass(ClassPool pool, String className, int accessFlags) throws IOException
Creates a class registered in a pool, adds a default constructor and class initializer, then rebuilds it.- Parameters:
pool- pool that owns the new classclassName- binary name of the new classaccessFlags- class access flags- Returns:
- the new class file
- Throws:
IOException- if the pool cannot create the class or the rebuild fails
-
addDefaultConstructor
public static MethodEntry addDefaultConstructor(ClassFile cf)
Adds a no-argument constructor whose body only invokes the superclass constructor.- Parameters:
cf- class to receive the constructor- Returns:
- the added constructor
-
addDefaultClassInitializer
public static MethodEntry addDefaultClassInitializer(ClassFile cf)
Adds an empty static class initializer.- Parameters:
cf- class to receive the initializer- Returns:
- the added initializer
-
createMethodWithBody
public static MethodEntry createMethodWithBody(ClassFile cf, int accessFlags, String methodName, String methodDescriptor)
Creates a method whose generated body immediately returns a zero or null value of the declared return type.- Parameters:
cf- class to receive the methodaccessFlags- method access flagsmethodName- method namemethodDescriptor- method descriptor- Returns:
- the added method
-
generateGetter
public static MethodEntry generateGetter(ClassFile cf, FieldEntry entry, boolean isStatic) throws IOException
Generates a getter namedget<Field>for a field.- Parameters:
cf- class to receive the getterentry- field to readisStatic- true to emit a static getter reading a static field- Returns:
- the generated getter
- Throws:
IOException- if the method body cannot be finalized
-
generateGetter
public static MethodEntry generateGetter(ClassFile cf, FieldEntry entry, String name, boolean isStatic) throws IOException
Generates a getter under a caller-chosen name for a field.- Parameters:
cf- class to receive the getterentry- field to readname- name of the generated methodisStatic- true to emit a static getter reading a static field- Returns:
- the generated getter
- Throws:
IOException- if the method body cannot be finalized
-
generateSetter
public static MethodEntry generateSetter(ClassFile cf, FieldEntry entry, boolean isStatic) throws IOException
Generates a setter namedset<Field>for a field.- Parameters:
cf- class to receive the setterentry- field to writeisStatic- true to emit a static setter writing a static field- Returns:
- the generated setter
- Throws:
IOException- if the method body cannot be finalized
-
generateSetter
public static MethodEntry generateSetter(ClassFile cf, FieldEntry entry, String name, boolean isStatic) throws IOException
Generates a setter under a caller-chosen name for a field.- Parameters:
cf- class to receive the setterentry- field to writename- name of the generated methodisStatic- true to emit a static setter writing a static field- Returns:
- the generated setter
- Throws:
IOException- if the method body cannot be finalized
-
setFieldInitialValue
public static void setFieldInitialValue(ClassFile cf, FieldEntry field, Object value) throws IOException
Prepends a store of a constant to a field at the top of the class initializer for a static field, or the no-argument constructor for an instance field, creating that method if absent.- Parameters:
cf- class owning the fieldfield- field to initializevalue- Integer, Long, Float, Double, or String constant to store- Throws:
IOException- if the method body cannot be finalizedIllegalArgumentException- if the value is not one of the supported constant types
-
computeFrames
public static int computeFrames(ClassFile cf)
Computes StackMapTable frames for every method in a class that carries code.- Parameters:
cf- class to process- Returns:
- the number of methods that had frames computed
-
computeFrames
public static boolean computeFrames(ClassFile cf, String methodName, String descriptor)
Computes StackMapTable frames for one method looked up by name and descriptor.- Parameters:
cf- class to searchmethodName- method namedescriptor- method descriptor- Returns:
- true if the method was found and frames were computed
-
computeFrames
public static void computeFrames(ClassFile cf, MethodEntry method)
Computes StackMapTable frames for one method, doing nothing if it carries no code.- Parameters:
cf- class supplying the constant poolmethod- method to process
-
-