1 /*
   2  * Copyright (c) 2015, 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 package compiler.compilercontrol.share.method;
  25 
  26 import jdk.test.lib.Utils;
  27 
  28 import java.lang.reflect.Constructor;
  29 import java.lang.reflect.Executable;
  30 import java.lang.reflect.Method;
  31 import java.util.Arrays;
  32 
  33 /**
  34  * This class represents a signature of the method
  35  */
  36 public class SignatureType extends MethodElementType {
  37     public SignatureType(Executable method) {
  38         super(MethodDescriptor.Separator.NONE);
  39         // Get parameters
  40         Class<?>[] types = method.getParameterTypes();
  41         String[] parameterTypes = new String[types.length];
  42         for (int i = 0; i < types.length; i++) {
  43             parameterTypes[i] = Utils.toJVMTypeSignature(types[i]);
  44         }
  45         // Get return value
  46         String returnType;
  47         if (method instanceof Method) {
  48             returnType = Utils.toJVMTypeSignature(((Method) method)
  49                     .getReturnType());
  50         } else if (method instanceof Constructor) {
  51             // Constructor returns void in VM
  52             returnType = Utils.toJVMTypeSignature(void.class);
  53         } else {
  54             throw new Error(String.format("TESTBUG: wrong type of executable "
  55                     + "%s of class %s", method, method.getClass()));
  56         }
  57         // Create signature
  58         setElement("(" + String.join("", parameterTypes)+ ")" + returnType);
  59         regexp = element;
  60         setPattern(MethodDescriptor.PatternType.EXACT);
  61         separator = MethodDescriptor.Separator.NONE;
  62     }
  63 
  64     @Override
  65     public void setElement(String element) {
  66         if (element.isEmpty()) {
  67             setPattern(MethodDescriptor.PatternType.ANY);
  68         } else {
  69             super.setElement(element);
  70         }
  71     }
  72 
  73     @Override
  74     public boolean isValid() {
  75         if (element.isEmpty()) {
  76             return true;
  77         }
  78         // Allowed primitive types
  79         char[] baseTypes = {'B', 'C', 'D', 'F', 'I', 'J', 'S', 'Z'};  // sorted
  80         // Parsing states
  81         boolean isArray = false;
  82         boolean isInsideSig = false;
  83         boolean isClass = false;
  84 
  85         for (char ch : element.toCharArray()) {
  86             if (ch == '(') {
  87                 if (isInsideSig) {
  88                     // Met another ( inside
  89                     return false;
  90                 }
  91                 isInsideSig = true;
  92             } else if (ch == ')') {
  93                 if (!isInsideSig) {
  94                     // met another ) outside
  95                     return false;
  96                 }
  97                 isInsideSig = false;
  98             } else if (ch == 'V') {
  99                 if (isInsideSig) {
 100                     // void type is allowed only as a return value
 101                     return false;
 102                 }
 103             } else if (ch == 'L') {
 104                 // this is a beginning of class/interface
 105                 isClass = true;
 106                 // met actual type of array
 107                 isArray = false;
 108             } else if (ch == '[') {
 109                 isArray = true;
 110             } else if (isClass) {
 111                 if (!Character.isJavaIdentifierPart(ch)) {
 112                     if (ch == '/' || ch == '.') {
 113                         // separator met
 114                     } else if (ch == ';') {
 115                         // end of class/interface
 116                         isClass = false;
 117                     } else {
 118                         return false;
 119                     }
 120                 }
 121             } else if (Arrays.binarySearch(baseTypes, ch) < 0) {
 122                 // if it doesn't belong to base types
 123                 return false;
 124             } else {
 125                 // array of a base type
 126                 isArray = false;
 127             }
 128         }
 129         return !(isArray || isInsideSig || isClass);
 130     }
 131 
 132     @Override
 133     public void setPattern(MethodDescriptor.PatternType patternType) {
 134         switch (patternType) {
 135             case PREFIX:
 136             case SUFFIX:
 137             case SUBSTRING:
 138                 // These patterns are not supported in Compiler Control
 139                 // Just use ANY pattern instead
 140             case ANY:
 141                 regexp = "\\(.*\\).*";
 142                 element = "";
 143                 break;
 144             case EXACT:
 145                 break;
 146             default:
 147                 throw new IllegalArgumentException("ERROR: wrong pattern type "
 148                         + patternType);
 149         }
 150     }
 151 }