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); } } }
/** * Creates a public abstract method. The created method must be * added to a class with <code>CtClass.addMethod()</code>. * * @param declaring the class to which the created method is added. * @param returnType the type of the returned value * @param mname the method name * @param parameters a list of the parameter types * * @see CtClass#addMethod(CtMethod) */ public CtMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass declaring) { this(null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); String desc = Descriptor.ofMethod(returnType, parameters); methodInfo = new MethodInfo(cp, mname, desc); setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); }
ConstParameter.integer(i), proxy); wmethod.setModifiers(mod); proxy.addMethod(wmethod);
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); } } }
CtMethod cm = new CtMethod(returnType, mname, parameters, declaring); cm.setModifiers(modifiers); cm.setExceptionTypes(exceptions); cm.setBody(body);
if (Modifier.isFinal(mod)) { mod &= ~Modifier.FINAL; m2.setModifiers(mod); m2.setModifiers(mod); clazz.addMethod(m2); body, ConstParameter.integer(identifier), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod);
/** * Creates a public abstract method. The created method must be * added to a class with <code>CtClass.addMethod()</code>. * * @param declaring the class to which the created method is added. * @param returnType the type of the returned value * @param mname the method name * @param parameters a list of the parameter types * * @see CtClass#addMethod(CtMethod) */ public CtMethod(CtClass returnType, String mname, CtClass[] parameters, CtClass declaring) { this(null, declaring); ConstPool cp = declaring.getClassFile2().getConstPool(); String desc = Descriptor.ofMethod(returnType, parameters); methodInfo = new MethodInfo(cp, mname, desc); setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); }
ConstParameter.integer(i), proxy); wmethod.setModifiers(mod); proxy.addMethod(wmethod);
CtMethod cm = new CtMethod(returnType, mname, parameters, declaring); cm.setModifiers(modifiers); cm.setExceptionTypes(exceptions); cm.setBody(body);
if (Modifier.isFinal(mod)) { mod &= ~Modifier.FINAL; m2.setModifiers(mod); m2.setModifiers(mod); clazz.addMethod(m2); body, ConstParameter.integer(identifier), clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod);
public static CtMethod wrapped(CtClass returnType, String mname, CtClass[] parameterTypes, CtClass[] exceptionTypes, CtMethod body, ConstParameter constParam, CtClass declaring) throws CannotCompileException { CtMethod mt = new CtMethod(returnType, mname, parameterTypes, declaring); mt.setModifiers(body.getModifiers()); try { mt.setExceptionTypes(exceptionTypes); } catch (NotFoundException e) { throw new CannotCompileException(e); } Bytecode code = makeBody(declaring, declaring.getClassFile2(), body, parameterTypes, returnType, constParam); MethodInfo minfo = mt.getMethodInfo2(); minfo.setCodeAttribute(code.toCodeAttribute()); // a stack map has been already created. return mt; }
public static CtMethod wrapped(CtClass returnType, String mname, CtClass[] parameterTypes, CtClass[] exceptionTypes, CtMethod body, ConstParameter constParam, CtClass declaring) throws CannotCompileException { CtMethod mt = new CtMethod(returnType, mname, parameterTypes, declaring); mt.setModifiers(body.getModifiers()); try { mt.setExceptionTypes(exceptionTypes); } catch (NotFoundException e) { throw new CannotCompileException(e); } Bytecode code = makeBody(declaring, declaring.getClassFile2(), body, parameterTypes, returnType, constParam); MethodInfo minfo = mt.getMethodInfo2(); minfo.setCodeAttribute(code.toCodeAttribute()); // a stack map has been already created. return mt; }
public void addMethod(CtMethod m) throws CannotCompileException { checkModify(); if (m.getDeclaringClass() != this) throw new CannotCompileException("bad declaring class"); int mod = m.getModifiers(); if ((getModifiers() & Modifier.INTERFACE) != 0) { if (Modifier.isProtected(mod) || Modifier.isPrivate(mod)) throw new CannotCompileException( "an interface method must be public: " + m.toString()); m.setModifiers(mod | Modifier.PUBLIC); } getMembers().addMethod(m); getClassFile2().addMethod(m.getMethodInfo2()); if ((mod & Modifier.ABSTRACT) != 0) setModifiers(getModifiers() | Modifier.ABSTRACT); }
@Override public void addMethod(CtMethod m) throws CannotCompileException { checkModify(); if (m.getDeclaringClass() != this) throw new CannotCompileException("bad declaring class"); int mod = m.getModifiers(); if ((getModifiers() & Modifier.INTERFACE) != 0) { if (Modifier.isProtected(mod) || Modifier.isPrivate(mod)) throw new CannotCompileException( "an interface method must be public: " + m.toString()); m.setModifiers(mod | Modifier.PUBLIC); } getMembers().addMethod(m); getClassFile2().addMethod(m.getMethodInfo2()); if ((mod & Modifier.ABSTRACT) != 0) setModifiers(getModifiers() | Modifier.ABSTRACT); }
CtMethod method = new CtMethod(rtype, r.getVariable().get(), plist, gen.getThisClass()); method.setModifiers(mod); gen.setThisMethod(method); md.accept(gen); bytecode.toCodeAttribute()); else method.setModifiers(mod | Modifier.ABSTRACT);
CtMethod method = new CtMethod(rtype, r.getVariable().get(), plist, gen.getThisClass()); method.setModifiers(mod); gen.setThisMethod(method); md.accept(gen); bytecode.toCodeAttribute()); else method.setModifiers(mod | Modifier.ABSTRACT);
private static void makeMethodPublicStatic(CtClass c, String methodName) throws NotFoundException { CtMethod method = c.getDeclaredMethod(methodName); method.setModifiers(Modifier.PUBLIC + Modifier.STATIC); } }
@Override public CtMethod merge() throws Exception { CtMethod method = versions.lastEntry().getValue(); wipeBody(method); method.setModifiers(mergedModifiers()); return method; }
public boolean instrumentMethod(CtClass clazz, CtMethod method) throws InterceptorException { // replace the body of the intercepted method with generated code block int mod = method.getModifiers(); if (Modifier.isPublic(mod)) { method.setModifiers(Modifier.setProtected(mod)); } return true; } }
protected void addArgumentFieldAndAccessor() throws CannotCompileException, NotFoundException { // duplicated code (See OptimizedBehaviour class) CtField inconsistentArgs = new CtField(CtClass.booleanType, "inconsistentArgs", jp); jp.addField(inconsistentArgs, CtField.Initializer.byExpr("false")); super.addArgumentFieldAndAccessor(); CtMethod enforceArgsConsistency = CtNewMethod.make( createEnforceArgsConsistencyBody(), jp); enforceArgsConsistency.setModifiers(Modifier.PROTECTED); jp.addMethod(enforceArgsConsistency); }