Package javassist.bytecode

Examples of javassist.bytecode.ConstPool


  }

  private int transformInvokevirtualsIntoPutfields(
      ClassFile classfile,
      CodeIterator iter, int pos) {
    ConstPool cp = classfile.getConstPool();
    int c = iter.byteAt(pos);
    if (c != Opcode.PUTFIELD) {
      return pos;
    }
    int index = iter.u16bitAt(pos + 1);
    String fieldName = cp.getFieldrefName(index);
    String className = cp.getFieldrefClassName(index);
    if ( !filter.handleWriteAccess( className, fieldName ) ) {
      return pos;
    }
    String desc = "(" + cp.getFieldrefType( index ) + ")V";
    int write_method_index = cp.addMethodrefInfo(
        cp.getThisClassInfo(),
        EACH_WRITE_METHOD_PREFIX + fieldName,
        desc
    );
    iter.writeByte(Opcode.INVOKEVIRTUAL, pos);
    iter.write16bit(write_method_index, pos + 1);
View Full Code Here


    return pos;
  }

  private static void addInvokeFieldHandlerMethod(ClassFile classfile,
                                                  Bytecode code, String typeName, boolean isReadMethod) {
    ConstPool cp = classfile.getConstPool();
    // invokeinterface
    int callback_type_index = cp.addClassInfo(FIELD_HANDLER_TYPE_NAME);
    if ((typeName.charAt(0) == 'L')
        && (typeName.charAt(typeName.length() - 1) == ';')
        || (typeName.charAt(0) == '[')) {
      // reference type
      int indexOfL = typeName.indexOf('L');
View Full Code Here

    addGetter( targetClass, theField, getterName );
    addSetter( targetClass, theField, setterName );
  }

  private CtField addField(CtClass targetClass, CtClass fieldType, String fieldName, boolean makeTransient) {
    final ConstPool constPool = targetClass.getClassFile().getConstPool();

    final CtField theField;
    try {
      theField = new CtField( fieldType, fieldName, targetClass );
      targetClass.addField( theField );
View Full Code Here

  private void transformFieldAccessesIntoReadsAndWrites(
      CtClass managedCtClass,
      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 );
View Full Code Here

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

   }

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

   }

   public static void addSyntheticAttribute(CtField field)
   {
      FieldInfo info = field.getFieldInfo();
      ConstPool cp = info.getConstPool();
      info.addAttribute(new SyntheticAttribute(cp));
   }
View Full Code Here

      file.setMajorVersion(48);

      // Rename known classes
      file.renameClass(getClassRenames());

      ConstPool constPool = file.getConstPool();

      // Rename classes with + to have $
      HashMap<String, String> mapClasses = new HashMap<String, String>();
     
      for (String name : (Set<String>) constPool.getClassNames())
      {
         if (name.indexOf('+') != -1) {
            mapClasses.put(name, name.replace('+', '$'));
         }
      }
      if (mapClasses.size() != 0) {
         constPool.renameClass(mapClasses);
      }

      // Replace LDC/LDC_W
      for (MethodInfo method : (List<MethodInfo>) file.getMethods()) {
         rewriteLDC(constPool, method);
View Full Code Here

         }
      }

      // Now we've checked the signatures of the class, let's look at the references
      file.compact();
      ConstPool consts = file.getConstPool();
      for (int i = 1; i < consts.getSize(); ++i) // Yes start at 1
      {
         switch (consts.getTag(i))
         {
            case ConstPool.CONST_Fieldref:
            {
               String type = consts.getFieldrefClassName(i);
               String name = consts.getFieldrefName(i);
               try
               {
                  CtClass ctClazz = pool.get(type);
                  ctClazz.getField(name);
               }
               catch (NotFoundException e)
               {
                  if (failed == 0)
                     System.out.println("==== " + info.getFile());
                  System.out.println("Cannot find field " + type + "." + name);
                  ++failed;
               }
               break;
            }
            case ConstPool.CONST_InterfaceMethodref:
            {
               String type = consts.getInterfaceMethodrefClassName(i);
               String name = consts.getInterfaceMethodrefName(i);
               String descriptor = consts.getInterfaceMethodrefType(i);
               try
               {
                  CtClass ctClazz = pool.get(type);
                  ctClazz.getMethod(name, descriptor);
               }
               catch (NotFoundException e)
               {
                  if (failed == 0)
                     System.out.println("==== " + info.getFile());
                  System.out.println("Cannot find interface method " + type + "." + name + descriptor);
                  ++failed;
               }
               break;
            }
            case ConstPool.CONST_Methodref:
            {
               String type = consts.getMethodrefClassName(i);
               String name = consts.getMethodrefName(i);
               String descriptor = consts.getMethodrefType(i);
               if (INIT.equals(name))
               {
                  try
                  {
                     CtClass ctClazz = pool.get(type);
View Full Code Here

   *
   * @param classfile
   * @throws CannotCompileException
   */
  private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
    ConstPool cp = classfile.getConstPool();
    String cons_desc = "()V";
    MethodInfo mi = new MethodInfo( cp, MethodInfo.nameInit, cons_desc );

    Bytecode code = new Bytecode( cp, 0, 1 );
    // aload_0
View Full Code Here

TOP

Related Classes of javassist.bytecode.ConstPool

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.