Package javassist

Examples of javassist.CtMethod


      proxy.addField(mixinField);
      instanceAdvisor = new CtField(instanceAdvisor.getType(), "instanceAdvisor", proxy);
      instanceAdvisor.setModifiers(Modifier.PRIVATE);
      proxy.addField(instanceAdvisor);

      CtMethod writeEx = CtNewMethod.make("   public void writeExternal(java.io.ObjectOutput out)\n" +
                                          "   throws java.io.IOException\n" +
                                          "   {\n" +
                                          "   }", proxy);
      Instrumentor.addSyntheticAttribute(writeEx);
      CtMethod readEx = CtNewMethod.make("   public void readExternal(java.io.ObjectInput in)\n" +
                                         "   throws java.io.IOException, ClassNotFoundException\n" +
                                         "   {\n" +
                                         "   }", proxy);
      Instrumentor.addSyntheticAttribute(readEx);
      CtMethod getInstanceAdvisor = CtNewMethod.make("   public org.jboss.aop.InstanceAdvisor _getInstanceAdvisor()\n" +
                                                     "   {\n" +
                                                     "      return instanceAdvisor;\n" +
                                                     "   }", proxy);
      Instrumentor.addSyntheticAttribute(getInstanceAdvisor);
      CtMethod setInstanceAdvisor = CtNewMethod.make("   public void _setInstanceAdvisor(org.jboss.aop.InstanceAdvisor newAdvisor)\n" +
                                                     "   {\n" +
                                                     "      instanceAdvisor = (org.jboss.aop.ClassInstanceAdvisor) newAdvisor;\n" +
                                                     "   }", proxy);
      Instrumentor.addSyntheticAttribute(setInstanceAdvisor);
      CtMethod dynamicInvoke = CtNewMethod.make("   public org.jboss.aop.joinpoint.InvocationResponse _dynamicInvoke(org.jboss.aop.joinpoint.Invocation invocation)\n" +
                                                "   throws Throwable\n" +
                                                "   {\n" +
                                                "      ((org.jboss.aop.joinpoint.InvocationBase) invocation).setInstanceResolver(instanceAdvisor.getMetaData());\n" +
                                                "      org.jboss.aop.advice.Interceptor[] aspects = instanceAdvisor.getInterceptors();\n" +
                                                "      return new org.jboss.aop.joinpoint.InvocationResponse(invocation.invokeNext(aspects));\n" +
                                                "   }", proxy);
      Instrumentor.addSyntheticAttribute(dynamicInvoke);
      CtMethod setMixins = CtNewMethod.make("   public void setMixins(org.jboss.aop.proxy.ProxyMixin[] mixins)\n" +
                                            "   {\n" +
                                            "      this.mixins = mixins;\n" +
                                            "   }", proxy);
      Instrumentor.addSyntheticAttribute(setMixins);
      CtMethod writeReplace = CtNewMethod.make("   public Object writeReplace() throws java.io.ObjectStreamException\n" +
                                               "   {\n" +
                                               "      return new org.jboss.aop.proxy.MarshalledClassProxy(this.getClass().getSuperclass(), mixins, instanceAdvisor);\n" +
                                               "   }", proxy);
      Instrumentor.addSyntheticAttribute(writeReplace);

      proxy.addMethod(writeEx);
      proxy.addMethod(readEx);
      proxy.addMethod(getInstanceAdvisor);
      proxy.addMethod(setInstanceAdvisor);
      proxy.addMethod(dynamicInvoke);
      proxy.addMethod(setMixins);
      if (!interceptWriteReplace)
         proxy.addMethod(writeReplace);

      /*
      CtMethod writeEx = template.getDeclaredMethod("writeExternal");
      CtMethod readEx = template.getDeclaredMethod("readExternal");
      CtMethod getInstanceAdvisor = template.getDeclaredMethod("_getInstanceAdvisor");
      CtMethod setInstanceAdvisor = template.getDeclaredMethod("_setInstanceAdvisor");
      CtMethod dynamicInvoke = template.getDeclaredMethod("_dynamicInvoke");
      CtMethod setMixins = template.getDeclaredMethod("setMixins");
      CtMethod writeReplace = template.getDeclaredMethod("writeReplace");




      proxy.addMethod(CtNewMethod.copy(writeEx, proxy, null));
      proxy.addMethod(CtNewMethod.copy(readEx, proxy, null));
      proxy.addMethod(CtNewMethod.copy(getInstanceAdvisor, proxy, null));
      proxy.addMethod(CtNewMethod.copy(setInstanceAdvisor, proxy, null));
      proxy.addMethod(CtNewMethod.copy(dynamicInvoke, proxy, null));
      proxy.addMethod(CtNewMethod.copy(setMixins, proxy, null));
      proxy.addMethod(CtNewMethod.copy(writeReplace, proxy, null));
      */


      proxy.addInterface(pool.get("org.jboss.aop.proxy.ClassProxy"));
      proxy.addInterface(pool.get("java.io.Externalizable"));
      proxy.addInterface(pool.get("org.jboss.aop.instrument.Untransformable"));
      proxy.addInterface(pool.get("org.jboss.aop.proxy.MethodMapped"));

      CtClass map = pool.get("java.util.Map");
      CtField methodMap = new CtField(map, "methodMap", proxy);
      methodMap.setModifiers(Modifier.PRIVATE | Modifier.STATIC);
      proxy.addField(methodMap);
      CtMethod getMethodMap = CtNewMethod.getter("getMethodMap", methodMap);
      getMethodMap.setModifiers(Modifier.PUBLIC);
      proxy.addMethod(getMethodMap);

      HashSet<String> addedInterfaces = new HashSet<String>();
      HashSet<Long> addedMethods = new HashSet<Long>();
      if (mixins != null)
      {
         for (int i = 0; i < mixins.length; i++)
         {
            HashSet<Long> mixinMethods = new HashSet<Long>();
            Class<?>[] mixinf = mixins[i].getInterfaces();
            ClassPool mixPool = AspectManager.instance().findClassPool(mixins[i].getMixin().getClass());
            CtClass mixClass = mixPool.get(mixins[i].getMixin().getClass().getName());
            for (int j = 0; j < mixinf.length; j++)
            {
               if (addedInterfaces.contains(mixinf[j].getName())) throw new Exception("2 mixins are implementing the same interfaces");
               ClassPool mixIntfPool = AspectManager.instance().findClassPool(mixinf[j]);
               CtClass intfClass = mixIntfPool.get(mixinf[j].getName());
               CtMethod[] methods = intfClass.getMethods();
               for (int m = 0; m < methods.length; m++)
               {
                  if (methods[m].getDeclaringClass().getName().equals("java.lang.Object")) continue;
                  Long hash = Long.valueOf(JavassistMethodHashing.methodHash(methods[m]));
                  if (mixinMethods.contains(hash)) continue;
                  if (addedMethods.contains(hash)) throw new Exception("More than one mixin has same method");
                  mixinMethods.add(hash);
                  addedMethods.add(hash);
                  String returnStr = (methods[m].getReturnType().equals(CtClass.voidType)) ? "" : "return ";
                  String code = "{" +
                  "   " + mixClass.getName() + " mixin = (" + mixClass.getName() + ")mixins[" + i + "].getMixin();" +
                  "   " + returnStr + " mixin." + methods[m].getName() + "($$);" +
                  "}";
                  CtMethod newMethod = CtNewMethod.make(methods[m].getReturnType(), methods[m].getName(), methods[m].getParameterTypes(), methods[m].getExceptionTypes(), code, proxy);
                  newMethod.setModifiers(Modifier.PUBLIC);
                  proxy.addMethod(newMethod);
               }

               proxy.addInterface(intfClass);
               addedInterfaces.add(intfClass.getName());
            }
         }
      }

      HashMap<Long, CtMethod> allMethods = JavassistMethodHashing.getMethodMap(superclass);
     
      if (interceptWriteReplace)
         allMethods.put(JavassistMethodHashing.methodHash(writeReplace), writeReplace);
    
      for (Map.Entry<Long, CtMethod> entry : allMethods.entrySet())
      {
         CtMethod m = entry.getValue();
         if (!Modifier.isPublic(m.getModifiers()) || Modifier.isStatic(m.getModifiers())) continue;

         Long hash = entry.getKey();
         if (addedMethods.contains(hash)) continue;
         addedMethods.add(hash);
         String aopReturnStr = (m.getReturnType().equals(CtClass.voidType)) ? "" : "return ($r)";
         String args = "null";
         if (m.getParameterTypes().length > 0) args = "$args";
         String code = "{   " +
         "    org.jboss.aop.advice.Interceptor[] aspects = instanceAdvisor.getInterceptors(); " +
         "    org.jboss.aop.MethodInfo mi = new org.jboss.aop.MethodInfo(); " +
         "    mi.setHash(" + hash.longValue() + "L);" +
         "    org.jboss.aop.proxy.ProxyMethodInvocation invocation = new org.jboss.aop.proxy.ProxyMethodInvocation(this, mi, aspects); " +
         "    invocation.setInstanceResolver(instanceAdvisor.getMetaData()); " +
         "    invocation.setArguments(" + args + "); " +
         "    " + aopReturnStr + " invocation.invokeNext(); " +
         "}";
         CtMethod newMethod = CtNewMethod.make(m.getReturnType(), m.getName(), m.getParameterTypes(), m.getExceptionTypes(), code, proxy);
         newMethod.setModifiers(Modifier.PUBLIC);
         proxy.addMethod(newMethod);
      }
      SerialVersionUID.setSerialVersionUID(proxy);
      return proxy;
   }
View Full Code Here


                "_instanceAdvisor",
                "org.jboss.aop.ClassInstanceAdvisor", null
                //CtField.Initializer.byExpr("new org.jboss.aop.ClassInstanceAdvisor(this)")
         );

         CtMethod getter = CtNewMethod.make("public org.jboss.aop.InstanceAdvisor _getInstanceAdvisor()" +
                                 "{ " +
                                 "    synchronized(this) {" +
                                 "       if (_instanceAdvisor == null) { _instanceAdvisor = new org.jboss.aop.ClassInstanceAdvisor(this); }" +
                                 "       return _instanceAdvisor;" +
                                 "    } " +
                                 "}",
                                 clazz);
         addSyntheticAttribute(getter);
         clazz.addMethod(getter);

         CtMethod setter = CtNewMethod.make("public void _setInstanceAdvisor(org.jboss.aop.InstanceAdvisor newAdvisor)" +
                                 "{ " +
                                 "    synchronized(this) {" +
                                 "       _instanceAdvisor = (org.jboss.aop.ClassInstanceAdvisor)newAdvisor;" +
                                 "    } " +
                                 "}",
View Full Code Here

      String originalBody =
         "{" +
         "   " + getReturnStr(mixinMethod) + " " + Instrumentor.mixinFieldName(mixinClass) + "." + mixinMethod.getName() + "($$);" +
         "}";

      CtMethod original = CtNewMethod.make(
            Modifier.PUBLIC,
            mixinMethod.getReturnType(),
            mixinMethod.getName(),
            mixinMethod.getParameterTypes(),
            mixinMethod.getExceptionTypes(),
            originalBody,
            clazz);
      clazz.addMethod(original);
      long hash = JavassistMethodHashing.methodHash(original);
      moveAnnotationsAndCopySignature(mixinMethod, original);

      String wrappedName = ClassAdvisor.notAdvisedMethodName(clazz.getName(), originalName);
      CtMethod wmethod = CtNewMethod.copy(original, clazz, null);
     
      //drop bridge flag if present
      int modifier = wmethod.getModifiers();
      modifier &= ~AccessFlag.BRIDGE;
      wmethod.setModifiers(modifier);

      wmethod.setName(wrappedName);
      clazz.addMethod(wmethod);
      moveAnnotationsAndCopySignature(original, wmethod);

      original.setName(wrappedName);
      Instrumentor.addSyntheticAttribute(original);
      wmethod.setName(originalName);

      MethodTransformation trans = new MethodTransformation(instrumentor, clazz, original, originalName, wmethod, wrappedName, hash);

      String methodInfoField = addMethodInfoFieldToGenAdvisor(trans);
      addMethodToGeneratedAdvisor(trans, methodInfoField);

      String wrapperBody =
         "{" +
         "   if (" + Instrumentor.mixinFieldName(mixinClass) + " == null)" +
         "   {" +
         "      " + Instrumentor.mixinFieldName(mixinClass) + " = " + initializer + ";" +
         "   }" +
         "   " + getReturnStr(trans.getMethod()) + " ((" + GeneratedAdvisorInstrumentor.getAdvisorFQN(trans.getClazz()) + ")" + GeneratedAdvisorInstrumentor.GET_CURRENT_ADVISOR + ")." + getAdvisorMethodName(trans) + "(this,$$);" +
         "}";
      wmethod.setBody(wrapperBody);
      return wmethod;
   }
View Full Code Here

            originalBody += "return null;";
         }
      }
      originalBody += "}";

      CtMethod original = CtNewMethod.make(
            Modifier.PUBLIC,
            mixinMethod.getReturnType(),
            mixinMethod.getName(),
            mixinMethod.getParameterTypes(),
            mixinMethod.getExceptionTypes(),
            originalBody,
            clazz);
      clazz.addMethod(original);
      long hash = JavassistMethodHashing.methodHash(original);
      moveAnnotationsAndCopySignature(mixinMethod, original);

      String wrappedName = ClassAdvisor.notAdvisedMethodName(clazz.getName(), originalName);
      CtMethod wmethod = CtNewMethod.copy(original, clazz, null);
     
      //drop bridge flag if present
      int modifier = wmethod.getModifiers();
      modifier &= ~AccessFlag.BRIDGE;
      wmethod.setModifiers(modifier);

      wmethod.setName(wrappedName);
      clazz.addMethod(wmethod);
      moveAnnotationsAndCopySignature(original, wmethod);

      original.setName(wrappedName);
      Instrumentor.addSyntheticAttribute(original);
      wmethod.setName(originalName);

      MethodTransformation trans = new MethodTransformation(instrumentor, clazz, original, originalName, wmethod, wrappedName, hash);

      String methodInfoField = addMethodInfoFieldToGenAdvisor(trans);
      //delegate = genadvisor.getSuperclass().getSuperclass().getDeclaredMethod("invokeMethod");
      String body = "{ return ((org.jboss.aop.ClassAdvisor)this._getAdvisor()).invokeMethod($1, ";
      body += trans.getHash() + "L";
      for (int i = 0; i < mixinMethod.getParameterTypes().length; i++)
      {
         body +=  "$" + (i + 2);
      }
      body += ");}";
      addMethodToGeneratedAdvisor(trans, methodInfoField, body);
//      CtMethod newMethod = CtNewMethod.wrapped(trans.getWMethod().getReturnType(),
//            getAdvisorMethodName(trans),
//            addTargetToParamsForNonStaticMethod(trans.getClazz(), trans.getWMethod()),
//            trans.getWMethod().getExceptionTypes(),
//            delegate,
//            CtMethod.ConstParameter.integer(hash),
//            clazz);
//      genadvisor.addMethod(newMethod);
//      newMethod.setModifiers(Modifier.setProtected(newMethod.getModifiers()));
//     
      String wrapperBody =
         "{" +
         "   " + getReturnStr(trans.getMethod()) + " ((" + GeneratedAdvisorInstrumentor.getAdvisorFQN(trans.getClazz()) + ")" + GeneratedAdvisorInstrumentor.GET_CURRENT_ADVISOR + ")." + getAdvisorMethodName(trans) + "(this,$$);" +
         "}";
      wmethod.setBody(wrapperBody);
      return wmethod;
   }
View Full Code Here

         throws CannotCompileException, NotFoundException
   {
      // generate Wrapper
      String wrappedName = ClassAdvisor.notAdvisedMethodName(trans.getClazzName(),
                                                             trans.getMethod().getName());
      CtMethod wmethod = CtNewMethod.copy(trans.getMethod(), trans.getClazz(), null);
     
      //drop bridge flag if present
      int modifier = wmethod.getModifiers();
      modifier &= ~AccessFlag.BRIDGE;
      wmethod.setModifiers(modifier);

      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);
View Full Code Here

      CtClass[] params = addTargetToParamsForNonStaticMethod(trans.getClazz(), trans.getWMethod());

      String code = createAdvisorMethodBody(trans);
      try
      {
         CtMethod advisorMethod = CtNewMethod.make(
               Modifier.PROTECTED,
               trans.getWMethod().getReturnType(),
               getAdvisorMethodName(trans),
               params,
               trans.getWMethod().getExceptionTypes(),
               code,
               genadvisor);

         genadvisor.addMethod(advisorMethod);
         advisorMethod.setModifiers(Modifier.setProtected(advisorMethod.getModifiers()));
      }
      catch (CannotCompileException e)
      {
         throw new RuntimeException("code was: " + code + " for method " + getAdvisorMethodName(trans), e);
      }
View Full Code Here

   {
      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);
     
      //drop bridge flag if present
      int modifier = wmethod.getModifiers();
      modifier &= ~AccessFlag.BRIDGE;
      wmethod.setModifiers(modifier);

      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);
View Full Code Here

   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);
     
      //drop bridge flag if present
      int modifier = wmethod.getModifiers();
      modifier &= ~AccessFlag.BRIDGE;
      wmethod.setModifiers(modifier);

      // 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);
View Full Code Here

      return field;
   }
  
   private CtMethod addMethodFromTemplate(CtClass template, String name, String body) throws Exception
   {
      CtMethod templateMethod = template.getDeclaredMethod(name);
      CtMethod method = CtNewMethod.make(templateMethod.getReturnType(), name, templateMethod.getParameterTypes(), templateMethod.getExceptionTypes(), body, proxy);
      method.setModifiers(templateMethod.getModifiers());
      Instrumentor.addSyntheticAttribute(method);
      proxy.addMethod(method);
      hardcodedMethods.add(JavassistMethodHashing.methodHash(method));
      return method;
   }
View Full Code Here

                             "       " + returnStr + " mixin." + methods[m].getName() + "($$);" +
                             "       } " +
                             "    }finally{" +
                             "    }" +
                             "}";
               CtMethod newMethod = CtNewMethod.make(methods[m].getReturnType(), methods[m].getName(), methods[m].getParameterTypes(), methods[m].getExceptionTypes(), code, proxy);
               newMethod.setModifiers(Modifier.PUBLIC);
               proxy.addMethod(newMethod);

               copySignature(methods[m], newMethod);
            }
View Full Code Here

TOP

Related Classes of javassist.CtMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.