/** * Creates a copy of a constructor. * This is a convenience method for calling * {@link CtConstructor#CtConstructor(CtConstructor, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param c the copied constructor. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtConstructor#CtConstructor(CtConstructor,CtClass,ClassMap) */ public static CtConstructor copy(CtConstructor c, CtClass declaring, ClassMap map) throws CannotCompileException { return new CtConstructor(c, declaring, map); }
/** * Creates a copy of a constructor. * This is a convenience method for calling * {@link CtConstructor#CtConstructor(CtConstructor, CtClass, ClassMap) this constructor}. * See the description of the constructor for particular behavior of the copying. * * @param c the copied constructor. * @param declaring the class to which the created method is added. * @param map the hash table associating original class names * with substituted names. * It can be <code>null</code>. * * @see CtConstructor#CtConstructor(CtConstructor,CtClass,ClassMap) */ public static CtConstructor copy(CtConstructor c, CtClass declaring, ClassMap map) throws CannotCompileException { return new CtConstructor(c, declaring, map); }
private void makeBehaviorCache(CtMember.Cache cache) { List list = getClassFile3(false).getMethods(); int n = list.size(); for (int i = 0; i < n; ++i) { MethodInfo minfo = (MethodInfo)list.get(i); if (minfo.isMethod()) { CtMethod newMethod = new CtMethod(minfo, this); cache.addMethod(newMethod); } else { CtConstructor newCons = new CtConstructor(minfo, this); cache.addConstructor(newCons); } } }
/** * Creates a public constructor. * * @param parameters a list of the parameter types. * @param exceptions a list of the exception types. * @param body the source text of the constructor body. * It must be a block surrounded by <code>{}</code>. * If it is <code>null</code>, the substituted * constructor body does nothing except calling * <code>super()</code>. * @param declaring the class to which the created method is added. */ public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException { try { CtConstructor cc = new CtConstructor(parameters, declaring); cc.setExceptionTypes(exceptions); cc.setBody(body); return cc; } catch (NotFoundException e) { throw new CannotCompileException(e); } }
/** * Creates a public constructor. * * @param parameters a list of the parameter types. * @param exceptions a list of the exception types. * @param body the source text of the constructor body. * It must be a block surrounded by <code>{}</code>. * If it is <code>null</code>, the substituted * constructor body does nothing except calling * <code>super()</code>. * @param declaring the class to which the created method is added. */ public static CtConstructor make(CtClass[] parameters, CtClass[] exceptions, String body, CtClass declaring) throws CannotCompileException { try { CtConstructor cc = new CtConstructor(parameters, declaring); cc.setExceptionTypes(exceptions); cc.setBody(body); return cc; } catch (NotFoundException e) { throw new CannotCompileException(e); } }
CtConstructor constructor = new CtConstructor(null, mCtc); constructor.setModifiers(Modifier.PUBLIC); constructor.setBody("{super(new " + UselessInvocationHandler.class.getName() + "());}");
CtConstructor constructor = new CtConstructor(null, mCtc); constructor.setModifiers(Modifier.PUBLIC); constructor.setBody("{super(new " + UselessInvocationHandler.class.getName() + "());}");
private void makeBehaviorCache(CtMember.Cache cache) { List<MethodInfo> methods = getClassFile3(false).getMethods(); for (MethodInfo minfo:methods) if (minfo.isMethod()) cache.addMethod(new CtMethod(minfo, this)); else cache.addConstructor(new CtConstructor(minfo, this)); }
public static CtConstructor wrapped(CtClass[] parameterTypes, CtClass[] exceptionTypes, int howToCallSuper, CtMethod body, ConstParameter constParam, CtClass declaring) throws CannotCompileException { try { CtConstructor cons = new CtConstructor(parameterTypes, declaring); cons.setExceptionTypes(exceptionTypes); Bytecode code = makeBody(declaring, declaring.getClassFile2(), howToCallSuper, body, parameterTypes, constParam); cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); // a stack map table is not needed. return cons; } catch (NotFoundException e) { throw new CannotCompileException(e); } }
public static CtConstructor wrapped(CtClass[] parameterTypes, CtClass[] exceptionTypes, int howToCallSuper, CtMethod body, ConstParameter constParam, CtClass declaring) throws CannotCompileException { try { CtConstructor cons = new CtConstructor(parameterTypes, declaring); cons.setExceptionTypes(exceptionTypes); Bytecode code = makeBody(declaring, declaring.getClassFile2(), howToCallSuper, body, parameterTypes, constParam); cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); // a stack map table is not needed. return cons; } catch (NotFoundException e) { throw new CannotCompileException(e); } }
/** * Creates a default (public) constructor. * * <p>The created constructor takes no parameter. It calls * <code>super()</code>. */ public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException { CtConstructor cons = new CtConstructor((CtClass[])null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); Bytecode code = new Bytecode(cp, 1, 1); code.addAload(0); try { code.addInvokespecial(declaring.getSuperclass(), "<init>", "()V"); } catch (NotFoundException e) { throw new CannotCompileException(e); } code.add(Bytecode.RETURN); // no need to construct a stack map table. cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); return cons; }
/** * Creates a default (public) constructor. * * <p>The created constructor takes no parameter. It calls * <code>super()</code>. */ public static CtConstructor defaultConstructor(CtClass declaring) throws CannotCompileException { CtConstructor cons = new CtConstructor((CtClass[])null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); Bytecode code = new Bytecode(cp, 1, 1); code.addAload(0); try { code.addInvokespecial(declaring.getSuperclass(), "<init>", "()V"); } catch (NotFoundException e) { throw new CannotCompileException(e); } code.add(Bytecode.RETURN); // no need to construct a stack map table. cons.getMethodInfo2().setCodeAttribute(code.toCodeAttribute()); return cons; }
private static void addEnumConstructor(ClassPool classPool, CtClass ctClass) throws Exception { String src = "super($1, $2);"; CtConstructor ctConstructor = new CtConstructor( classPool.get(new String[] {String.class.getName(), int.class.getName()}), ctClass); ctConstructor.setBody(src); ctClass.addConstructor(ctConstructor); }
CtMember.Cache cache = hasMemberCache(); if (cache != null) cache.addConstructor(new CtConstructor(m, this));
try { if (md.isConstructor()) { CtConstructor cons = new CtConstructor(plist, gen.getThisClass()); cons.setModifiers(mod);
CtMember.Cache cache = hasMemberCache(); if (cache != null) cache.addConstructor(new CtConstructor(m, this));
CtField reflectorField = new CtField(interfaceCtClass, "publicInterface", reflectorImplClass); reflectorImplClass.addField(reflectorField); CtConstructor constructor = new CtConstructor(new CtClass[] {interfaceCtClass}, reflectorImplClass); StringBuilder sb = new StringBuilder(); reflectorImplClass.addConstructor(constructor);
try { if (md.isConstructor()) { CtConstructor cons = new CtConstructor(plist, gen.getThisClass()); cons.setModifiers(mod);
CtField reflectorField = new CtField(reflectorClass, "reflector", reflectorImplClass); reflectorImplClass.addField(reflectorField); CtConstructor constructor = new CtConstructor(new CtClass[] {reflectorClass}, reflectorImplClass); StringBuilder sb = new StringBuilder(); reflectorImplClass.addConstructor(constructor);
private static void addEnumConstructor(ClassPool classPool, CtClass ctClass) throws Exception { String src = "super($1, $2);"; CtConstructor ctConstructor = new CtConstructor( classPool.get(new String[] {String.class.getName(), int.class.getName()}), ctClass); ctConstructor.setBody(src); ctClass.addConstructor(ctConstructor); }