📄 ctfield.java
字号:
public static Initializer constant(String s) { return new StringInitializer(s); } /** * Makes an initializer using a constructor parameter. * * <p>The initial value is the * N-th parameter given to the constructor of the object including * the field. If the constructor takes less than N parameters, * the field is not initialized. * If the field is static, it is never initialized. * * @param nth the n-th (>= 0) parameter is used as * the initial value. * If nth is 0, then the first parameter is * used. */ public static Initializer byParameter(int nth) { ParamInitializer i = new ParamInitializer(); i.nthParam = nth; return i; } /** * Makes an initializer creating a new object. * * <p>This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameter: * * <ul><code>Object obj</code> - the object including the field.<br> * </ul> * * <p>If the initialized field is static, then the constructor does * not receive any parameters. * * @param objectType the class instantiated for the initial value. */ public static Initializer byNew(CtClass objectType) { NewInitializer i = new NewInitializer(); i.objectType = objectType; i.stringParams = null; i.withConstructorParams = false; return i; } /** * Makes an initializer creating a new object. * * <p>This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * <code>String[] strs</code> - the character strings specified * by <code>stringParams</code><br> * </ul> * * <p>If the initialized field is static, then the constructor * receives only <code>strs</code>. * * @param objectType the class instantiated for the initial value. * @param stringParams the array of strings passed to the * constructor. */ public static Initializer byNew(CtClass objectType, String[] stringParams) { NewInitializer i = new NewInitializer(); i.objectType = objectType; i.stringParams = stringParams; i.withConstructorParams = false; return i; } /** * Makes an initializer creating a new object. * * <p>This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * <code>Object[] args</code> - the parameters passed to the * constructor of the object including the * filed. * </ul> * * <p>If the initialized field is static, then the constructor does * not receive any parameters. * * @param objectType the class instantiated for the initial value. * * @see javassist.CtField.Initializer#byNewArray(CtClass,int) * @see javassist.CtField.Initializer#byNewArray(CtClass,int[]) */ public static Initializer byNewWithParams(CtClass objectType) { NewInitializer i = new NewInitializer(); i.objectType = objectType; i.stringParams = null; i.withConstructorParams = true; return i; } /** * Makes an initializer creating a new object. * * <p>This initializer creates a new object and uses it as the initial * value of the field. The constructor of the created object receives * the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * <code>String[] strs</code> - the character strings specified * by <code>stringParams</code><br> * <code>Object[] args</code> - the parameters passed to the * constructor of the object including the * filed. * </ul> * * <p>If the initialized field is static, then the constructor receives * only <code>strs</code>. * * @param objectType the class instantiated for the initial value. * @param stringParams the array of strings passed to the * constructor. */ public static Initializer byNewWithParams(CtClass objectType, String[] stringParams) { NewInitializer i = new NewInitializer(); i.objectType = objectType; i.stringParams = stringParams; i.withConstructorParams = true; return i; } /** * Makes an initializer calling a static method. * * <p>This initializer calls a static method and uses the returned * value as the initial value of the field. * The called method receives the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * </ul> * * <p>If the initialized field is static, then the method does * not receive any parameters. * * <p>The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. */ public static Initializer byCall(CtClass methodClass, String methodName) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = null; i.withConstructorParams = false; return i; } /** * Makes an initializer calling a static method. * * <p>This initializer calls a static method and uses the returned * value as the initial value of the field. The called method * receives the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * <code>String[] strs</code> - the character strings specified * by <code>stringParams</code><br> * </ul> * * <p>If the initialized field is static, then the method * receive only <code>strs</code>. * * <p>The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. * @param stringParams the array of strings passed to the * static method. */ public static Initializer byCall(CtClass methodClass, String methodName, String[] stringParams) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = stringParams; i.withConstructorParams = false; return i; } /** * Makes an initializer calling a static method. * * <p>This initializer calls a static method and uses the returned * value as the initial value of the field. The called method * receives the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * <code>Object[] args</code> - the parameters passed to the * constructor of the object including the * filed. * </ul> * * <p>If the initialized field is static, then the method does * not receive any parameters. * * <p>The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. */ public static Initializer byCallWithParams(CtClass methodClass, String methodName) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = null; i.withConstructorParams = true; return i; } /** * Makes an initializer calling a static method. * * <p>This initializer calls a static method and uses the returned * value as the initial value of the field. The called method * receives the parameters: * * <ul><code>Object obj</code> - the object including the field.<br> * <code>String[] strs</code> - the character strings specified * by <code>stringParams</code><br> * <code>Object[] args</code> - the parameters passed to the * constructor of the object including the * filed. * </ul> * * <p>If the initialized field is static, then the method * receive only <code>strs</code>. * * <p>The type of the returned value must be the same as the field * type. * * @param methodClass the class that the static method is * declared in. * @param methodName the name of the satic method. * @param stringParams the array of strings passed to the * static method. */ public static Initializer byCallWithParams(CtClass methodClass, String methodName, String[] stringParams) { MethodInitializer i = new MethodInitializer(); i.objectType = methodClass; i.methodName = methodName; i.stringParams = stringParams; i.withConstructorParams = true; return i; } /** * Makes an initializer creating a new array. * * @param type the type of the array. * @param size the size of the array. * @throws NotFoundException if the type of the array components * is not found. */ public static Initializer byNewArray(CtClass type, int size) throws NotFoundException { return new ArrayInitializer(type.getComponentType(), size); } /** * Makes an initializer creating a new multi-dimensional array. * * @param type the type of the array. * @param sizes an <code>int</code> array of the size in every * dimension. * The first element is the size in the first * dimension. The second is in the second, etc. */ public static Initializer byNewArray(CtClass type, int[] sizes) { return new MultiArrayInitializer(type, sizes); } /** * Makes an initializer. * * @param source initializer expression. */ public static Initializer byExpr(String source) { return new CodeInitializer(source); } static Initializer byExpr(ASTree source) { return new PtreeInitializer(source); } // Check whether this initializer is valid for the field type. // If it is invaild, this method throws an exception. void check(CtClass type) throws CannotCompileException {} // produce codes for initialization abstract int compile(CtClass type, String name, Bytecode code, CtClass[] parameters, Javac drv) throws CannotCompileException; // produce codes for initialization abstract int compileIfStatic(CtClass type, String name, Bytecode code, Javac drv) throws CannotCompileException; // returns the index of CONSTANT_Integer_info etc // if the value is constant. Otherwise, 0. int getConstantValue(ConstPool cp, CtClass type) { return 0; } } static abstract class CodeInitializer0 extends Initializer { abstract void compileExpr(Javac drv) throws CompileError; int compile(CtClass type, String name, Bytecode code, CtClass[] parameters, Javac drv) throws CannotCompileException { try { code.addAload(0); compileExpr(drv); code.addPutfield(Bytecode.THIS, name, Descriptor.of(type)); return code.getMaxStack(); } catch (CompileError e) { throw new CannotCompileException(e); } } int compileIfStatic(CtClass type, String name, Bytecode code, Javac drv) throws CannotCompileException { try { compileExpr(drv); code.addPutstatic(Bytecode.THIS, name, Descriptor.of(type)); return code.getMaxStack(); } catch (CompileError e) { throw new CannotCompileException(e); } } int getConstantValue2(ConstPool cp, CtClass type, ASTree tree) { if (type.isPrimitive()) { if (tree instanceof IntConst) { long value = ((IntConst)tree).get(); if (type == CtClass.doubleType) return cp.addDoubleInfo((double)value); else if (type == CtClass.floatType) return cp.addFloatInfo((float)value); else if (type == CtClass.longType) return cp.addLongInfo(value); else if (type != CtClass.voidType) return cp.addIntegerInfo((int)value); } else if (tree instanceof DoubleConst) { double value = ((DoubleConst)tree).get(); if (type == CtClass.floatType) return cp.addFloatInfo((float)value); else if (type == CtClass.doubleType) return cp.addDoubleInfo(value); } } else if (tree instanceof StringL && type.getName().equals(javaLangString)) return cp.addStringInfo(((StringL)tree).get()); return 0; } } static class CodeInitializer extends CodeInitializer0 { private String expression; CodeInitializer(String expr) { expression = expr; } void compileExpr(Javac drv) throws CompileError { drv.compileExpr(expression); } int getConstantValue(ConstPool cp, CtClass type) { try { ASTree t = Javac.parseExpr(expression, new SymbolTable()); return getConstantValue2(cp, type, t); } catch (CompileError e) { return 0; } } } static class PtreeInitializer extends CodeInitializer0 { private ASTree expression; PtreeInitializer(ASTree expr) { expression = expr; } void compileExpr(Javac drv) throws CompileError { drv.compileExpr(expression); } int getConstantValue(ConstPool cp, CtClass type) { return getConstantValue2(cp, type, expression); } } /** * A field initialized with a parameter passed to the constructor * of the class containing that field. */ static class ParamInitializer extends Initializer { int nthParam; ParamInitializer() {} int compile(CtClass type, String name, Bytecode code,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -