author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58078 | 41f119856e7c |
permissions | -rw-r--r-- |
52914 | 1 |
/* |
54965
e022b9cb35a5
8223723: j.l.c.MethodTypeDesc.dropParameterTypes​ throws the undocumented exception: IllegalArgumentException
vromero
parents:
52914
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
52914 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.lang.constant; |
|
26 |
||
27 |
import java.lang.invoke.MethodHandles; |
|
28 |
import java.lang.invoke.MethodType; |
|
58078
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
29 |
import java.security.AccessController; |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
30 |
import java.security.PrivilegedAction; |
52914 | 31 |
import java.util.Arrays; |
32 |
import java.util.List; |
|
33 |
||
34 |
import static java.util.Objects.requireNonNull; |
|
35 |
||
36 |
/** |
|
37 |
* A <a href="package-summary.html#nominal">nominal descriptor</a> for a |
|
38 |
* {@link MethodType}. A {@linkplain MethodTypeDescImpl} corresponds to a |
|
39 |
* {@code Constant_MethodType_info} entry in the constant pool of a classfile. |
|
40 |
*/ |
|
41 |
final class MethodTypeDescImpl implements MethodTypeDesc { |
|
42 |
private final ClassDesc returnType; |
|
43 |
private final ClassDesc[] argTypes; |
|
44 |
||
45 |
/** |
|
46 |
* Constructs a {@linkplain MethodTypeDesc} with the specified return type |
|
47 |
* and parameter types |
|
48 |
* |
|
49 |
* @param returnType a {@link ClassDesc} describing the return type |
|
50 |
* @param argTypes {@link ClassDesc}s describing the parameter types |
|
51 |
*/ |
|
52 |
MethodTypeDescImpl(ClassDesc returnType, ClassDesc[] argTypes) { |
|
53 |
this.returnType = requireNonNull(returnType); |
|
54 |
this.argTypes = requireNonNull(argTypes); |
|
55 |
||
56 |
for (ClassDesc cr : argTypes) |
|
57 |
if (cr.isPrimitive() && cr.descriptorString().equals("V")) |
|
58 |
throw new IllegalArgumentException("Void parameters not permitted"); |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Creates a {@linkplain MethodTypeDescImpl} given a method descriptor string. |
|
63 |
* |
|
64 |
* @param descriptor the method descriptor string |
|
65 |
* @return a {@linkplain MethodTypeDescImpl} describing the desired method type |
|
66 |
* @throws IllegalArgumentException if the descriptor string is not a valid |
|
67 |
* method descriptor |
|
68 |
* @jvms 4.3.3 Method Descriptors |
|
69 |
*/ |
|
70 |
static MethodTypeDescImpl ofDescriptor(String descriptor) { |
|
71 |
requireNonNull(descriptor); |
|
72 |
List<String> types = ConstantUtils.parseMethodDescriptor(descriptor); |
|
73 |
ClassDesc[] paramTypes = types.stream().skip(1).map(ClassDesc::ofDescriptor).toArray(ClassDesc[]::new); |
|
74 |
return new MethodTypeDescImpl(ClassDesc.ofDescriptor(types.get(0)), paramTypes); |
|
75 |
} |
|
76 |
||
77 |
@Override |
|
78 |
public ClassDesc returnType() { |
|
79 |
return returnType; |
|
80 |
} |
|
81 |
||
82 |
@Override |
|
83 |
public int parameterCount() { |
|
84 |
return argTypes.length; |
|
85 |
} |
|
86 |
||
87 |
@Override |
|
88 |
public ClassDesc parameterType(int index) { |
|
89 |
return argTypes[index]; |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public List<ClassDesc> parameterList() { |
|
94 |
return List.of(argTypes); |
|
95 |
} |
|
96 |
||
97 |
@Override |
|
98 |
public ClassDesc[] parameterArray() { |
|
99 |
return argTypes.clone(); |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public MethodTypeDesc changeReturnType(ClassDesc returnType) { |
|
104 |
return MethodTypeDesc.of(returnType, argTypes); |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public MethodTypeDesc changeParameterType(int index, ClassDesc paramType) { |
|
109 |
ClassDesc[] newArgs = argTypes.clone(); |
|
110 |
newArgs[index] = paramType; |
|
111 |
return MethodTypeDesc.of(returnType, newArgs); |
|
112 |
} |
|
113 |
||
114 |
@Override |
|
115 |
public MethodTypeDesc dropParameterTypes(int start, int end) { |
|
54965
e022b9cb35a5
8223723: j.l.c.MethodTypeDesc.dropParameterTypes​ throws the undocumented exception: IllegalArgumentException
vromero
parents:
52914
diff
changeset
|
116 |
if (start < 0 || start >= argTypes.length || end < 0 || end > argTypes.length || start > end) |
52914 | 117 |
throw new IndexOutOfBoundsException(); |
118 |
ClassDesc[] newArgs = new ClassDesc[argTypes.length - (end - start)]; |
|
119 |
System.arraycopy(argTypes, 0, newArgs, 0, start); |
|
120 |
System.arraycopy(argTypes, end, newArgs, start, argTypes.length - end); |
|
121 |
return MethodTypeDesc.of(returnType, newArgs); |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes) { |
|
126 |
if (pos < 0 || pos > argTypes.length) |
|
127 |
throw new IndexOutOfBoundsException(pos); |
|
128 |
ClassDesc[] newArgs = new ClassDesc[argTypes.length + paramTypes.length]; |
|
129 |
System.arraycopy(argTypes, 0, newArgs, 0, pos); |
|
130 |
System.arraycopy(paramTypes, 0, newArgs, pos, paramTypes.length); |
|
131 |
System.arraycopy(argTypes, pos, newArgs, pos+paramTypes.length, argTypes.length - pos); |
|
132 |
return MethodTypeDesc.of(returnType, newArgs); |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
55502
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
136 |
public MethodType resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException { |
58078
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
137 |
MethodType mtype = AccessController.doPrivileged(new PrivilegedAction<>() { |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
138 |
@Override |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
139 |
public MethodType run() { |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
140 |
return MethodType.fromMethodDescriptorString(descriptorString(), |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
141 |
lookup.lookupClass().getClassLoader()); |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
142 |
} |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
143 |
}); |
41f119856e7c
8229785: MethodType::fromMethodDescriptorString should require security permission if loader is null
mchung
parents:
55502
diff
changeset
|
144 |
|
55502
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
145 |
// let's check that the lookup has access to all the types in the method type |
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
146 |
lookup.accessClass(mtype.returnType()); |
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
147 |
for (Class<?> paramType: mtype.parameterArray()) { |
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
148 |
lookup.accessClass(paramType); |
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
149 |
} |
bdaec4628ea9
8226709: MethodTypeDesc::resolveConstantDesc needs access check per the specification
vromero
parents:
54965
diff
changeset
|
150 |
return mtype; |
52914 | 151 |
} |
152 |
||
153 |
/** |
|
154 |
* Returns {@code true} if this {@linkplain MethodTypeDescImpl} is |
|
155 |
* equal to another {@linkplain MethodTypeDescImpl}. Equality is |
|
156 |
* determined by the two descriptors having equal return types and argument |
|
157 |
* types. |
|
158 |
* |
|
159 |
* @param o the {@code MethodTypeDescImpl} to compare to this |
|
160 |
* {@code MethodTypeDescImpl} |
|
161 |
* @return {@code true} if the specified {@code MethodTypeDescImpl} is |
|
162 |
* equals to this {@code MethodTypeDescImpl}. |
|
163 |
*/ |
|
164 |
@Override |
|
165 |
public boolean equals(Object o) { |
|
166 |
if (this == o) return true; |
|
167 |
if (o == null || getClass() != o.getClass()) return false; |
|
168 |
||
169 |
MethodTypeDescImpl constant = (MethodTypeDescImpl) o; |
|
170 |
||
171 |
return returnType.equals(constant.returnType) |
|
172 |
&& Arrays.equals(argTypes, constant.argTypes); |
|
173 |
} |
|
174 |
||
175 |
@Override |
|
176 |
public int hashCode() { |
|
177 |
int result = returnType.hashCode(); |
|
178 |
result = 31 * result + Arrays.hashCode(argTypes); |
|
179 |
return result; |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
183 |
public String toString() { |
|
184 |
return String.format("MethodTypeDesc[%s]", displayDescriptor()); |
|
185 |
} |
|
186 |
} |