Skip to content

Commit 6217e81

Browse files
authored
Ignore all annotations in all scopes if source is missing (#10035)
JDT supports three kinds of annotations, and the ASTVisitor lets them be encountered in two different scopes. This patch ensures that all six cases are handled, and adds a test that uses each of the six cases - removing any one of the `visit` implementations will fail the test by incorrectly reporting the annotation as not having sources available. Fixes #10020 See #10021
1 parent e030c53 commit 6217e81

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

dev/core/src/com/google/gwt/dev/javac/BinaryTypeReferenceRestrictionsChecker.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.jdt.internal.compiler.impl.Constant;
2626
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
2727
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
28+
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
2829
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
2930

3031
import java.util.ArrayList;
@@ -79,27 +80,44 @@ public boolean visit(MarkerAnnotation annotation, BlockScope scope) {
7980
return false;
8081
}
8182

83+
@Override
84+
public boolean visit(MarkerAnnotation annotation, ClassScope scope) {
85+
// Ignore annotations
86+
return false;
87+
}
88+
8289
@Override
8390
public boolean visit(NormalAnnotation annotation, BlockScope scope) {
8491
// Ignore annotations
8592
return false;
8693
}
8794

95+
@Override
96+
public boolean visit(NormalAnnotation annotation, ClassScope scope) {
97+
// Ignore annotations
98+
return false;
99+
}
100+
88101
@Override
89102
public boolean visit(SingleMemberAnnotation annotation, BlockScope scope) {
90103
// Ignore annotations
91104
return false;
92105
}
93106

107+
@Override
108+
public boolean visit(SingleMemberAnnotation annotation, ClassScope scope) {
109+
// Ignore annotations
110+
return false;
111+
}
112+
94113
@Override
95114
protected void onBinaryTypeRef(BinaryTypeBinding binding,
96115
CompilationUnitDeclaration unitOfReferrer, Expression expression) {
97116
if (expression.constant != null && expression.constant != Constant.NotAConstant) {
98117
// Allow compile time constants from classes provided only in binary form.
99118
return;
100119
}
101-
binaryTypeReferenceSites.add(new BinaryTypeReferenceSite(expression,
102-
binding));
120+
binaryTypeReferenceSites.add(new BinaryTypeReferenceSite(expression, binding));
103121
}
104122

105123
@Override
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.gwt.dev.jjs.impl;
15+
16+
import com.google.gwt.core.ext.UnableToCompleteException;
17+
import com.google.gwt.thirdparty.guava.common.base.Joiner;
18+
19+
public class AnnotationCompilerTest extends FullCompileTestBase {
20+
@Override
21+
protected void optimizeJava() {
22+
}
23+
24+
public void testUseAnnotationWithoutSource() throws UnableToCompleteException {
25+
// The JDT represents annotations as three kinds: markers, which have no members,
26+
// single-member, which have only one member, and normal, which have more than one.
27+
// This test validates that all three work in two cases each: block scope, and class
28+
// scope.
29+
30+
String annotations = Joiner.on(" ").join(
31+
"@SampleBytecodeOnlyMarkerAnnotation",
32+
"@SampleBytecodeOnlySingleMemberAnnotation(\"abc\")",
33+
"@SampleBytecodeOnlyNormalAnnotation(a=1, b=2)"
34+
);
35+
36+
String code = Joiner.on('\n').join(
37+
"package test;",
38+
"import com.google.gwt.dev.jjs.impl.*;",
39+
"public class EntryPoint<T extends " + annotations + " Object> {",
40+
" public static void onModuleLoad() {",
41+
" }",
42+
" " + annotations + " public String foo() {",
43+
" return \"\";",
44+
" }",
45+
"}");
46+
47+
compileSnippetToJS(code);
48+
}
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.gwt.dev.jjs.impl;
15+
16+
/**
17+
* Annotation with no bytecode for GWT, and no members.
18+
*/
19+
public @interface SampleBytecodeOnlyMarkerAnnotation {
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.gwt.dev.jjs.impl;
15+
16+
/**
17+
* Annotation with no bytecode for GWT, and multiple members.
18+
*/
19+
public @interface SampleBytecodeOnlyNormalAnnotation {
20+
int a();
21+
int b();
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.gwt.dev.jjs.impl;
15+
16+
/**
17+
* Annotation with no bytecode for GWT, and a single member.
18+
*/
19+
public @interface SampleBytecodeOnlySingleMemberAnnotation {
20+
String value();
21+
}

0 commit comments

Comments
 (0)