Package org.objectweb.asm

Examples of org.objectweb.asm.ClassWriter


  })
  @Ensures("result != null")
  protected byte[] instrumentWithContracts(byte[] bytecode,
                                           ContractAnalyzer contracts) {
    ClassReader reader = new ClassReader(bytecode);
    ClassWriter writer =
        new NonLoadingClassWriter(reader,
                                  ClassWriter.COMPUTE_FRAMES |
                                  ClassWriter.COMPUTE_MAXS);

    SpecificationClassAdapter adapter =
        new SpecificationClassAdapter(writer, contracts);
    reader.accept(adapter, ClassReader.EXPAND_FRAMES);

    return writer.toByteArray();
  }
View Full Code Here


   */
  @Requires("bytecode != null")
  @Ensures("result != null")
  private byte[] instrumentWithDebug(byte[] bytecode) {
    ClassReader reader = new ClassReader(bytecode);
    ClassWriter writer = new NonLoadingClassWriter(reader, 0);
    reader.accept(new HelperClassAdapter(writer), ClassReader.EXPAND_FRAMES);
    return writer.toByteArray();
  }
View Full Code Here

  protected boolean weave(InputStream sourceStream, String fileName, ByteArrayOutputStream bos)
      throws IOException {

        ClassReader cr = new ClassReader(sourceStream);
        ClassWriter cw = new ClassWriter(true);

        try {
          NameTranslatorClassVisitor translator;
          ConstantPool cp;
            if (COMPACT_CONSTANTS) {
                cp = new ConstantPool();
                ClassConstantsCollector ccc = new ClassConstantsCollector(cw, cp);
                translator = new NameTranslatorClassVisitor(ccc);
            } else {
              translator = new NameTranslatorClassVisitor(cw);
            }
            ClassWeaver classWeaver = new ClassWeaver(translator, lazy, target, listener);
            SignatureStripper sigStripper = new SignatureStripper(classWeaver);
            cr.accept(sigStripper, false);       

            if (COMPACT_CONSTANTS) {
              Set<Constant> constants = new TreeSet<Constant>(new ConstantComparator());
              constants.addAll(cp.values());

              cr = new ClassReader(cw.toByteArray());
                cw = new ClassWriter(false);
                for(Constant c: constants)
                  c.write(cw);
                cr.accept(cw, false);
            }

          bos.write(cw.toByteArray());
          return true;
        } catch (LazyException e) {
          return false;
        }
   }
View Full Code Here

  protected boolean weave(InputStream sourceStream, String fileName, ByteArrayOutputStream bos)
      throws IOException {

        ClassReader cr = new ClassReader(sourceStream);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

        try {
          // chain class visitors
          ClassVisitor classVisitor = cw;
          ConstantPool cp;
            if (COMPACT_CONSTANTS) {
                cp = new ConstantPool();
                classVisitor = new ClassConstantsCollector(classVisitor, cp);
            }
            classVisitor = new NameTranslatorClassVisitor(classVisitor, NameTranslator.getGeneralTranslator());
          classVisitor = new ClassWeaver(classVisitor,
                        lazy, stripAttributes, target, listener);
         
          // StringBuilder translation will be done before general weaving and
          // mirror translation: trimToSize() calls will be processed correctly
          // and no need to do translations in general weaving process
            classVisitor = new NameTranslatorClassVisitor(classVisitor, NameTranslator.getStringBuilderTranslator());

          if (stripSignatures) {
            classVisitor = new SignatureStripper(classVisitor);
          }

          classVisitor = new AnnotationWeaver(classVisitor);

          cr.accept(classVisitor, CUSTOM_ATTRIBUTES, ClassReader.EXPAND_FRAMES);       

            if (COMPACT_CONSTANTS) {
              Set<Constant> constants = new TreeSet<Constant>(new ConstantComparator());
              constants.addAll(cp.values());

              cr = new ClassReader(cw.toByteArray());
                cw = new ClassWriter(0);
                for(Constant c: constants) {
                  c.write(cw);
                }
                cr.accept(cw, 0);
            }

          bos.write(cw.toByteArray());
          return true;
        } catch (TranslatorException te) {
          listener.weavingError(te.getMessage());
          return false;
        } catch (LazyException e) {
View Full Code Here

  protected boolean weave(InputStream sourceStream, String fileName, ByteArrayOutputStream bos)
      throws IOException {

        ClassReader cr = new ClassReader(sourceStream);
        ClassWriter cw = new ClassWriter(true);

        try {
          NameTranslatorClassVisitor translator;
          ConstantPool cp;
            if (COMPACT_CONSTANTS) {
                cp = new ConstantPool();
                ClassConstantsCollector ccc = new ClassConstantsCollector(cw, cp);
                translator = new NameTranslatorClassVisitor(ccc);
            } else {
              translator = new NameTranslatorClassVisitor(cw);
            }
            ClassWeaver classWeaver = new ClassWeaver(translator, lazy, stripAttributes, target, listener);
            ClassVisitor v;
            if (stripSignatures) {
                v = new SignatureStripper(classWeaver);
            } else {
              v = classWeaver;
            }
            cr.accept(v, CUSTOM_ATTRIBUTES, false);       

            if (COMPACT_CONSTANTS) {
              Set<Constant> constants = new TreeSet<Constant>(new ConstantComparator());
              constants.addAll(cp.values());

              cr = new ClassReader(cw.toByteArray());
                cw = new ClassWriter(false);
                for(Constant c: constants)
                  c.write(cw);
                cr.accept(cw, false);
            }

          bos.write(cw.toByteArray());
          return true;
        } catch (TranslationException te) {
          listener.weavingError(te.getMessage());
          return false;
        } catch (LazyException e) {
View Full Code Here

        QName wrapperElement = messageInfo.getName();
       
        boolean anonymous = factory.getAnonymousWrapperTypes();

        ClassWriter cw = createClassWriter();
        String pkg = getPackageName(method) + ".jaxws_asm" + (anonymous ? "_an" : "");
        String className =  pkg + "."
            + StringUtils.capitalize(op.getName().getLocalPart());
        if (!isRequest) {
            className = className + "Response";
        }
        String pname = pkg + ".package-info";
        Class<?> def = findClass(pname, method.getDeclaringClass());
        if (def == null) {
            generatePackageInfo(pname, wrapperElement.getNamespaceURI(),
                                method.getDeclaringClass());
        }
       
        def = findClass(className, method.getDeclaringClass());
        if (def != null) {
            wrapperPart.setTypeClass(def);
            wrapperBeans.add(def);
            return;
        }
        String classFileName = periodToSlashes(className);
        cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classFileName, null,
                 "java/lang/Object", null);

        AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true);
        av0.visit("name", wrapperElement.getLocalPart());
        av0.visit("namespace", wrapperElement.getNamespaceURI());
        av0.visitEnd();

        av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
        av0.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
        av0.visitEnd();

        av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true);
        if (!anonymous) {
            av0.visit("name", wrapperElement.getLocalPart());
            av0.visit("namespace", wrapperElement.getNamespaceURI());
        } else {
            av0.visit("name", "");           
        }
        av0.visitEnd();

        // add constructor
        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label lbegin = new Label();
        mv.visitLabel(lbegin);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        Label lend = new Label();
        mv.visitLabel(lend);
        mv.visitLocalVariable("this", "L" + classFileName + ";", null, lbegin, lend, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();

        for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
            generateMessagePart(cw, mpi, method, classFileName);
        }

        cw.visitEnd();

        Class<?> clz = loadClass(className, method.getDeclaringClass(), cw.toByteArray());
        wrapperPart.setTypeClass(clz);
        wrapperBeans.add(clz);
    }
View Full Code Here

        wrapperPart.setTypeClass(clz);
        wrapperBeans.add(clz);
    }

    private void generatePackageInfo(String className, String ns, Class clz) {
        ClassWriter cw = createClassWriter();
        String classFileName = periodToSlashes(className);
        cw.visit(Opcodes.V1_5, Opcodes.ACC_ABSTRACT + Opcodes.ACC_INTERFACE, classFileName, null,
                 "java/lang/Object", null);
       
        boolean q = qualified;
        SchemaInfo si = interfaceInfo.getService().getSchema(ns);
        if (si != null) {
            q = si.isElementFormQualified();
        }
        AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlSchema;", true);
        av0.visit("namespace", ns);
        av0.visitEnum("elementFormDefault",
                      getClassCode(XmlNsForm.class),
                      q ? "QUALIFIED" : "UNQUALIFIED");
        av0.visitEnd();
        cw.visitEnd();

        loadClass(className, clz, cw.toByteArray());
    }
View Full Code Here

     */
    public static byte[] dump(Class clazz, String className, List fields, List methods, Handler handler) {
        Method[] itfmethods = clazz.getMethods();

        // Create the class
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        className = className.replace('.', '/');
        createClass(cw, className, clazz.getName());

        // Inject fields inside the POJO
        injectFields(cw, fields);

        generateConstructor(cw);
       
        for (int i = 0; i < itfmethods.length; ++i) {
            Method method = itfmethods[i];

            // Get the field for this method
            // 1) find the MethodMetadata
            FieldMetadata delegator = null; // field to delegate
            MethodMetadata methodDelegator = null; // field to delegate
            for (int j = 0; j < methods.size(); j++) {
                MethodMetadata methodMeta = (MethodMetadata) methods.get(j);
                if (methodMeta.equals(method)) {
                    delegator = methodMeta.getDelegation();
                    methodDelegator = methodMeta;
                }
            }

            generateMethod(cw, className, methodDelegator, method, delegator, handler);

        }

        // End process
        cw.visitEnd();
        return cw.toByteArray();
    }
View Full Code Here

  public static void main(String[] args) throws Exception
  {
    //add the synthetic modifier for each of the supplied args
    for (String arg : args) {
      FileInputStream classInStream = null;
      ClassWriter writer = null;
      try {
        //read in the class
        classInStream = new FileInputStream(arg);
        ClassReader reader = new ClassReader(classInStream);
        //make a ClassWriter constructed with the reader for speed
        //since we are mostly just copying
        //we just need to override the visit method so we can add
        //the synthetic modifier, otherwise we use the methods in
        //a standard writer
        writer = new ClassWriter(reader, 0) {
          @Override
          public void visit(int version, int access, String name, String signature,
              String superName, String[] interfaces)
          {
            super.visit(version, access | Opcodes.ACC_SYNTHETIC, name, signature, superName,
                interfaces);
          }
        };
        //call accept on the reader to start the visits
        //using the writer we created as the visitor
        reader.accept(writer, 0);
      } finally {
        //close the InputStream if it is hanging around
        if (classInStream != null) classInStream.close();
      }
      FileOutputStream classOutStream = null;
      try {
        //write out the new bytes of the class file
        classOutStream = new FileOutputStream(arg);
        if (writer != null) classOutStream.write(writer.toByteArray());
      } finally {
        //close the OutputStream if it is still around
        if (classOutStream != null) classOutStream.close();
      }
    }
View Full Code Here

    if((cReader.getAccess() & (ACC_INTERFACE | ACC_ANNOTATION | ACC_ENUM)) != 0)
      return null;
   
    //If we are Java 1.6 + compiled then we need to compute stack frames, otherwise
    //maxs are fine (and faster)
    ClassWriter cWriter = new OSGiFriendlyClassWriter(cReader, AbstractWovenProxyAdapter.IS_AT_LEAST_JAVA_6 ?
            ClassWriter.COMPUTE_FRAMES : ClassWriter.COMPUTE_MAXS, loader);
   
    //Wrap our outer layer to add the original SerialVersionUID if it was previously being defaulted
    ClassVisitor weavingAdapter = new SyntheticSerialVerUIDAdder(
                               new WovenProxyAdapter(cWriter, className, loader));
   
    // If we are Java 1.6 + then we need to skip frames as they will be recomputed
    cReader.accept(weavingAdapter, AbstractWovenProxyAdapter.IS_AT_LEAST_JAVA_6 ? ClassReader.SKIP_FRAMES : 0);
   
    return cWriter.toByteArray();
  }
View Full Code Here

TOP

Related Classes of org.objectweb.asm.ClassWriter

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.