Skip to content

Commit

Permalink
Fix for #1231: skip problem annotations from JDT bindings for Groovy AST
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 27, 2021
1 parent 0ac8183 commit ad5c02e
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 375 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,11 +33,7 @@
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.core.runtime.IPath;
import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -93,13 +89,15 @@ public void testFieldInitializerIsAvailable4() throws Exception {

@Test
public void testClosureReturner() throws Exception {
IProject project = createSimpleGroovyProject().getProject();
IPath src = createEmptyGroovyProject();
//@formatter:off
env.addGroovyClass(project.getFullPath().append("src"), "p1", "Hello2",
"class C { def aaa = { 123 } }");
GroovyCompilationUnit unit = env.getUnit(env.addGroovyClass(src, "p", "C",
"package p\n" +
"class C {\n" +
" def aaa = { 123 }\n" +
"}\n"
));
//@formatter:on
IFile javaFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("Project/src/p1/Hello2.groovy"));
GroovyCompilationUnit unit = (GroovyCompilationUnit) JavaCore.createCompilationUnitFrom(javaFile);
ClassNode inClass = unit.getModuleNode().getClasses().get(0);
FieldNode field = inClass.getField("aaa");
Expression initialExpression = field.getInitialExpression();
Expand All @@ -112,8 +110,7 @@ public void testClosureReturner() throws Exception {
//--------------------------------------------------------------------------

private Expression findFieldInitializer(String contents, Class<? extends Expression> expressionClass) throws Exception {
IProject project = createSimpleGroovyProject().getProject();
GroovyCompilationUnit unit = env.getUnit(env.addGroovyClass(project.getFullPath().append("src"), "p", "C", contents));
GroovyCompilationUnit unit = env.getUnit(env.addGroovyClass(createEmptyGroovyProject(), "p", "C", contents));

ClassNode clazz = unit.getModuleNode().getClasses().get(0);
FieldNode field = clazz.getField("aField");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@

public abstract class GroovyTypeRootTestSuite extends BuilderTestSuite {

protected final IFile createProject(boolean isGroovy) throws Exception {
private IFile createProject(boolean isGroovy) throws Exception {
IPath projectPath = env.addProject("Project");
if (!isGroovy) env.removeGroovyNature("Project");

Expand All @@ -31,8 +31,8 @@ protected final IFile createProject(boolean isGroovy) throws Exception {
if (isGroovy) {
env.addGroovyJars(projectPath);
//@formatter:off
path = env.addGroovyClass(path, "p1", "Hello",
"package p1;\n" +
path = env.addGroovyClass(path, "p", "Hello",
"package p\n" +
"public class Hello {\n" +
" static def main(String[] args) {\n" +
" print 'Hello world'\n" +
Expand All @@ -42,6 +42,7 @@ protected final IFile createProject(boolean isGroovy) throws Exception {
}

fullBuild(projectPath);

return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
}

Expand All @@ -62,34 +63,34 @@ protected final IPath createEmptyGroovyProject() throws Exception {
}

protected final IPath createAnnotationGroovyProject() throws Exception {
IPath root = createEmptyGroovyProject();
IPath src = createEmptyGroovyProject();

//@formatter:off
env.addClass(root, "p", "Anno1.java",
env.addClass(src, "p", "Anno1.java",
"package p;\n" +
"import java.lang.annotation.*;\n" +
"@Retention(RetentionPolicy.RUNTIME)\n" +
"@interface Anno1 { Class<?> value(); }\n");
env.addClass(root, "p", "Anno2.java",
env.addClass(src, "p", "Anno2.java",
"package p;\n" +
"import java.lang.annotation.*;\n" +
"@Retention(RetentionPolicy.RUNTIME)\n" +
"@interface Anno2 { }\n");
env.addClass(root, "p", "Anno3.java",
env.addClass(src, "p", "Anno3.java",
"package p;\n" +
"import java.lang.annotation.*;\n" +
"@Retention(RetentionPolicy.RUNTIME)\n" +
"@interface Anno3 { String value(); }\n");
env.addClass(root, "p", "Anno4.java",
env.addClass(src, "p", "Anno4.java",
"package p;\n" +
"import java.lang.annotation.*;\n" +
"@Retention(RetentionPolicy.RUNTIME)\n" +
"@interface Anno4 { Class<?> value1(); }\n");
env.addClass(root, "p", "Target.java",
env.addClass(src, "p", "Target.java",
"package p;\n" +
"class Target { }");
//@formatter:on

return root;
return src;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,8 @@
import static org.junit.Assert.assertTrue;

import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.groovy.tests.builder.BuilderTestSuite;
import org.junit.Test;

Expand Down Expand Up @@ -132,7 +129,6 @@ private GroovyCompilationUnit createSimpleGroovyProject(String pack, String cont

IPath root = env.getPackageFragmentRootPath(projectPath, "src");
IPath path = env.addGroovyClass(root, "Groovy", contents);
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
return (GroovyCompilationUnit) JavaCore.createCompilationUnitFrom(file);
return env.getUnit(path);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -2379,7 +2379,7 @@ private int getModifiers(FieldNode node) {
}

private int getModifiers(MethodNode node) {
int modifiers = node.getModifiers() & ~(Flags.AccSynthetic | Flags.AccTransient);
int modifiers = node.getModifiers() & ~(Flags.AccSynthetic | Flags.AccTransient); // GRECLIPSE-370, GROOVY-10140
if (node.isDefault()) {
modifiers |= Flags.AccDefaultMethod;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -141,7 +141,7 @@ private Expression createExpressionFor(TypeBinding b, Object value) {
}

if (b.isAnnotationType()) {
return new AnnotationConstantExpression(new JDTAnnotationNode((AnnotationBinding) value, resolver));
return new AnnotationConstantExpression(resolver.convertToAnnotationNode((AnnotationBinding) value));
}

if (b.isEnum()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ private ConstructorNode constructorBindingToConstructorNode(final MethodBinding

ConstructorNode ctorNode = new ConstructorNode(methodBinding.modifiers, parameters, exceptions, null);
for (AnnotationBinding annotationBinding : methodBinding.getAnnotations()) {
ctorNode.addAnnotation(new JDTAnnotationNode(annotationBinding, resolver));
ctorNode.addAnnotation(resolver.convertToAnnotationNode(annotationBinding));
}
ctorNode.setGenericsTypes(new JDTClassNodeBuilder(resolver).configureTypeVariables(methodBinding.typeVariables()));
ctorNode.putNodeMetaData("JdtBinding", methodBinding);
Expand Down Expand Up @@ -495,7 +495,7 @@ private Parameter[] makeParameters(final TypeBinding[] parameterTypes, final cha
parameters[i] = makeParameter(parameterTypes[i], parameterName);
if (parameterAnnotations != null && parameterAnnotations.length > i) {
for (AnnotationBinding annotationBinding : parameterAnnotations[i]) {
parameters[i].addAnnotation(new JDTAnnotationNode(annotationBinding, resolver));
parameters[i].addAnnotation(resolver.convertToAnnotationNode(annotationBinding));
}
}
}
Expand All @@ -515,7 +515,7 @@ public List<AnnotationNode> getAnnotations() {
long tagBits = ((SourceTypeBinding) jdtBinding).getAnnotationTagBits();
}
for (AnnotationBinding annotationBinding : jdtBinding.getAnnotations()) {
addAnnotation(new JDTAnnotationNode(annotationBinding, resolver));
addAnnotation(resolver.convertToAnnotationNode(annotationBinding));
}
bits |= ANNOTATIONS_INITIALIZED;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -58,7 +58,7 @@ private void ensureAnnotationsInitialized() {
// if the declaring entity for the member is not a SourceTypeBinding
// then the annotations will have already been discarded/lost
for (AnnotationBinding annotationBinding : fieldBinding.getAnnotations()) {
super.addAnnotation(new JDTAnnotationNode(annotationBinding, resolver));
super.addAnnotation(resolver.convertToAnnotationNode(annotationBinding));
}
}
bits |= ANNOTATIONS_INITIALIZED;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ private void ensureAnnotationsInitialized() {
// if the declaring entity for the member is not a SourceTypeBinding
// then the annotations will have already been discarded/lost
for (AnnotationBinding annotationBinding : methodBinding.getAnnotations()) {
super.addAnnotation(new JDTAnnotationNode(annotationBinding, resolver));
super.addAnnotation(resolver.convertToAnnotationNode(annotationBinding));
}
bits |= ANNOTATIONS_INITIALIZED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;
import java.util.Set;

import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ImportNode;
Expand All @@ -39,6 +40,7 @@
import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
Expand Down Expand Up @@ -460,8 +462,20 @@ protected boolean resolveToInner(ClassNode type) {
return false;
}

//--------------------------------------------------------------------------

/**
* Converts a JDT {@code AnnotationBinding} to a Groovy {@code AnnotationNode}.
*/
protected AnnotationNode convertToAnnotationNode(AnnotationBinding jdtBinding) {
if (jdtBinding == null || jdtBinding.getAnnotationType().problemId() != 0) {
return null;
}
return new JDTAnnotationNode(jdtBinding, this);
}

/**
* Converts a JDT TypeBinding to a Groovy ClassNode.
* Converts a JDT {@code TypeBinding} to a Groovy {@code ClassNode}.
*/
protected ClassNode convertToClassNode(TypeBinding jdtBinding) {
ClassNode existingNode = checkForExisting(jdtBinding);
Expand Down
4 changes: 2 additions & 2 deletions extras/groovy-eclipse-batch-builder/build.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# version numbers
version2.5=2.5.14-02
version3.0=3.0.8-01
version2.5=2.5.14-03
version3.0=3.0.8-02
version4.0=4.0.0-01

# uncomment to do a particular build -- only one should be uncommented at a time
Expand Down

0 comments on commit ad5c02e

Please sign in to comment.