< prev index next >

src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotSignature.java

Print this page




  28 import jdk.vm.ci.common.JVMCIError;
  29 import jdk.vm.ci.meta.JavaKind;
  30 import jdk.vm.ci.meta.JavaType;
  31 import jdk.vm.ci.meta.ResolvedJavaType;
  32 import jdk.vm.ci.meta.Signature;
  33 
  34 /**
  35  * Represents a method signature.
  36  */
  37 public class HotSpotSignature implements Signature {
  38 
  39     private final List<String> parameters = new ArrayList<>();
  40     private final String returnType;
  41     private final String originalString;
  42     private ResolvedJavaType[] parameterTypes;
  43     private ResolvedJavaType returnTypeCache;
  44     private final HotSpotJVMCIRuntimeProvider runtime;
  45 
  46     public HotSpotSignature(HotSpotJVMCIRuntimeProvider runtime, String signature) {
  47         this.runtime = runtime;
  48         assert signature.length() > 0;


  49         this.originalString = signature;
  50 
  51         if (signature.charAt(0) == '(') {
  52             int cur = 1;
  53             while (cur < signature.length() && signature.charAt(cur) != ')') {
  54                 int nextCur = parseSignature(signature, cur);
  55                 parameters.add(signature.substring(cur, nextCur));
  56                 cur = nextCur;
  57             }
  58 
  59             cur++;
  60             int nextCur = parseSignature(signature, cur);
  61             returnType = signature.substring(cur, nextCur);
  62             assert nextCur == signature.length();


  63         } else {
  64             returnType = null;
  65         }
  66     }
  67 
  68     public HotSpotSignature(HotSpotJVMCIRuntimeProvider runtime, ResolvedJavaType returnType, ResolvedJavaType... parameterTypes) {
  69         this.runtime = runtime;
  70         this.parameterTypes = parameterTypes.clone();
  71         this.returnTypeCache = returnType;
  72         this.returnType = returnType.getName();
  73         StringBuilder sb = new StringBuilder("(");
  74         for (JavaType type : parameterTypes) {
  75             parameters.add(type.getName());
  76             sb.append(type.getName());
  77         }
  78         sb.append(")").append(returnType.getName());
  79         this.originalString = sb.toString();
  80         assert new HotSpotSignature(runtime, originalString).equals(this);
  81     }
  82 
  83     private static int parseSignature(String signature, int start) {

  84         int cur = start;
  85         char first;
  86         do {
  87             first = signature.charAt(cur++);

  88         } while (first == '[');
  89 
  90         switch (first) {
  91             case 'L':
  92                 while (signature.charAt(cur) != ';') {



  93                     cur++;
  94                 }
  95                 cur++;
  96                 break;
  97             case 'V':
  98             case 'I':
  99             case 'B':
 100             case 'C':
 101             case 'D':
 102             case 'F':
 103             case 'J':
 104             case 'S':
 105             case 'Z':
 106                 break;
 107             default:
 108                 throw new JVMCIError("Invalid character at index %d in signature: %s", cur, signature);
 109         }
 110         return cur;



 111     }
 112 
 113     @Override
 114     public int getParameterCount(boolean withReceiver) {
 115         return parameters.size() + (withReceiver ? 1 : 0);
 116     }
 117 
 118     @Override
 119     public JavaKind getParameterKind(int index) {
 120         return JavaKind.fromTypeString(parameters.get(index));
 121     }
 122 
 123     private static boolean checkValidCache(ResolvedJavaType type, ResolvedJavaType accessingClass) {
 124         assert accessingClass != null;
 125         if (type == null) {
 126             return false;
 127         } else if (type instanceof HotSpotResolvedObjectTypeImpl) {
 128             return ((HotSpotResolvedObjectTypeImpl) type).isDefinitelyResolvedWithRespectTo(accessingClass);
 129         }
 130         return true;




  28 import jdk.vm.ci.common.JVMCIError;
  29 import jdk.vm.ci.meta.JavaKind;
  30 import jdk.vm.ci.meta.JavaType;
  31 import jdk.vm.ci.meta.ResolvedJavaType;
  32 import jdk.vm.ci.meta.Signature;
  33 
  34 /**
  35  * Represents a method signature.
  36  */
  37 public class HotSpotSignature implements Signature {
  38 
  39     private final List<String> parameters = new ArrayList<>();
  40     private final String returnType;
  41     private final String originalString;
  42     private ResolvedJavaType[] parameterTypes;
  43     private ResolvedJavaType returnTypeCache;
  44     private final HotSpotJVMCIRuntimeProvider runtime;
  45 
  46     public HotSpotSignature(HotSpotJVMCIRuntimeProvider runtime, String signature) {
  47         this.runtime = runtime;
  48         if (signature.length() == 0) {
  49             throw new IllegalArgumentException("Signature cannot be empty");
  50         }
  51         this.originalString = signature;
  52 
  53         if (signature.charAt(0) == '(') {
  54             int cur = 1;
  55             while (cur < signature.length() && signature.charAt(cur) != ')') {
  56                 int nextCur = parseSignature(signature, cur);
  57                 parameters.add(signature.substring(cur, nextCur));
  58                 cur = nextCur;
  59             }
  60 
  61             cur++;
  62             int nextCur = parseSignature(signature, cur);
  63             returnType = signature.substring(cur, nextCur);
  64             if (nextCur != signature.length()) {
  65                 throw new IllegalArgumentException("Extra characters at end of signature: " + signature);
  66             }
  67         } else {
  68             throw new IllegalArgumentException("Signature must start with a '(': " + signature);
  69         }
  70     }
  71 
  72     public HotSpotSignature(HotSpotJVMCIRuntimeProvider runtime, ResolvedJavaType returnType, ResolvedJavaType... parameterTypes) {
  73         this.runtime = runtime;
  74         this.parameterTypes = parameterTypes.clone();
  75         this.returnTypeCache = returnType;
  76         this.returnType = returnType.getName();
  77         StringBuilder sb = new StringBuilder("(");
  78         for (JavaType type : parameterTypes) {
  79             parameters.add(type.getName());
  80             sb.append(type.getName());
  81         }
  82         sb.append(")").append(returnType.getName());
  83         this.originalString = sb.toString();
  84         assert new HotSpotSignature(runtime, originalString).equals(this);
  85     }
  86 
  87     private static int parseSignature(String signature, int start) {
  88         try {
  89             int cur = start;
  90             char first;
  91             do {
  92                 first = signature.charAt(cur);
  93                 cur++;
  94             } while (first == '[');
  95 
  96             switch (first) {
  97                 case 'L':
  98                     while (signature.charAt(cur) != ';') {
  99                         if (signature.charAt(cur) == '.') {
 100                             throw new IllegalArgumentException("Class name in signature contains '.' at index " + cur + ": " + signature);
 101                         }
 102                         cur++;
 103                     }
 104                     cur++;
 105                     break;
 106                 case 'V':
 107                 case 'I':
 108                 case 'B':
 109                 case 'C':
 110                 case 'D':
 111                 case 'F':
 112                 case 'J':
 113                 case 'S':
 114                 case 'Z':
 115                     break;
 116                 default:
 117                     throw new IllegalArgumentException("Invalid character '" + signature.charAt(cur - 1) + "' at index " + (cur - 1) + " in signature: " + signature);
 118             }
 119             return cur;
 120         } catch (StringIndexOutOfBoundsException e) {
 121             throw new IllegalArgumentException("Truncated signature: " + signature);
 122         }
 123     }
 124 
 125     @Override
 126     public int getParameterCount(boolean withReceiver) {
 127         return parameters.size() + (withReceiver ? 1 : 0);
 128     }
 129 
 130     @Override
 131     public JavaKind getParameterKind(int index) {
 132         return JavaKind.fromTypeString(parameters.get(index));
 133     }
 134 
 135     private static boolean checkValidCache(ResolvedJavaType type, ResolvedJavaType accessingClass) {
 136         assert accessingClass != null;
 137         if (type == null) {
 138             return false;
 139         } else if (type instanceof HotSpotResolvedObjectTypeImpl) {
 140             return ((HotSpotResolvedObjectTypeImpl) type).isDefinitelyResolvedWithRespectTo(accessingClass);
 141         }
 142         return true;


< prev index next >