Package javassist

Examples of javassist.CtMethod


      try {
         ct.removeField(ct.getField("number"));
         Class clazz = ct.toClass();
         if (isNewExternalizer) {
            CtClass extCt = pool.get(HOUSE_EXT);
            CtMethod writeObjMeth = extCt.getMethod("writeObject", "(Ljava/io/ObjectOutput;Ljava/lang/Object;)V");
            writeObjMeth.setBody("{\n" +
               "$1.writeInt(0);\n" + // Safe the spot to avoid incompatibility
               "$1.writeObject((("  + HOUSE + ") $2).street);\n" +
            "}\n"
            );
            CtMethod readObjMeth = extCt.getMethod("readObject", "(Ljava/io/ObjectInput;)Ljava/lang/Object;");
            readObjMeth.setBody("{\n" +
               HOUSE + " o = new " + HOUSE + "();\n" +
               "try {\n" +
               "   $1.readInt();\n" +
               "} catch(java.io.OptionalDataException e) {}\n" +
               "o.street = (String) $1.readObject();\n" +
View Full Code Here


        ctClass.setSuperclass(pool.get(BasicComponent.class.getName()));

        // Implement method getName()

        CtMethod method = CtNewMethod.make("public String getName() { return \"" + name + "\"; }", ctClass);
        ctClass.addMethod(method);

        ctClass.addInterface(pool.get(Named.class.getName()));

        ctClass.writeFile(extraClasspath.getAbsolutePath());
View Full Code Here

        CtClass ctReturnType = convertClass(ms.getReturnType());

        CtClass[] ctParameters = convertClasses(ms.getParameterTypes());
        CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes());

        CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, getCtClass());

        try
        {
            method.setModifiers(modifiers);
            method.setBody(body);
            method.setExceptionTypes(ctExceptions);

            getCtClass().addMethod(method);
        }
        catch (Exception ex)
        {
View Full Code Here

        builder.append(")");
        builder.append(ClassFabUtils.getTypeCode(method.getReturnType()));

        try
        {
            CtMethod ctMethod = ctClass.getMethod(method.getName(), builder.toString());

            int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);

            String sourceFile = ctMethod.getDeclaringClass().getClassFile2().getSourceFile();

            return new MethodLocation(method, sourceFile, lineNumber);
        }
        catch (Exception ex)
        {
View Full Code Here

        CtClass ctClass = pool.makeClass(className);

        ctClass.addInterface(pool.get(ReloadableService.class.getName()));

        CtMethod method = new CtMethod(pool.get("java.lang.String"), "getStatus", null, ctClass);

        method.setBody(String.format("return \"%s\";", status));

        ctClass.addMethod(method);

        ctClass.writeFile(classesDir.getAbsolutePath());
    }
View Full Code Here

        ctClass.setSuperclass(pool.get(BasicComponent.class.getName()));

        // Implement method getName()

        CtMethod method = CtNewMethod.make(
                "public String getName() { return \"" + name + "\"; }",
                ctClass);
        ctClass.addMethod(method);

        ctClass.addInterface(pool.get(Named.class.getName()));
View Full Code Here

        ctClass.setSuperclass(pool.get(BasicComponent.class.getName()));

        // Implement method getName()

        CtMethod method = CtNewMethod.make(
                "public String getName() { return \"" + name + "\"; }",
                ctClass);
        ctClass.addMethod(method);

        ctClass.addInterface(pool.get(Named.class.getName()));
View Full Code Here

      CtClass map = pool.get("java.util.Map");
      CtField methodMap = new CtField(map, "methodMap", proxy);
      methodMap.setModifiers(Modifier.PRIVATE | Modifier.STATIC);
      Instrumentor.addSyntheticAttribute(methodMap);
      proxy.addField(methodMap);
      CtMethod getMethodMap = CtNewMethod.getter("getMethodMap", methodMap);
      getMethodMap.setModifiers(Modifier.PUBLIC);
      Instrumentor.addSyntheticAttribute(getMethodMap);
      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 = new Long(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());
            }
         }
      }

      for (int i = 0; i < interfaces.length; i++)
      {
         if (addedInterfaces.contains(interfaces[i].getName())) continue;
         ClassPool mixPool = AspectManager.instance().findClassPool(interfaces[i]);
         CtClass intfClass = mixPool.get(interfaces[i].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 (addedMethods.contains(hash)) continue;
            addedMethods.add(hash);
            String aopReturnStr = (methods[m].getReturnType().equals(CtClass.voidType)) ? "" : "return ($r)";
            String args = "null";
            if (methods[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(methods[m].getReturnType(), methods[m].getName(), methods[m].getParameterTypes(), methods[m].getExceptionTypes(), code, proxy);
            newMethod.setModifiers(Modifier.PUBLIC);
            proxy.addMethod(newMethod);
         }
         proxy.addInterface(intfClass);
      }
      return proxy;
View Full Code Here

      code =
      "{ " +
      "" + returnStr + " " + mixinFieldName(mixinClass) + "." + method.getName() + "($$); " +
      "}";
      String wrappedName = ClassAdvisor.notAdvisedMethodName(clazz.getName(), method.getName());
      CtMethod nmethod = CtNewMethod.make(method.getReturnType(), wrappedName, method.getParameterTypes(),
                                        method.getExceptionTypes(), code, clazz);
      int modifier = method.getModifiers();
      if ((modifier & AccessFlag.ABSTRACT) == AccessFlag.ABSTRACT)
      {
         modifier &= ~AccessFlag.ABSTRACT;
      }
      if ((modifier & AccessFlag.BRIDGE) == AccessFlag.BRIDGE)
      {
         modifier &= ~AccessFlag.BRIDGE;
      }
      nmethod.setModifiers(modifier);
      addSyntheticAttribute(nmethod);
      clazz.addMethod(nmethod);
     
     
     
      //2 make the wrapped invocation call through to the helper method, using this as the target object
      code =
      "{ " +
      "   if (" + mixinFieldName(mixinClass) + " == null) { " +
      "      this." + mixinFieldName(mixinClass) + " = " + initializer + "; " +
      "   } " +
      "   org.jboss.aop.ClassInstanceAdvisor instAdv = (org.jboss.aop.ClassInstanceAdvisor)_getInstanceAdvisor();" +
      "   if (" + HELPER_FIELD_NAME + ".hasAspects() || (instAdv != null && instAdv.hasInstanceAspects)) " +
      "   { " +
      "      Object[] ags = " + args + "; " +
      "      " + aopReturnStr + HELPER_FIELD_NAME + ".invokeMethod(instAdv, this, " + hash + "L, ags); " +
      "   } " +
      "   else " +
      "   {" +
      "      " + returnStr + " " + mixinFieldName(mixinClass) + "." + method.getName() + "($$); " +
      "   }" +
      "}";
     
      CtMethod wmethod = CtNewMethod.make(method.getReturnType(), method.getName(), method.getParameterTypes(),
               method.getExceptionTypes(), code, clazz);
         wmethod.setModifiers(modifier);
      clazz.addMethod(wmethod);
     
      return wmethod;
  
   }
View Full Code Here

   private void addHelperClass(CtClass clazz)
           throws CannotCompileException, NotFoundException
   {
      addHelperField(clazz);
      // To make sure that correct Advisor gets called
      CtMethod getter = CtNewMethod.make("public org.jboss.aop.Advisor _getAdvisor()" +
                                         "{ " +
                                         "    return " + ClassicInstrumentor.HELPER_FIELD_NAME + ";" +
                                         "}",
                                         clazz);
      addSyntheticAttribute(getter);
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.