Skip to content

Commit

Permalink
Fix for #671: propagate @deprecated to content assist proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 23, 2018
1 parent d45b0d0 commit b30a69f
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.eclipse.jdt.groovy.core.tests.basic;

import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isAtLeastGroovy;
import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -3228,6 +3229,80 @@ public void testExtendingGroovyObjects_clinit() {
"success");
}

@Test
public void testGroovyPropertyAccessors1() {
runConformTest(new String[] {
"p/C.java",
"package p;\n" +
"public class C {\n"+
" public static void main(String[] argv) {\n"+
" G o = new G();\n"+
" System.out.print(o.isB());\n"+
" System.out.print(o.getB());\n"+
" }\n"+
"}\n",

"p/G.groovy",
"package p;\n"+
"public class G {\n" +
" boolean b\n"+
"}\n",
},
"falsefalse");
}

@Test
public void testGroovyPropertyAccessors2() {
runConformTest(new String[] {
"p/C.java",
"package p;\n" +
"public class C {\n"+
" public static void main(String[] argv) {\n"+
" G o = new G();\n"+
" System.out.print(o.getB());\n"+
" o.setB(true);\n"+
" System.out.print(o.getB());\n"+
" }\n"+
"}\n",

"p/G.groovy",
"package p;\n"+
"public class G {\n" +
" boolean b\n"+
"}\n",
},
"falsetrue");
}

@Test // @Deprecated should be propagated to accessors
public void testGroovyPropertyAccessors3() {
runNegativeTest(new String[] {
"p/G.groovy",
"package p;\n"+
"class G {\n" +
" @Deprecated\n"+
" boolean flag\n"+
"}\n",
},
"");

checkDisassemblyFor("p/G.class",
" @java.lang.Deprecated\n" +
" private boolean flag;\n");

checkDisassemblyFor("p/G.class",
" @java.lang.Deprecated\n" + (isAtLeastGroovy(25) ? " @groovy.transform.Generated\n" : "") +
" public boolean isFlag();\n");

checkDisassemblyFor("p/G.class",
" @java.lang.Deprecated\n" + (isAtLeastGroovy(25) ? " @groovy.transform.Generated\n" : "") +
" public boolean getFlag();\n");

checkDisassemblyFor("p/G.class",
" @java.lang.Deprecated\n" + (isAtLeastGroovy(25) ? " @groovy.transform.Generated\n" : "") +
" public void setFlag(boolean arg0);\n");
}

@Test
public void testGroovyPropertyAccessors_ErrorCases1() {
// check no duplicate created for 'String getProp'
Expand Down Expand Up @@ -3387,51 +3462,6 @@ public void testGroovyPropertyAccessors_ErrorCases6() {
"----------\n");
}

@Test
public void testGroovyPropertyAccessors() {
runConformTest(new String[] {
"p/C.java",
"package p;\n" +
"public class C {\n"+
" public static void main(String[] argv) {\n"+
" G o = new G();\n"+
" System.out.print(o.isB());\n"+
" System.out.print(o.getB());\n"+
" }\n"+
"}\n",

"p/G.groovy",
"package p;\n"+
"public class G {\n" +
" boolean b\n"+
"}\n",
},
"falsefalse");
}

@Test
public void testGroovyPropertyAccessors_Set() {
runConformTest(new String[] {
"p/C.java",
"package p;\n" +
"public class C {\n"+
" public static void main(String[] argv) {\n"+
" G o = new G();\n"+
" System.out.print(o.getB());\n"+
" o.setB(true);\n"+
" System.out.print(o.getB());\n"+
" }\n"+
"}\n",

"p/G.groovy",
"package p;\n"+
"public class G {\n" +
" boolean b\n"+
"}\n",
},
"falsetrue");
}

@Test
public void testDefaultValueMethods() {
runConformTest(new String[] {
Expand All @@ -3453,7 +3483,7 @@ public void testDefaultValueMethods() {
},
"abcabc");

String expectedOutput =
checkGCUDeclaration("G.groovy",
"package p;\n" +
"public class G {\n" +
" public G() {\n" +
Expand All @@ -3462,18 +3492,19 @@ public void testDefaultValueMethods() {
" }\n" +
" public void m(String s) {\n" +
" }\n" +
"}\n";
checkGCUDeclaration("G.groovy",expectedOutput);
expectedOutput =
"}\n");

checkDisassemblyFor("p/G.class",
" \n" +
" public void m(String s, Integer i);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
expectedOutput =
" \n",
ClassFileBytesDisassembler.COMPACT);

checkDisassemblyFor("p/G.class",
" \n" +
" public void m(String s);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
" \n",
ClassFileBytesDisassembler.COMPACT);
}

@Test
Expand All @@ -3500,7 +3531,7 @@ public void testDefaultValueMethods02() {
},
"xyz4.0xyz3.0xyz3.0xyz3.0");

String expectedOutput =
checkGCUDeclaration("G.groovy",
"package p;\n" +
"public class G {\n" +
" public G() {\n" +
Expand All @@ -3513,29 +3544,31 @@ public void testDefaultValueMethods02() {
" }\n" +
" public void m(String s, String k, String l) {\n" +
" }\n" +
"}\n";
checkGCUDeclaration("G.groovy", expectedOutput);
"}\n");

expectedOutput =
checkDisassemblyFor("p/G.class",
" \n" +
" public void m(String s, Integer i, String j, String k, float f, String l);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
expectedOutput =
" \n",
ClassFileBytesDisassembler.COMPACT);

checkDisassemblyFor("p/G.class",
" \n" +
" public void m(String s, Integer i, String j, String k, String l);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
expectedOutput =
" \n",
ClassFileBytesDisassembler.COMPACT);

checkDisassemblyFor("p/G.class",
" \n" +
" public void m(String s, Integer i, String k, String l);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
expectedOutput =
" \n",
ClassFileBytesDisassembler.COMPACT);

checkDisassemblyFor("p/G.class",
" \n" +
" public void m(String s, String k, String l);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
" \n",
ClassFileBytesDisassembler.COMPACT);
}

@Test
Expand All @@ -3562,7 +3595,7 @@ public void testDefaultValueConstructors() {
},
"abcabc");

String expectedOutput=
checkGCUDeclaration("G.groovy",
"package p;\n" +
"public class G {\n" +
" private java.lang.Object msg;\n" +
Expand All @@ -3574,18 +3607,19 @@ public void testDefaultValueConstructors() {
" }\n" +
" public void print() {\n" +
" }\n" +
"}\n";
checkGCUDeclaration("G.groovy", expectedOutput);
expectedOutput =
"}\n");

checkDisassemblyFor("p/G.class",
" \n" +
" public G(Integer i, String m);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
expectedOutput =
" \n",
ClassFileBytesDisassembler.COMPACT);

checkDisassemblyFor("p/G.class",
" \n" +
" public G(Integer i);\n" +
" \n";
checkDisassemblyFor("p/G.class", expectedOutput, ClassFileBytesDisassembler.COMPACT);
" \n",
ClassFileBytesDisassembler.COMPACT);
}

@Test
Expand Down Expand Up @@ -3655,7 +3689,6 @@ public void testCallingJavaFromGroovy1() throws Exception {
"}\n",
},
"successname");
//checkDisassembledClassFile(OUTPUT_DIR + File.separator + "p/Code.class", "Code", "");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ protected void addGroovyObjectInterfaceAndMethods(ClassNode node, final String c
new BytecodeSequence(new BytecodeInstruction() {
public void visit(MethodVisitor mv) {
Label nullLabel = new Label();
/**
/*
* the code is:
* if (this.metaClass==null) {
* this.metaClass = this.$getStaticMetaClass()
Expand Down Expand Up @@ -428,7 +428,7 @@ public void visit(MethodVisitor mv) {
List list = new ArrayList();
list.add(new BytecodeInstruction() {
public void visit(MethodVisitor mv) {
/**
/*
* the code is (meta class is stored in 1):
* this.metaClass = <1>
*/
Expand Down Expand Up @@ -677,6 +677,9 @@ public void visitProperty(PropertyNode node) {
if (getterBlock != null) {
MethodNode getter =
new MethodNode(getterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(getter::addAnnotation);
// GRECLIPSE end
getter.setSynthetic(true);
addPropertyMethod(getter);
visitMethod(getter);
Expand All @@ -685,6 +688,9 @@ public void visitProperty(PropertyNode node) {
String secondGetterName = "is" + capitalize(name);
MethodNode secondGetter =
new MethodNode(secondGetterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(secondGetter::addAnnotation);
// GRECLIPSE end
secondGetter.setSynthetic(true);
addPropertyMethod(secondGetter);
visitMethod(secondGetter);
Expand All @@ -694,6 +700,9 @@ public void visitProperty(PropertyNode node) {
Parameter[] setterParameterTypes = {new Parameter(node.getType(), "value")};
MethodNode setter =
new MethodNode(setterName, accessorModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(setter::addAnnotation);
// GRECLIPSE end
setter.setSynthetic(true);
addPropertyMethod(setter);
visitMethod(setter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,18 +755,24 @@ public void visitProperty(PropertyNode node) {
Parameter[] setterParameterTypes = {new Parameter(node.getType(), "value")};
MethodNode setter =
new MethodNode(setterName, accessorModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(setter::addAnnotation);
// GRECLIPSE end
setter.setSynthetic(true);
addPropertyMethod(setter);
visitMethod(setter);
}
}

private void visitGetter(PropertyNode node, Statement getterBlock, int getterModifiers, String secondGetterName) {
MethodNode secondGetter =
MethodNode getter =
new MethodNode(secondGetterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
secondGetter.setSynthetic(true);
addPropertyMethod(secondGetter);
visitMethod(secondGetter);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(getter::addAnnotation);
// GRECLIPSE end
getter.setSynthetic(true);
addPropertyMethod(getter);
visitMethod(getter);
}

protected void addPropertyMethod(MethodNode method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,22 +735,29 @@ public void visitProperty(PropertyNode node) {
Parameter[] setterParameterTypes = {new Parameter(node.getType(), "value")};
MethodNode setter =
new MethodNode(setterName, accessorModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(setter::addAnnotation);
// GRECLIPSE end
setter.setSynthetic(true);
addPropertyMethod(setter);
visitMethod(setter);
}
}

private void visitGetter(PropertyNode node, Statement getterBlock, int getterModifiers, String secondGetterName) {
MethodNode secondGetter =
MethodNode getter =
new MethodNode(secondGetterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
secondGetter.setSynthetic(true);
addPropertyMethod(secondGetter);
visitMethod(secondGetter);
// GRECLIPSE add
node.getField().getAnnotations().stream().filter(a -> a.getClassNode().getName().equals("java.lang.Deprecated")).forEach(getter::addAnnotation);
// GRECLIPSE end
getter.setSynthetic(true);
addPropertyMethod(getter);
visitMethod(getter);
}

protected void addPropertyMethod(MethodNode method) {
classNode.addMethod(method);
method.addAnnotation(new AnnotationNode(ClassHelper.make(GENERATED_ANNOTATION)));
// GROOVY-4415 / GROOVY-4645: check that there's no abstract method which corresponds to this one
List<MethodNode> abstractMethods = classNode.getAbstractMethods();
if (abstractMethods == null) return;
Expand Down
Loading

0 comments on commit b30a69f

Please sign in to comment.