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