Skip to content

Commit 4780fa5

Browse files
authored
Making annotation processing work with patterns. (openjdk#6)
1 parent 16a7f99 commit 4780fa5

File tree

3 files changed

+135
-15
lines changed

3 files changed

+135
-15
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java

+31-14
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ protected MemberEnter(Context context) {
103103
Type signature(MethodSymbol msym,
104104
List<JCTypeParameter> typarams,
105105
List<JCVariableDecl> params,
106+
List<JCVariableDecl> bindings,
106107
JCTree res,
107108
JCVariableDecl recvparam,
108109
List<JCExpression> thrown,
@@ -119,6 +120,18 @@ Type signature(MethodSymbol msym,
119120
argbuf.append(l.head.vartype.type);
120121
}
121122

123+
// Enter and bindings.
124+
ListBuffer<Type> bindingsbuf = null;
125+
126+
if (bindings != null) {
127+
bindingsbuf = new ListBuffer<>();
128+
129+
for (List<JCVariableDecl> l = bindings; l.nonEmpty(); l = l.tail) {
130+
memberEnter(l.head, env);
131+
bindingsbuf.append(l.head.vartype.type);
132+
}
133+
}
134+
122135
// Attribute result type, if one is given.
123136
Type restype = res == null ? syms.voidType : attr.attribType(res, env);
124137

@@ -147,6 +160,10 @@ Type signature(MethodSymbol msym,
147160
restype,
148161
thrownbuf.toList(),
149162
syms.methodClass);
163+
if (bindings != null) {
164+
mtype.bindingtypes = bindingsbuf.toList();
165+
}
166+
150167
mtype.recvtype = recvtype;
151168

152169
return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype);
@@ -197,14 +214,10 @@ public void visitMethodDef(JCMethodDecl tree) {
197214
DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
198215
try {
199216
// Compute the method type
200-
Type t = signature(m, tree.typarams, tree.params,
217+
Type t = signature(m, tree.typarams, tree.params, tree.bindings,
201218
tree.restype, tree.recvparam,
202219
tree.thrown,
203220
localEnv);
204-
if (t instanceof MethodType mt && m.isPattern()) {
205-
mt.bindingtypes = mt.argtypes;
206-
mt.argtypes = List.nil();
207-
}
208221
m.type = t;
209222
} finally {
210223
deferredLintHandler.setPos(prevLintPos);
@@ -222,20 +235,24 @@ public void visitMethodDef(JCMethodDecl tree) {
222235
params.append(Assert.checkNonNull(param.sym));
223236
}
224237

225-
if (m.isPattern()) {
226-
m.bindings = params.toList();
227-
m.params = List.nil();
228-
tree.bindings = tree.params;
229-
tree.params = List.nil();
230-
} else {
231-
m.params = params.toList();
232-
m.bindings = List.nil();
233-
}
238+
m.params = params.toList();
234239

235240
// mark the method varargs, if necessary
236241
if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
237242
m.flags_field |= Flags.VARARGS;
238243

244+
// Set m.bindings
245+
ListBuffer<VarSymbol> bindings = new ListBuffer<>();
246+
247+
if (tree.bindings != null) {
248+
for (List<JCVariableDecl> l = tree.bindings; l.nonEmpty(); l = l.tail) {
249+
JCVariableDecl binding = l.head;
250+
bindings.append(Assert.checkNonNull(binding.sym));
251+
}
252+
}
253+
254+
m.bindings = bindings.toList();
255+
239256
localEnv.info.scope.leave();
240257
if (chk.checkUnique(tree.pos(), m, enclScope)) {
241258
enclScope.enter(m);

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -5086,10 +5086,16 @@ protected JCTree methodDeclaratorRest(int pos,
50865086
}
50875087
}
50885088

5089+
boolean isPattern = (mods.flags & PATTERN) != 0;
50895090
JCMethodDecl result =
50905091
toP(F.at(pos).MethodDef(mods, name, type, typarams,
5091-
receiverParam, params, thrown,
5092+
receiverParam, isPattern ? List.nil() : params, thrown,
50925093
body, defaultValue));
5094+
5095+
if (isPattern) {
5096+
result.bindings = params;
5097+
}
5098+
50935099
attach(result, dc);
50945100
return result;
50955101
} finally {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2024, 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+
/**
25+
* @test
26+
* @summary Verify that annotation processing works with patterns.
27+
* @library /tools/lib
28+
* @modules jdk.compiler/com.sun.tools.javac.api
29+
* jdk.compiler/com.sun.tools.javac.main
30+
* @build toolbox.TestRunner toolbox.ToolBox AnnotationProcessing
31+
* @run main AnnotationProcessing
32+
*/
33+
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import java.util.Set;
38+
39+
import javax.annotation.processing.AbstractProcessor;
40+
import javax.annotation.processing.RoundEnvironment;
41+
import javax.annotation.processing.SupportedAnnotationTypes;
42+
import javax.lang.model.SourceVersion;
43+
import javax.lang.model.element.TypeElement;
44+
import toolbox.JavacTask;
45+
import toolbox.TestRunner;
46+
import toolbox.TestRunner.Test;
47+
import toolbox.ToolBox;
48+
49+
public class AnnotationProcessing extends TestRunner {
50+
51+
public static void main(String... args) throws Exception {
52+
new AnnotationProcessing().runTests(m -> new Object[] { Paths.get(m.getName()) });
53+
}
54+
55+
private final ToolBox tb = new ToolBox();
56+
57+
public AnnotationProcessing() {
58+
super(System.err);
59+
}
60+
61+
@Test
62+
public void testErrorsAfter(Path outerBase) throws Exception {
63+
Path src = outerBase.resolve("src");
64+
tb.writeJavaFiles(src,
65+
"""
66+
public class T {
67+
public pattern T(int i) {
68+
match T(0);
69+
}
70+
}
71+
""");
72+
Path classes = outerBase.resolve("classes");
73+
Files.createDirectories(classes);
74+
new JavacTask(tb)
75+
.options("-processor", "AnnotationProcessing$P",
76+
"-processorpath", System.getProperty("test.classes"),
77+
"--enable-preview", "--source", System.getProperty("java.specification.version"))
78+
.outdir(classes.toString())
79+
.files(tb.findJavaFiles(src))
80+
.run()
81+
.writeAll();
82+
}
83+
84+
@SupportedAnnotationTypes("*")
85+
public static class P extends AbstractProcessor {
86+
@Override
87+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
88+
return false;
89+
}
90+
91+
@Override
92+
public SourceVersion getSupportedSourceVersion() {
93+
return SourceVersion.latest();
94+
}
95+
}
96+
97+
}

0 commit comments

Comments
 (0)