View Javadoc
1   package org.apache.maven.coreits.javaagent.mng5669;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.instrument.ClassFileTransformer;
23  import java.lang.instrument.IllegalClassFormatException;
24  import java.lang.instrument.Instrumentation;
25  import java.security.ProtectionDomain;
26  
27  import org.objectweb.asm.ClassReader;
28  import org.objectweb.asm.ClassVisitor;
29  import org.objectweb.asm.ClassWriter;
30  import org.objectweb.asm.MethodVisitor;
31  import org.objectweb.asm.Opcodes;
32  import org.objectweb.asm.commons.AdviceAdapter;
33  
34  /**
35   *
36   * @author Robert Scholte
37   *
38   */
39  public class Premain
40  {
41      public static void premain( String agentArgs, Instrumentation inst )
42      {
43          inst.addTransformer( new ClassFileTransformer()
44          {
45  
46              public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined,
47                                       ProtectionDomain protectionDomain, byte[] classfileBuffer )
48                  throws IllegalClassFormatException
49              {
50                  if ( "org/apache/maven/model/io/DefaultModelReader".equals( className ) )
51                  {
52                      ClassReader r = new ClassReader( classfileBuffer );
53                      final ClassWriter w = new ClassWriter( Opcodes.ASM6 );
54  
55                      ClassVisitor v = new DefaultModelReaderVisitior( Opcodes.ASM6, w );
56  
57                      r.accept( v, ClassReader.EXPAND_FRAMES );
58                      return w.toByteArray();
59                  }
60                  else
61                  {
62                      return classfileBuffer;
63                  }
64              }
65          } );
66      }
67  
68      private static class DefaultModelReaderVisitior
69          extends ClassVisitor
70      {
71          DefaultModelReaderVisitior( int api, org.objectweb.asm.ClassVisitor cv )
72          {
73              super( api, cv );
74          }
75  
76          @Override
77          public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions )
78          {
79              MethodVisitor mv = cv.visitMethod( access, name, desc, signature, exceptions );
80              if ( "getSource".equals( name ) )
81              {
82                  return new GetSourceMethodAdvice( Opcodes.ASM6, mv, access, name, desc );
83              }
84              else
85              {
86                  return mv;
87              }
88          }
89      }
90  
91      // org.apache.maven.model.io.DefaultModelReader.getSource(Map<String, ?>)
92      private static class GetSourceMethodAdvice
93          extends AdviceAdapter
94      {
95          GetSourceMethodAdvice( int api, MethodVisitor mv, int access, String name, String desc )
96          {
97              super( api, mv, access, name, desc );
98          }
99  
100         @Override
101         protected void onMethodEnter()
102         {
103             // System.out.println( options ),
104             mv.visitFieldInsn( GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;" );
105             mv.visitVarInsn( ALOAD, 1 );
106             mv.visitMethodInsn( INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V", false );
107         }
108     }
109 }