jdk/make/modules/tools/src/com/sun/classanalyzer/CodeAttributeParser.java
changeset 8642 a6cccd458bef
parent 8641 fbf4a969ccba
parent 8608 8e043a4d3cf6
child 8643 def8e16dd237
equal deleted inserted replaced
8641:fbf4a969ccba 8642:a6cccd458bef
     1 /*
       
     2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  *
       
    23  */
       
    24 
       
    25 package com.sun.classanalyzer;
       
    26 
       
    27 import com.sun.classanalyzer.Klass.Method;
       
    28 
       
    29 import com.sun.tools.classfile.*;
       
    30 import com.sun.tools.classfile.Instruction.*;
       
    31 
       
    32 import java.util.HashMap;
       
    33 import java.util.Map;
       
    34 import java.util.Set;
       
    35 import java.util.TreeSet;
       
    36 
       
    37 /**
       
    38  *
       
    39  * @author Mandy Chung
       
    40  */
       
    41 public class CodeAttributeParser {
       
    42     private final ClassFileParser cfparser;
       
    43     private final ConstantPool cpool;
       
    44     private final ConstantPoolParser constantPoolParser;
       
    45 
       
    46 
       
    47     static final Map<String, Set<Method>> runtimeReferences =
       
    48             new HashMap<String, Set<Method>>();
       
    49 
       
    50 
       
    51     CodeAttributeParser(ClassFileParser parser) {
       
    52         this.cfparser = parser;
       
    53         this.cpool = cfparser.classfile.constant_pool;
       
    54         this.constantPoolParser = cfparser.constantPoolParser;
       
    55     }
       
    56 
       
    57     static boolean parseCodeAttribute = false; // by default don't parse code attribute
       
    58     static void setParseCodeAttribute(boolean newValue) {
       
    59         parseCodeAttribute = newValue;
       
    60     }
       
    61 
       
    62     void parse(Code_attribute attr, Klass.Method method) {
       
    63         if (!parseCodeAttribute) {
       
    64             return;
       
    65         }
       
    66 
       
    67         for (Instruction instr : attr.getInstructions()) {
       
    68             try {
       
    69                 instr.accept(instructionVisitor, method);
       
    70             } catch (ArrayIndexOutOfBoundsException e) {
       
    71                 throw new RuntimeException("error at or after byte " + instr.getPC());
       
    72             }
       
    73 
       
    74         }
       
    75 
       
    76         if (attr.exception_table_langth > 0) {
       
    77             for (int i = 0; i <
       
    78                     attr.exception_table.length; i++) {
       
    79                 Code_attribute.Exception_data handler = attr.exception_table[i];
       
    80                 int catch_type = handler.catch_type;
       
    81                 if (catch_type > 0) {
       
    82                     addMethodReference(catch_type, method);
       
    83                 }
       
    84 
       
    85             }
       
    86         }
       
    87 
       
    88     }
       
    89 
       
    90 
       
    91     private void addMethodReference(int index, Klass.Method m) {
       
    92         String method = constantPoolParser.getMethodName(index);
       
    93 
       
    94         if (method != null &&
       
    95                (method.equals("java.lang.Class.forName") ||
       
    96                 method.equals("java.lang.Class.loadClass") ||
       
    97                 method.startsWith("java.util.ServiceLoader.load") ||
       
    98                 method.equals("sun.misc.Service.providers"))) {
       
    99             Set<Method> refs = runtimeReferences.get(method);
       
   100             if (refs == null) {
       
   101                 refs = new TreeSet<Method>();
       
   102                 runtimeReferences.put(method, refs);
       
   103             }
       
   104             refs.add(m);
       
   105         }
       
   106     }
       
   107 
       
   108     Instruction.KindVisitor<Void, Klass.Method> instructionVisitor =
       
   109             new Instruction.KindVisitor<Void, Klass.Method>() {
       
   110 
       
   111                 public Void visitNoOperands(Instruction instr, Klass.Method m) {
       
   112                     return null;
       
   113                 }
       
   114 
       
   115                 public Void visitArrayType(Instruction instr, TypeKind kind, Klass.Method m) {
       
   116                     return null;
       
   117                 }
       
   118 
       
   119                 public Void visitBranch(Instruction instr, int offset, Klass.Method m) {
       
   120                     return null;
       
   121                 }
       
   122 
       
   123                 public Void visitConstantPoolRef(Instruction instr, int index, Klass.Method m) {
       
   124                     addMethodReference(index, m);
       
   125                     return null;
       
   126                 }
       
   127 
       
   128                 public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Klass.Method m) {
       
   129                     addMethodReference(index, m);
       
   130                     return null;
       
   131                 }
       
   132 
       
   133                 public Void visitLocal(Instruction instr, int index, Klass.Method m) {
       
   134                     return null;
       
   135                 }
       
   136 
       
   137                 public Void visitLocalAndValue(Instruction instr, int index, int value, Klass.Method m) {
       
   138                     return null;
       
   139                 }
       
   140 
       
   141                 public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, Klass.Method m) {
       
   142                     return null;
       
   143                 }
       
   144 
       
   145                 public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, Klass.Method m) {
       
   146                     return null;
       
   147                 }
       
   148 
       
   149                 public Void visitValue(Instruction instr, int value, Klass.Method m) {
       
   150                     return null;
       
   151                 }
       
   152 
       
   153                 public Void visitUnknown(Instruction instr, Klass.Method m) {
       
   154                     return null;
       
   155                 }
       
   156             };
       
   157 }