for (String code : mMethods) { if (code.charAt(0) == ':') { mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null)); } else {
for (String code : mMethods) { if (code.charAt(0) == ':') { mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null)); } else {
CtMethod method = CtNewMethod.copy(intfMethod, targetCt, null);
for (String code : mMethods) { if (code.charAt(0) == ':') mCtc.addMethod(CtNewMethod.copy(getCtMethod(mCopyMethods.get(code.substring(1))), code.substring(1, code.indexOf('(')), mCtc, null)); else {
CtMethod newGetClass = CtNewMethod.copy(modelGetClass, target, null); newGetClass.setBody("{ return " + target.getName() + ".class; }"); CtMethod newMethod; if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers)) { newMethod = CtNewMethod.copy(method, target, classMap); newMethod.instrument(conv); } else if ("modelClass".equals(method.getName())) {
private void generateParamNamesGetter(CtClass declaring, CtClass destination) throws NotFoundException, CannotCompileException { //little helper method, it is used on init, only once. CtMethod abstractMethod = declaring.getDeclaredMethod(_METHOD_GET_PARAM_NAMES_); CtMethod concreteMethod = CtNewMethod.copy(abstractMethod, destination, null); String body="{"; body += java.util.HashSet.class.getName()+" ret = new "+java.util.HashSet.class.getName()+"();"; for(String fieldName : this.generatedFields) { body +="ret.add(\""+fieldName+"\");"; } body += "return ($r)ret;"; body += "}"; if (logger.isTraceEnabled()) logger.trace("Adding get usage param names getter with source "+body); concreteMethod.setBody(body); destination.addMethod(concreteMethod); }
private void copyStaticMethodsToSubclass(CtClass document, CtClass targetClass) throws Exception { CtMethod[] ctMethods = document.getMethods(); for (CtMethod ctMethod : ctMethods) { if (Modifier.isStatic(ctMethod.getModifiers()) && !shouldNotCopyToSubclassStaticMethods.contains(ctMethod.getName())) { CtMethod ctNewMethod = CtNewMethod.copy(ctMethod, targetClass, null); targetClass.addMethod(ctNewMethod); } } }
private void copyStaticMethodsToSubclass(CtClass document, CtClass targetClass) throws Exception { CtMethod[] ctMethods = document.getMethods(); for (CtMethod ctMethod : ctMethods) { if (Modifier.isStatic(ctMethod.getModifiers()) && !shouldNotCopyToSubclassStaticMethods.contains(ctMethod.getName())) { CtMethod ctNewMethod = CtNewMethod.copy(ctMethod, targetClass, null); targetClass.addMethod(ctNewMethod); } } }
final String nname = oname + "$impl"; method.setName(nname); final CtMethod interceptor = CtNewMethod.copy(method, oname, clazz, null);
private void implementMethod(CtClass concreteClass, CtMethod method, String interceptorAccess) throws Exception { // copy method to concrete class CtMethod newMethod = CtNewMethod.copy( method, concreteClass, null ); // generate body String returnStatement = method.getReturnType().equals(CtClass.voidType) ? "" : "return ($r)"; String body= "{ " + " try {" + returnStatement + interceptorAccess +'.'+ method.getName()+"($$);" + " } catch ("+ReadOnlyProfileException.class.getName()+" e) {" + " throw new "+UnsupportedOperationException.class.getName()+"(e.getMessage(),e);" + " }" + "}"; if(logger.isTraceEnabled()) { logger.trace("Instrumented method, name:"+method.getName()+", with body:\n"+body); } newMethod.setBody(body); // add to concrete class concreteClass.addMethod(newMethod); } }
private void generateDefaultUsageParameterGetter( CtClass profileConcreteClass) { String methodName = "getDefaultUsageParameterSet"; for (CtMethod ctMethod : profileConcreteClass.getMethods()) { if (ctMethod.getName().equals(methodName)) { try { // copy method, we can't just add body becase it is in super // class and does not sees profileObject field CtMethod ctMethodCopy = CtNewMethod.copy(ctMethod, profileConcreteClass, null); // create the method body String methodBody = "{ return ($r)" + ClassGeneratorUtils.MANAGEMENT_HANDLER + ".getDefaultUsageParameterSet(profileObject); }"; if (logger.isTraceEnabled()) logger.trace("Implemented method " + methodName + " , body = " + methodBody); ctMethodCopy.setBody(methodBody); profileConcreteClass.addMethod(ctMethodCopy); } catch (CannotCompileException e) { throw new SLEEException(e.getMessage(), e); } } } }
private void instrumentQuery(CtClass profileTableConcreteClass, CtMethod iMethod, String interceptorQuery) throws Exception { if(logger.isTraceEnabled()) { logger.trace("About to instrument query method: " + iMethod.getName() + ", into: " + profileTableConcreteClass); } CtMethod method = CtNewMethod.copy(iMethod, profileTableConcreteClass, null); method.setModifiers(method.getModifiers() & ~Modifier.ABSTRACT); String queryName = method.getName(); if(queryName.startsWith(_QUERY_METHOD_NAME_PREFIX)) { queryName= Introspector.decapitalize(queryName.replace(_QUERY_METHOD_NAME_PREFIX, "")); } else { throw new SLEEException("Method has wrong prefix, method name: " + queryName); } String body = "{ return " + _INTERCEPTOR_QUERY + "(this,\"" + queryName + "\",$args); }"; if(logger.isTraceEnabled()) { logger.trace("Instrument query method: " + method.getName() + ", into: " + profileTableConcreteClass + ", with body:\n" + body); } method.setBody(body); profileTableConcreteClass.addMethod(method); }
/** * Create a named usage parameter getter. * * @param profileConcreteClass * @throws SLEEException */ private void generateNamedUsageParameterGetter(CtClass profileConcreteClass) { String methodName = "getUsageParameterSet"; for (CtMethod ctMethod : profileConcreteClass.getMethods()) { if (ctMethod.getName().equals(methodName)) { try { // copy method, we can't just add body becase it is in super // class and does not sees profileObject field CtMethod ctMethodCopy = CtNewMethod.copy(ctMethod, profileConcreteClass, null); // create the method body String methodBody = "{ return ($r)" + ClassGeneratorUtils.MANAGEMENT_HANDLER + ".getUsageParameterSet(profileObject,$1); }"; if (logger.isTraceEnabled()) logger.trace("Implemented method " + methodName + " , body = " + methodBody); ctMethodCopy.setBody(methodBody); profileConcreteClass.addMethod(ctMethodCopy); } catch (CannotCompileException e) { throw new SLEEException(e.getMessage(), e); } } } }
System.out.println("-----> Found CtMethod " + (originalMain != null)); CtMethod wrapper = CtNewMethod.copy(originalMain, remoteTestRunner, null); originalMain.setName("originalMain");
private void createInvokeNextMethod(CtClass jp, boolean isVoid, AdviceSetups setups, JoinPointInfo info) throws NotFoundException, CannotCompileException { AdviceSetup[] aroundSetups = setups.getByType(AdviceType.AROUND); if (aroundSetups == null) return; CtMethod method = jp.getSuperclass().getSuperclass().getDeclaredMethod("invokeNext"); CtMethod invokeNext = CtNewMethod.copy(method, jp, null); String code = createInvokeNextMethodBody(jp, isVoid, aroundSetups, info); try { invokeNext.setBody(code); } catch (CannotCompileException e) { throw new RuntimeException("Error creating invokeNext method: " + code, e); } jp.addMethod(invokeNext); }
public boolean instrumentMethod(CtClass clazz, CtMethod method) throws InterceptorException { try { String mname = method.getName(); // rename old method to synthetic name, then duplicate the // method with original name for use as interceptor String nname = mname + oldMethodSufix; method.setName(nname); CtMethod mnew = CtNewMethod.copy(method, mname, clazz, null); // replace the body of the interceptor method with generated // code block and add it to class mnew.setBody(StringUtil.replaceAll(code, "$origMethodName", nname)); clazz.addMethod(mnew); } catch (Exception e) { throw new InterceptorException("Failed to replace method body " + method.getName(), e); } return true; }
CtField field = CtField.make(String.format(constantKind.getFieldTemplate(), fieldType, fieldName), clazz); CtMethod initializer = CtNewMethod.copy( originalMethod, String.format(INITIALIZER_NAME_TEMPLATE, methodName),
protected void transformMethod(MethodTransformation trans, boolean wrap) throws NotFoundException, CannotCompileException { String methodInfoField = addMethodInfoField(Modifier.PRIVATE | Modifier.STATIC, trans.getClazz(), trans); // generate Wrapper String wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(), trans.getMethod().getName()); CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null); String originalName = trans.getOriginalName(); wmethod.setName(wrappedName); trans.getClazz().addMethod(wmethod); moveAnnotationsAndCopySignature(trans.getMethod(), wmethod); trans.getMethod().setName(wrappedName); Instrumentor.addSyntheticAttribute(trans.getMethod()); wmethod.setName(originalName); trans.setWMethod(wmethod, wrappedName); // prepareForWrapping getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); if (wrap) { // wrap getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); // executeWrapping setWrapperBody(trans, methodInfoField); } }
protected void transformMethod(MethodTransformation trans, boolean wrap) throws CannotCompileException, NotFoundException { // generate Wrapper String wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(), trans.getMethod().getName()); CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null); String originalName = trans.getOriginalName(); wmethod.setName(wrappedName); trans.getClazz().addMethod(wmethod); moveAnnotationsAndCopySignature(trans.getMethod(), wmethod); trans.getMethod().setName(wrappedName); Instrumentor.addSyntheticAttribute(trans.getMethod()); wmethod.setName(originalName); trans.setWMethod(wmethod, wrappedName); String methodInfoField = addMethodInfoFieldToGenAdvisor(trans); addMethodToGeneratedAdvisor(trans, methodInfoField); // prepareForWrapping getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); if (wrap) { // wrap getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); // executeWrapping setWrapperBody(trans, methodInfoField); } }
protected void transformMethod(MethodTransformation trans, boolean wrap)throws CannotCompileException, NotFoundException { String methodInfoField = addMethodInfoField(Modifier.PRIVATE | Modifier.STATIC, trans.getClazz(), trans); String wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(), trans.getOriginalName()); CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null); // generate Wrapper String originalName = trans.getOriginalName(); wmethod.setName(wrappedName); trans.getClazz().addMethod(wmethod); moveAnnotationsAndCopySignature(trans.getMethod(), wmethod); String optimizedInvocation = OptimizedMethodInvocations.createOptimizedInvocationClass(trans.getInstrumentor(), trans.getClazz(), trans.getMethod(), wmethod); trans.getMethod().setName(wrappedName); Instrumentor.addSyntheticAttribute(trans.getMethod()); wmethod.setName(originalName); trans.setWMethod(wmethod, wrappedName); // prepareForWrapping getWrapper().prepareForWrapping(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); if (wrap) { // wrap getWrapper().wrap(wmethod, WrapperTransformer.SINGLE_TRANSFORMATION_INDEX); // executeWrapping setWrapperBody(trans, methodInfoField, optimizedInvocation); } }