/** * 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() + "());}"); mCtc.addConstructor(constructor);
CtConstructor constructor = new CtConstructor(null, mCtc); constructor.setModifiers(Modifier.PUBLIC); constructor.setBody("{super(new " + UselessInvocationHandler.class.getName() + "());}"); mCtc.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); }
sb.append("this.publicInterface = $1;"); sb.append("}"); constructor.setBody(sb.toString());
sb.append("this.reflector = $1;"); sb.append("}"); constructor.setBody(sb.toString());
public static CtConstructor defaultConstructor(final CtClass declaring) throws CannotCompileException { // 默认添加无参构造器 CtConstructor cons = new CtConstructor(null, declaring); cons.setBody("{}"); return cons; }
public static CtConstructor defaultConstructor(final CtClass declaring) throws CannotCompileException { // 默认添加无参构造器 CtConstructor cons = new CtConstructor(null, declaring); cons.setBody("{}"); 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); }
private void updateConstructors(CtClass klass, Node node) { CtConstructor[] constructors = klass.getDeclaredConstructors(); for (int i = 0; i < constructors.length; i++) { try { constructors[i].setBody(emptyBodyCode(CtClass.voidType)); } catch (CannotCompileException ce) { throw new RuntimeException(klass.getName(), ce); } } }
/** * Create a constructor. This method simply records the input parameters in * appropriately named fields. * * @param * @param classes */ private void createConstructor(CtClass concreteClass, CtClass usageMBeanInterface, CtClass notificationSource,CtClass usageComponent) throws Exception { CtConstructor ctCons = new CtConstructor(new CtClass[]{usageMBeanInterface,notificationSource,usageComponent}, concreteClass); ctCons.setBody("{ super($1,$2,$3); }"); concreteClass.addConstructor(ctCons); }
@Override public CtClass transform(final CtClass clazz) throws CannotCompileException { if (getStrategy() == CLASSLOADER) { if (MockGateway.staticConstructorCall(clazz.getName()) != MockGateway.PROCEED) { CtConstructor classInitializer = clazz.makeClassInitializer(); classInitializer.setBody("{}"); } } return clazz; } }
/** * Create a constructor. This method simply records the input parameters in * appropriately named fields. * * @param * @param classes */ private void createConstructor(CtClass concreteClass, CtClass usageMBeanInterface, CtClass notificationSource) throws Exception { CtConstructor ctCons = new CtConstructor(new CtClass[]{usageMBeanInterface,notificationSource}, concreteClass); ctCons.setBody("{ super($1,$2); }"); concreteClass.addConstructor(ctCons); }
/** * Create a constructor. This method simply records the input parameters in * appropriately named fields. * * @param * @param classes */ private void createConstructor(CtClass concreteClass, CtClass sleeContainerClass, CtClass resourceAdaptorTypeIDClass) throws Exception { CtConstructor ctCons = new CtConstructor(new CtClass[]{sleeContainerClass,resourceAdaptorTypeIDClass}, concreteClass); ctCons.setBody("{ super($1,$2); }"); concreteClass.addConstructor(ctCons); }
private void createDefaultConstructor(CtClass concreteClass) { CtConstructor defaultConstructor = new CtConstructor(null, concreteClass); String constructorBody = "{ this.reset(); }"; try { defaultConstructor.setBody(constructorBody); concreteClass.addConstructor(defaultConstructor); logger.debug("DefaultConstructor created"); } catch (CannotCompileException e) { //Auto-generated catch block e.printStackTrace(); } }
public void fixConstructor(CtConstructor ctConstructor) throws NotFoundException, CannotCompileException { String methodBody = generateConstructorBody(ctConstructor.getParameterTypes()); ctConstructor.setBody("{\n" + methodBody + "}\n"); wrapMethodInvocation(ctConstructor, false); }
public void addConstructor(Class<?>[] types, String string) throws ObjectCompositionException { try { logger.trace("public {}({}) {{}}", new Object[] { getName(), types, string }); CtConstructor con = new CtConstructor(asCtClassArray(types), cc); con.setBody("{" + string + "}"); cc.addConstructor(con); } catch (CannotCompileException e) { throw new ObjectCompositionException(e); } catch (NotFoundException e) { throw new ObjectCompositionException(e); } }
private CtConstructor createConstructor(ConstPool constPool, CtClass declaringClass) throws NotFoundException, CannotCompileException { CtConstructor cc = new CtConstructor(new CtClass[]{ declaringClass.getClassPool().getCtClass(Object.class.getName() + "[]") }, declaringClass); // Define the constructor behavior cc.setBody("super($1);"); cc.setModifiers(Modifier.PUBLIC); // Add the @Inject annotation to the constructor addInjectAnnotation(constPool, cc); // Add the @Assisted annotation to the constructor parameter addAssistedAnnotation(constPool, cc); return cc; }
public static CtClass makeWrapper(CtClass containerClass, CtClass wrappedClass, String wrapperName, File destDir) throws NotFoundException, CannotCompileException, IOException { CtClass wrapperClass = makeWrapperSubclass(containerClass, wrappedClass, wrapperName); CtField wrappedField = new CtField(wrappedClass, "wrapped", wrapperClass); wrappedField.setModifiers(Modifier.PROTECTED | Modifier.FINAL); wrapperClass.addField(wrappedField); CtConstructor wrapperConstructor = new CtConstructor(new CtClass[] {wrappedClass}, wrapperClass); wrapperConstructor.setBody("this.wrapped = $1;"); wrapperClass.addConstructor(wrapperConstructor); wrapMethods(wrapperClass, wrappedClass); wrapperClass.writeFile(destDir.getPath()); return wrapperClass; } }