View Javadoc

1   /**
2    * Copyright 2007 Google Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.tonicsystems.jarjar;
18  
19  import org.objectweb.asm.*;
20  
21  abstract class StringReader extends ClassVisitor
22  {
23      private int line = -1;
24      private String className;
25  
26      public StringReader() {
27          super(Opcodes.ASM4);
28      }
29      
30      abstract public void visitString(String className, String value, int line);
31  
32      private void handleObject(Object value) {
33          if (value instanceof String)
34              visitString(className, (String)value, line);
35      }
36      
37      @Override
38      public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
39          className = name;
40          line = -1;
41      }
42  
43      public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
44          handleObject(value);
45          return new FieldVisitor(Opcodes.ASM4){
46              @Override
47              public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
48                  return StringReader.this.visitAnnotation(desc, visible);
49              }
50          };
51      }
52      
53      @Override
54      public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
55          return new AnnotationVisitor(Opcodes.ASM4) {
56              @Override
57              public void visit(String name, Object value) {
58                  handleObject(value);
59              }
60              @Override
61              public void visitEnum(String name, String desc, String value) {
62                  handleObject(value);
63              }
64              @Override
65              public AnnotationVisitor visitAnnotation(String name, String desc) {
66                  return this;
67              }
68          };
69      }
70      
71      @Override
72      public MethodVisitor visitMethod(int access, String name, String desc,
73              String signature, String[] exceptions) {
74          MethodVisitor mv = new MethodVisitor(Opcodes.ASM4){
75              @Override
76              public void visitLdcInsn(Object cst) {
77                  handleObject(cst);
78              }
79              @Override
80              public void visitLineNumber(int line, Label start) {
81                  StringReader.this.line = line;
82              }
83              @Override
84              public void visitInvokeDynamicInsn(String name, String desc,
85                      Handle bsm, Object... bsmArgs) {
86                  for (Object bsmArg : bsmArgs) handleObject(bsmArg);
87              }
88              @Override
89              public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
90                  return StringReader.this.visitAnnotation(desc, visible);
91              }
92              @Override
93              public AnnotationVisitor visitParameterAnnotation(int parameter,
94                      String desc, boolean visible) {
95                  return StringReader.this.visitAnnotation(desc, visible);
96              }
97          };
98          return mv;
99      }
100 }