private void getter(String fieldName, CtField field) { try { CtMethod m = CtNewMethod.getter("get" + StringUtils.capitalize(fieldName), field); clazz.addMethod(m); } catch (Exception ex) { ex.printStackTrace(); } }
private void setter(String fieldName, CtField field) { try { CtMethod m = CtNewMethod.setter("set" + StringUtils.capitalize(fieldName), field); clazz.addMethod(m); } catch (Exception ex) { ex.printStackTrace(); } }
/** * convenience method that builds a method from a format string. {@see String.format} for more details * * @throws CannotCompileException */ public static CtMethod write(CtClass target, String format, Object ... args) throws CannotCompileException { String body = String.format( format, args ); // System.out.printf( "writing method into [%s]:%n%s%n", target.getName(), body ); log.debugf( "writing method into [%s]:%n%s", target.getName(), body ); CtMethod method = CtNewMethod.make( body, target ); target.addMethod( method ); return method; }
public Proxy<I> addMethod(String code) { return handleException(() -> ctClass.addMethod(CtNewMethod.make(code, ctClass))); }
public void makeScoreHashMethod(CtClass scClass) { // Map of previously extracted PMML names, and their java equivs HashMap<String,String> vars = new HashMap<String,String>(); StringBuilder sb = new StringBuilder(); sb.append("double score( java.util.HashMap row ) {\n"+ " double score = "+_initialScore+";\n"); try { for( int i=0; i<_rules.length; i++ ) _rules[i].makeFeatureHashMethod(sb,vars,scClass); sb.append(" return score;\n}\n"); CtMethod happyMethod = CtMethod.make(sb.toString(),scClass); scClass.addMethod(happyMethod); } catch( Exception re ) { Log.err(Sys.SCORM,"Crashing:"+sb.toString(), new RuntimeException(re)); } }
public void makeScoreAryMethod(CtClass scClass) { // Map of previously extracted PMML names, and their java equivs HashMap<String,String> vars = new HashMap<String,String>(); StringBuilder sb = new StringBuilder(); sb.append("double score( int[] MAP, java.lang.String[] SS, double[] DS ) {\n"+ " double score = "+_initialScore+";\n"); try { for( int i=0; i<_rules.length; i++ ) _rules[i].makeFeatureAryMethod(sb,vars,scClass,i); sb.append(" return score;\n}\n"); CtMethod happyMethod = CtMethod.make(sb.toString(),scClass); scClass.addMethod(happyMethod); } catch( Exception re ) { Log.err(Sys.SCORM,"Crashing:"+sb.toString(), new RuntimeException(re)); } }
public static Decoder gen(String cacheKey, String source) throws Exception { Decoder decoder; CtClass ctClass = pool.makeClass(cacheKey); ctClass.setInterfaces(new CtClass[]{pool.get(Decoder.class.getName())}); CtMethod staticMethod = CtNewMethod.make(source, ctClass); ctClass.addMethod(staticMethod); CtMethod interfaceMethod = CtNewMethod.make("" + "public Object decode(com.jsoniter.JsonIterator iter) {" + "return decode_(iter);" + "}", ctClass); ctClass.addMethod(interfaceMethod); decoder = (Decoder) ctClass.toClass().newInstance(); return decoder; }
cls.addField(CtField.make("private " + method, cls)); } else { cls.addMethod(CtNewMethod.make("public " + method, cls));
cls.addField(CtField.make("private " + method, cls)); } else { cls.addMethod(CtNewMethod.make("public " + 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(); }
private CtClass createControlClass(CtClass modifiedClass) throws Exception { CtClass patchClass = classPool.get(NameManger.getInstance().getPatchName(modifiedClass.getName())); patchClass.defrost(); CtClass controlClass = classPool.getAndRename(Constants.PATCH_TEMPLATE_FULL_NAME, NameManger.getInstance().getPatchControlName(modifiedClass.getSimpleName())); StringBuilder getRealParameterMethodBody = new StringBuilder(); getRealParameterMethodBody.append("public Object getRealParameter(Object parameter) {"); getRealParameterMethodBody.append("if(parameter instanceof " + modifiedClass.getName() + "){"); getRealParameterMethodBody. append("return new " + patchClass.getName() + "(parameter);"); getRealParameterMethodBody.append("}"); getRealParameterMethodBody.append("return parameter;}"); controlClass.addMethod(CtMethod.make(getRealParameterMethodBody.toString(), controlClass)); controlClass.getDeclaredMethod("accessDispatch").insertBefore(getAccessDispatchMethodBody(patchClass, modifiedClass.getName())); controlClass.getDeclaredMethod("isSupport").insertBefore(getIsSupportMethodBody(patchClass, modifiedClass.getName())); controlClass.defrost(); return controlClass; }
private void exposeRawEnumArray(CtClass cc) throws NotFoundException, CannotCompileException { CtField field; try { field = cc.getField("$VALUES"); } catch( NotFoundException nfe ) { // Eclipse apparently stores this in a different place. field = cc.getField("ENUM$VALUES"); } String body = "public static "+cc.getName()+" raw_enum(int i) { return i==255?null:"+field.getName()+"[i]; } "; try { cc.addMethod(CtNewMethod.make(body,cc)); } catch( CannotCompileException ce ) { Log.warn(Sys.WATER,"--- Compilation failure while compiler raw_enum for "+cc.getName()+"\n"+body+"\n------",ce); throw ce; } }
private void ensureNewInstance(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("newInstance", "()Lwater/Freezable;", ccms) ) { cc.addMethod(CtNewMethod.make( "public water.Freezable newInstance() {\n" + " return new " +cc.getName()+"();\n" + "}", cc)); } }
proxy); wmethod.setModifiers(mod); proxy.addMethod(wmethod);
private void ensureType(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("frozenType", "()I", ccms) ) { // Build a simple field & method returning the type token cc.addField(new CtField(CtClass.intType, "_frozen$type", cc)); cc.addMethod(CtNewMethod.make("public int frozenType() {" + " return _frozen$type == 0 ? (_frozen$type=water.TypeMap.onIce(\""+cc.getName()+"\")) : _frozen$type;" + "}",cc)); } }
private void processFields(CtClass clazz) throws CannotCompileException, NotFoundException { CtField[] fs = clazz.getDeclaredFields(); for (int i = 0; i < fs.length; ++i) { CtField f = fs[i]; int mod = f.getModifiers(); if ((mod & Modifier.PUBLIC) != 0 && (mod & Modifier.FINAL) == 0) { mod |= Modifier.STATIC; String name = f.getName(); CtClass ftype = f.getType(); CtMethod wmethod = CtNewMethod.wrapped(ftype, readPrefix + name, readParam, null, trapRead, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); CtClass[] writeParam = new CtClass[2]; writeParam[0] = classPool.get("java.lang.Object"); writeParam[1] = ftype; wmethod = CtNewMethod.wrapped(CtClass.voidType, writePrefix + name, writeParam, null, trapWrite, ConstParameter.string(name), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod); } } }
proxy.addField(f, CtField.Initializer.byParameter(1)); proxy.addMethod(CtNewMethod.getter(accessorObjectId, f));