View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.javaflow.bytecode.transformation.asm;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.commons.javaflow.bytecode.transformation.ResourceTransformer;
23  import org.objectweb.asm.ClassReader;
24  import org.objectweb.asm.ClassWriter;
25  import org.objectweb.asm.util.CheckClassAdapter;
26  
27  
28  /**
29   * AsmClassTransformer
30   * 
31   * @author Eugene Kuleshov
32   */
33  public final class AsmClassTransformer implements ResourceTransformer {
34  
35      public byte[] transform(InputStream is) throws IOException {
36          return transform(new ClassReader(is));
37      }
38  
39      public byte[] transform(final byte[] original) {
40          return transform(new ClassReader(original));
41      }
42  
43      private byte[] transform(ClassReader cr) {
44          // final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
45          final ClassWriter cw = new ClassWriter(true, false);
46  
47          // print bytecode before transformation
48          // cr.accept(new TraceClassVisitor(new ContinuationClassAdapter(this, cw), new PrintWriter(System.out)), false);
49  
50          // prints bytecode after transformation
51          // cr.accept(new ContinuationClassAdapter(this, new TraceClassVisitor(cw, new PrintWriter(System.err))), 0);
52          // cr.accept(new ContinuationClassAdapter(this, new TraceClassVisitor(cw, new PrintWriter(System.err))), false);
53  
54          cr.accept(new ContinuationClassAdapter(new CheckClassAdapter(cw)), false);
55  
56          byte[] bytecode = cw.toByteArray();
57  
58          // CheckClassAdapter.verify(new ClassReader(bytecode), true);
59          // new ClassReader(bytecode).accept(new ASMifierClassVisitor(new PrintWriter(System.err)), false);
60  
61          // ClassWriter cww = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
62          // new ClassReader(bytecode).accept(cww, ClassReader.SKIP_DEBUG);
63          // new ClassReader(cww.toByteArray()).accept(new TraceClassVisitor(new PrintWriter(System.err)), 0);
64  
65          return bytecode;
66      }
67  
68  }
69