Package javassist.bytecode

Examples of javassist.bytecode.MethodInfo


  private void transformInvokevirtualsIntoPutAndGetfields(ClassFile classfile)
      throws CannotCompileException {
    List methods = classfile.getMethods();
    for (Iterator method_iter = methods.iterator(); method_iter.hasNext();) {
      MethodInfo minfo = (MethodInfo) method_iter.next();
      String methodName = minfo.getName();
      if (methodName.startsWith(EACH_READ_METHOD_PREFIX)
          || methodName.startsWith(EACH_WRITE_METHOD_PREFIX)
          || methodName.equals(GETFIELDHANDLER_METHOD_NAME)
          || methodName.equals(SETFIELDHANDLER_METHOD_NAME)) {
        continue;
      }
      CodeAttribute codeAttr = minfo.getCodeAttribute();
      if (codeAttr == null) {
        return;
      }
      CodeIterator iter = codeAttr.iterator();
      while (iter.hasNext()) {
View Full Code Here


            catch (NotFoundException e)
            {
               throw new RuntimeException("Unable to find method " + methods[i].getName() + " for " + annotation.getName());
            }
            Object defaultValue = null;
            MethodInfo minfo = method.getMethodInfo2();
            AnnotationDefaultAttribute defAttr = (AnnotationDefaultAttribute)minfo.getAttribute(AnnotationDefaultAttribute.tag);
           
            if (defAttr != null)
            {
               MemberValue value = defAttr.getDefaultValue();    // default value of age
               MemberValueGetter getter = new MemberValueGetter(methods[i]);
View Full Code Here

              EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME + "(" +
              EnhancerConstants.TRACKER_FIELD_NAME + ");" +
              "return " + EnhancerConstants.TRACKER_FIELD_NAME + "; }";
      CtMethod getMethod = CtNewMethod.make( trackerGetMethod, managedCtClass );

      MethodInfo methodInfo = getMethod.getMethodInfo();
      SignatureAttribute signatureAttribute =
          new SignatureAttribute( methodInfo.getConstPool(), "()Ljava/util/Set<Ljava/lang/String;>;" );
      methodInfo.addAttribute( signatureAttribute );
      managedCtClass.addMethod( getMethod );

    }
    catch (CannotCompileException e) {
      e.printStackTrace();
View Full Code Here

      IdentityHashMap<String, PersistentAttributeDescriptor> attributeDescriptorMap) {

    final ConstPool constPool = managedCtClass.getClassFile().getConstPool();

    for ( Object oMethod : managedCtClass.getClassFile().getMethods() ) {
      final MethodInfo methodInfo = (MethodInfo) oMethod;
      final String methodName = methodInfo.getName();

      // skip methods added by enhancement
      if ( methodName.startsWith( EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX )
          || methodName.startsWith( EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX )
          || methodName.equals( EnhancerConstants.ENTITY_INSTANCE_GETTER_NAME )
          || methodName.equals( EnhancerConstants.ENTITY_ENTRY_GETTER_NAME )
          || methodName.equals( EnhancerConstants.ENTITY_ENTRY_SETTER_NAME )
          || methodName.equals( EnhancerConstants.PREVIOUS_GETTER_NAME )
          || methodName.equals( EnhancerConstants.PREVIOUS_SETTER_NAME )
          || methodName.equals( EnhancerConstants.NEXT_GETTER_NAME )
          || methodName.equals( EnhancerConstants.NEXT_SETTER_NAME ) ) {
        continue;
      }

      final CodeAttribute codeAttr = methodInfo.getCodeAttribute();
      if ( codeAttr == null ) {
        // would indicate an abstract method, continue to next method
        continue;
      }

      try {
        final CodeIterator itr = codeAttr.iterator();
        while ( itr.hasNext() ) {
          final int index = itr.next();
          final int op = itr.byteAt( index );
          if ( op != Opcode.PUTFIELD && op != Opcode.GETFIELD ) {
            continue;
          }

          final int constIndex = itr.u16bitAt( index + 1 );

          final String fieldName = constPool.getFieldrefName( constIndex );
          final PersistentAttributeDescriptor attributeDescriptor = attributeDescriptorMap.get( fieldName );

          if ( attributeDescriptor == null ) {
            // its not a field we have enhanced for interception, so skip it
            continue;
          }

          log.tracef(
              "Transforming access to field [%s] from method [%s]",
              fieldName,
              methodName
          );

          if ( op == Opcode.GETFIELD ) {
            final int readMethodIndex = constPool.addMethodrefInfo(
                constPool.getThisClassInfo(),
                attributeDescriptor.getReader().getName(),
                attributeDescriptor.getReader().getSignature()
            );
            itr.writeByte( Opcode.INVOKESPECIAL, index );
            itr.write16bit( readMethodIndex, index + 1 );
          }
          else {
            final int writeMethodIndex = constPool.addMethodrefInfo(
                constPool.getThisClassInfo(),
                attributeDescriptor.getWriter().getName(),
                attributeDescriptor.getWriter().getSignature()
            );
            itr.writeByte( Opcode.INVOKESPECIAL, index );
            itr.write16bit( writeMethodIndex, index + 1 );
          }
        }

        final StackMapTable smt = MapMaker.make( classPool, methodInfo );
        methodInfo.getCodeAttribute().setAttribute( smt );
      }
      catch (BadBytecode e) {
        throw new EnhancementException(
            "Unable to perform field access transformation in method : " + methodName,
            e
View Full Code Here

    for (int j = 0; j < ms.length; ++j) {
      CtMethod m = ms[j];
      int modifiers = m.getModifiers();
      if (!Modifier.isStatic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isNative(modifiers)) {
        if (!m.isEmpty()) {
          MethodInfo info = m.getMethodInfo();
          Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
          bc.addAload(0);
          bc.addAload(0);
          bc.addGetfield(cc, "_counter", "I");
          bc.add(Bytecode.ICONST_1);
          bc.add(Bytecode.IADD);
          bc.addPutfield(cc, "_counter", "I");
          CodeIterator iter = info.getCodeAttribute().iterator();
          iter.begin();
          iter.insert(bc.get());
        }
      }
    }
View Full Code Here

         for (int i = 0; i < methods.length; i++)
         {
            if (introduction.matches(advisor, methods[i]))
            {
               javassist.bytecode.annotation.Annotation info = AnnotationInfoCreator.createAnnotationInfo(classPool, methods[i].getMethodInfo2().getConstPool(), introduction.getAnnotation());
               MethodInfo mi = methods[i].getMethodInfo2();
               if (introduction.isInvisible())
               {
                  AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
                  if (invisible == null)
                  {
                     invisible = new AnnotationsAttribute(mi.getConstPool(), AnnotationsAttribute.invisibleTag);
                     mi.addAttribute(invisible);
                  }
                  changed = true;
                  invisible.addAnnotation(info);
               }
               else
               {
                  AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
                  if (visible == null)
                  {
                     visible = new AnnotationsAttribute(mi.getConstPool(), AnnotationsAttribute.visibleTag);
                     mi.addAttribute(visible);
                  }
                  changed = true;
                  visible.addAnnotation(info);
               }
            }

         }

         CtConstructor[] cons = clazz.getDeclaredConstructors();
         for (int i = 0; i < cons.length; i++)
         {
            if (introduction.matches(advisor, cons[i]))
            {
               javassist.bytecode.annotation.Annotation info = AnnotationInfoCreator.createAnnotationInfo(classPool, cons[i].getMethodInfo2().getConstPool(), introduction.getAnnotation());
               MethodInfo mi = cons[i].getMethodInfo2();
               if (introduction.isInvisible())
               {
                  AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
                  if (invisible == null)
                  {
                     invisible = new AnnotationsAttribute(mi.getConstPool(), AnnotationsAttribute.invisibleTag);
                     mi.addAttribute(invisible);
                  }
                  changed = true;
                  invisible.addAnnotation(info);
               }
               else
               {
                  AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
                  if (visible == null)
                  {
                     visible = new AnnotationsAttribute(mi.getConstPool(), AnnotationsAttribute.visibleTag);
                     mi.addAttribute(visible);
                  }
                  changed = true;
                  visible.addAnnotation(info);
               }
            }
         }

         CtField[] fields = clazz.getDeclaredFields();
         for (int i = 0; i < fields.length; i++)
         {
            if (introduction.matches(advisor, fields[i]))
            {
               javassist.bytecode.annotation.Annotation info = AnnotationInfoCreator.createAnnotationInfo(classPool, fields[i].getFieldInfo2().getConstPool(), introduction.getAnnotation());
               FieldInfo mi = fields[i].getFieldInfo2();
               if (introduction.isInvisible())
               {
                  AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
                  if (invisible == null)
                  {
                     invisible = new AnnotationsAttribute(mi.getConstPool(), AnnotationsAttribute.invisibleTag);
                     mi.addAttribute(invisible);
                  }
                  changed = true;
                  invisible.addAnnotation(info);
               }
               else
               {
                  AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
                  if (visible == null)
                  {
                     visible = new AnnotationsAttribute(mi.getConstPool(), AnnotationsAttribute.visibleTag);
                     mi.addAttribute(visible);
                  }
                  changed = true;
                  visible.addAnnotation(info);
               }
            }
View Full Code Here

   }
  
   private static SignatureAttribute.MethodSignature getMethodSignature(Method m)
   {
      CtMethod mtd = JavassistUtil.getCtMethod(m);
      MethodInfo info = mtd.getMethodInfo2();
      return getMethodSignature(info);
   }
View Full Code Here

   }
  
   private static SignatureAttribute.MethodSignature getMethodSignature(Constructor c)
   {
      CtConstructor con = JavassistUtil.getCtConstructor(c);
      MethodInfo info = con.getMethodInfo2();
      return getMethodSignature(info);
   }
View Full Code Here

      return shouldReplaceArrayAccess;
   }

   public static void addSyntheticAttribute(CtMethod method)
   {
      MethodInfo info = method.getMethodInfo();
      addSyntheticAttribute(info);
   }
View Full Code Here

      info.addAttribute(new SyntheticAttribute(cp));
   }

   public static void addSyntheticAttribute(CtConstructor ctor)
   {
      MethodInfo info = ctor.getMethodInfo();
      ConstPool cp = info.getConstPool();
      info.addAttribute(new SyntheticAttribute(cp));
   }
View Full Code Here

TOP

Related Classes of javassist.bytecode.MethodInfo

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.