|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -44,49 +44,64 @@ public SignaturesImpl() {
|
44 | 44 | public ClassSignature parseClassSignature(String signature) {
|
45 | 45 | this.sig = signature;
|
46 | 46 | sigp = 0;
|
47 |
| - List<TypeParam> typeParamTypes = parseParamTypes(); |
48 |
| - RefTypeSig superclass = referenceTypeSig(); |
49 |
| - ArrayList<RefTypeSig> superinterfaces = null; |
50 |
| - while (sigp < sig.length()) { |
51 |
| - if (superinterfaces == null) |
52 |
| - superinterfaces = new ArrayList<>(); |
53 |
| - superinterfaces.add(referenceTypeSig()); |
| 47 | + try { |
| 48 | + List<TypeParam> typeParamTypes = parseParamTypes(); |
| 49 | + RefTypeSig superclass = referenceTypeSig(); |
| 50 | + ArrayList<RefTypeSig> superinterfaces = null; |
| 51 | + while (sigp < sig.length()) { |
| 52 | + if (superinterfaces == null) |
| 53 | + superinterfaces = new ArrayList<>(); |
| 54 | + superinterfaces.add(referenceTypeSig()); |
| 55 | + } |
| 56 | + return new ClassSignatureImpl(typeParamTypes, superclass, null2Empty(superinterfaces)); |
| 57 | + } catch (IndexOutOfBoundsException e) { |
| 58 | + throw error("Not a valid class signature"); |
54 | 59 | }
|
55 |
| - return new ClassSignatureImpl(typeParamTypes, superclass, null2Empty(superinterfaces)); |
56 | 60 | }
|
57 | 61 |
|
58 | 62 | public MethodSignature parseMethodSignature(String signature) {
|
59 | 63 | this.sig = signature;
|
60 | 64 | sigp = 0;
|
61 |
| - List<TypeParam> typeParamTypes = parseParamTypes(); |
62 |
| - assert sig.charAt(sigp) == '('; |
63 |
| - sigp++; |
64 |
| - ArrayList<Signature> paramTypes = null; |
65 |
| - while (sig.charAt(sigp) != ')') { |
66 |
| - if (paramTypes == null) |
67 |
| - paramTypes = new ArrayList<>(); |
68 |
| - paramTypes.add(typeSig()); |
69 |
| - } |
70 |
| - sigp++; |
71 |
| - Signature returnType = typeSig(); |
72 |
| - ArrayList<ThrowableSig> throwsTypes = null; |
73 |
| - while (sigp < sig.length() && sig.charAt(sigp) == '^') { |
| 65 | + try { |
| 66 | + List<TypeParam> typeParamTypes = parseParamTypes(); |
| 67 | + if (sig.charAt(sigp) != '(') throw error("Expected ( at possition %d of signature".formatted(sigp)); |
| 68 | + sigp++; |
| 69 | + ArrayList<Signature> paramTypes = null; |
| 70 | + while (sig.charAt(sigp) != ')') { |
| 71 | + if (paramTypes == null) |
| 72 | + paramTypes = new ArrayList<>(); |
| 73 | + paramTypes.add(typeSig()); |
| 74 | + } |
74 | 75 | sigp++;
|
75 |
| - if (throwsTypes == null) |
76 |
| - throwsTypes = new ArrayList<>(); |
77 |
| - var t = typeSig(); |
78 |
| - if (t instanceof ThrowableSig ts) |
79 |
| - throwsTypes.add(ts); |
80 |
| - else |
81 |
| - throw new IllegalArgumentException("not a valid type signature: " + sig); |
| 76 | + Signature returnType = typeSig(); |
| 77 | + ArrayList<ThrowableSig> throwsTypes = null; |
| 78 | + while (sigp < sig.length()) { |
| 79 | + if (sig.charAt(sigp) != '^') throw error("Expected ^ at possition %d of signature".formatted(sigp)); |
| 80 | + sigp++; |
| 81 | + if (throwsTypes == null) |
| 82 | + throwsTypes = new ArrayList<>(); |
| 83 | + var t = referenceTypeSig(); |
| 84 | + if (t instanceof ThrowableSig ts) |
| 85 | + throwsTypes.add(ts); |
| 86 | + else |
| 87 | + throw error("Not a valid throwable signature %s in".formatted(t.signatureString())); |
| 88 | + } |
| 89 | + return new MethodSignatureImpl(typeParamTypes, null2Empty(throwsTypes), returnType, null2Empty(paramTypes)); |
| 90 | + } catch (IndexOutOfBoundsException e) { |
| 91 | + throw error("Not a valid method signature"); |
82 | 92 | }
|
83 |
| - return new MethodSignatureImpl(typeParamTypes, null2Empty(throwsTypes), returnType, null2Empty(paramTypes)); |
84 | 93 | }
|
85 | 94 |
|
86 | 95 | public Signature parseSignature(String signature) {
|
87 | 96 | this.sig = signature;
|
88 | 97 | sigp = 0;
|
89 |
| - return typeSig(); |
| 98 | + try { |
| 99 | + var s = typeSig(); |
| 100 | + if (sigp == signature.length()) |
| 101 | + return s; |
| 102 | + } catch (IndexOutOfBoundsException e) { |
| 103 | + } |
| 104 | + throw error("Not a valid type signature"); |
90 | 105 | }
|
91 | 106 |
|
92 | 107 | private List<TypeParam> parseParamTypes() {
|
@@ -157,7 +172,7 @@ private RefTypeSig referenceTypeSig() {
|
157 | 172 | return ty;
|
158 | 173 | case '[': return ArrayTypeSig.of(typeSig());
|
159 | 174 | }
|
160 |
| - throw new IllegalArgumentException("not a valid type signature: " + sig); |
| 175 | + throw error("Unexpected character %c at possition %d of signature".formatted(c, sigp - 1)); |
161 | 176 | }
|
162 | 177 |
|
163 | 178 | private TypeArg typeArg() {
|
@@ -292,4 +307,8 @@ public String signatureString() {
|
292 | 307 | private static <T> List<T> null2Empty(ArrayList<T> l) {
|
293 | 308 | return l == null ? List.of() : Collections.unmodifiableList(l);
|
294 | 309 | }
|
| 310 | + |
| 311 | + private IllegalArgumentException error(String message) { |
| 312 | + return new IllegalArgumentException("%s: %s".formatted(message, sig)); |
| 313 | + } |
295 | 314 | }
|
0 commit comments