public static CtClass addPatchConstruct(CtClass patchClass, CtClass sourceClass) { try { CtField originField = new CtField(sourceClass, ORIGINCLASS, patchClass); patchClass.addField(originField); StringBuilder patchClassConstruct = new StringBuilder(); patchClassConstruct.append(" public Patch(Object o) {"); patchClassConstruct.append(ORIGINCLASS + "=(" + sourceClass.getName() + ")o;"); patchClassConstruct.append("}"); CtConstructor constructor = CtNewConstructor.make(patchClassConstruct.toString(), patchClass); patchClass.addConstructor(constructor); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } return patchClass; }
public Proxy<I> addConstructor(String code) { return handleException(() -> ctClass.addConstructor(CtNewConstructor.make(code, ctClass))); }
if (method.length() > 0) { if (method.startsWith(className)) { cls.addConstructor(CtNewConstructor.make("public " + method, cls)); } else if (FIELD_PATTERN.matcher(method).matches()) { cls.addField(CtField.make("private " + method, cls));
if (method.length() > 0) { if (method.startsWith(className)) { cls.addConstructor(CtNewConstructor.make("public " + method, cls)); } else if (FIELD_PATTERN.matcher(method).matches()) { cls.addField(CtField.make("private " + method, cls));
private static TemplateRenderer tryToCompile(String source, Map<String, String> expressions, Map<String, String> constants, Class<?> modelType) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { ClassPool cp = ClassPool.getDefault(); CtClass sup = cp.get(Object.class.getCanonicalName()); CtClass cls = cp.makeClass("RapidoidTemplate" + ID_GEN.incrementAndGet(), sup); cls.addInterface(cp.get(TemplateRenderer.class.getCanonicalName())); cls.addConstructor(CtNewConstructor.defaultConstructor(cls)); addExpressions(expressions, cls); addConstants(constants, cls); CtClass[] params = {cp.get(RenderCtx.class.getCanonicalName())}; CtClass clsVoid = cp.get(void.class.getCanonicalName()); cls.addMethod(CtNewMethod.make(Modifier.PUBLIC, clsVoid, "render", params, new CtClass[0], source, cls)); return (TemplateRenderer) cls.toClass().newInstance(); }
mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc)); mCtc.addConstructor(CtNewConstructor .copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null)); } else { String[] sn = mCtc.getSimpleName().split("\\$+"); // inner class name include $. mCtc.addConstructor( CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc)); mCtc.addConstructor(CtNewConstructor .copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null)); } else { String[] sn = mCtc.getSimpleName().split("\\$+"); // inner class name include $. mCtc.addConstructor( CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
/** * Adds a default constructor to the super classes. */ private void modifySuperclass(CtClass orgclass) throws CannotCompileException, NotFoundException { CtClass superclazz; for (;; orgclass = superclazz) { superclazz = orgclass.getSuperclass(); if (superclazz == null) break; try { superclazz.getDeclaredConstructor(null); break; // the constructor with no arguments is found. } catch (NotFoundException e) { } superclazz.addConstructor( CtNewConstructor.defaultConstructor(superclazz)); } } }
/** * Adds a default constructor to the super classes. */ private void modifySuperclass(CtClass orgclass) throws CannotCompileException, NotFoundException { CtClass superclazz; for (;; orgclass = superclazz) { superclazz = orgclass.getSuperclass(); if (superclazz == null) break; try { superclazz.getDeclaredConstructor(null); break; // the constructor with no arguments is found. } catch (NotFoundException e) { } superclazz.addConstructor( CtNewConstructor.defaultConstructor(superclazz)); } } }
scClass.addConstructor(happyConst);
constructor.setModifiers(Modifier.PUBLIC); constructor.setBody("{super(new " + UselessInvocationHandler.class.getName() + "());}"); mCtc.addConstructor(constructor);
constructor.setModifiers(Modifier.PUBLIC); constructor.setBody("{super(new " + UselessInvocationHandler.class.getName() + "());}"); mCtc.addConstructor(constructor);
cc.addConstructor(cons);
proxy.addConstructor(CtNewConstructor.defaultConstructor(proxy)); CtConstructor cons = CtNewConstructor.skeleton(proxyConstructorParamTypes, null, proxy); proxy.addConstructor(cons);
mCtc.addConstructor(CtNewConstructor.defaultConstructor(mCtc)); if (mConstructors != null) { for (String code : mConstructors) { if (code.charAt(0) == ':') { mCtc.addConstructor(CtNewConstructor.copy(getCtConstructor(mCopyConstructors.get(code.substring(1))), mCtc, null)); } else { String[] sn = mCtc.getSimpleName().split("\\$+"); // inner class name include $. mCtc.addConstructor(CtNewConstructor.make(code.replaceFirst(SIMPLE_NAME_TAG, sn[sn.length - 1]), mCtc));
/** Adds a new field of type double to the customized class */ public void addDoubleField(String fieldName) { // FIXME: this should support default values but does not ClassFile classFile = ctClass.getClassFile(); ConstPool constPool = classFile.getConstPool(); try { // add field FieldInfo fieldInfo = new FieldInfo(constPool, fieldName, "D"); classFile.addField(fieldInfo); CtConstructor ctor = CtNewConstructor.defaultConstructor(ctClass); ctClass.addConstructor(ctor); addDoubleSetter(classFile, fieldName); addDoubleGetter(classFile, fieldName); } catch (Exception e) { throw new RuntimeException(e); } }
proxy.addConstructor(CtNewConstructor.defaultConstructor(proxy)); CtConstructor cons = CtNewConstructor.skeleton(proxyConstructorParamTypes, null, proxy); proxy.addConstructor(cons);
private void ensureNullaryCtor(CtClass cc) throws NotFoundException, CannotCompileException { // Build a null-ary constructor if needed String clzname = cc.getSimpleName(); if( !hasExisting(clzname,"()V",cc.getDeclaredConstructors()) ) { String body = "public "+clzname+"() { }"; cc.addConstructor(CtNewConstructor.make(body,cc)); } else { CtConstructor ctor = cc.getConstructor("()V"); ctor.setModifiers(javassist.Modifier.setPublic(ctor.getModifiers())); } }