Skip to content

Commit 609c910

Browse files
committedOct 21, 2020
Fix for NPE during post-save clean-ups
at org.codehaus.groovy.classgen.InnerClassCompletionVisitor.addThisReference(InnerClassCompletionVisitor.java) at org.codehaus.groovy.classgen.InnerClassCompletionVisitor.visitConstructor(InnerClassCompletionVisitor.java) at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java) at org.codehaus.groovy.classgen.InnerClassCompletionVisitor.visitClass(InnerClassCompletionVisitor.java) ... at org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.processToPhase(GroovyCompilationUnitDeclaration.java) at org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.generateCode(GroovyCompilationUnitDeclaration.java) at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java) at org.eclipse.jdt.core.dom.CompilationUnitResolver.resolve(CompilationUnitResolver.java) at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java) at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java) at org.codehaus.groovy.eclipse.refactoring.actions.CleanUpPostSaveListener.createAst(CleanUpPostSaveListener.java) at org.codehaus.groovy.eclipse.refactoring.actions.CleanUpPostSaveListener.saved(CleanUpPostSaveListener.java) at org.codehaus.groovy.eclipse.refactoring.actions.DelegatingCleanUpPostSaveListener.saved(DelegatingCleanUpPostSaveListener.java) ...
1 parent e5bc605 commit 609c910

File tree

7 files changed

+298
-11
lines changed

7 files changed

+298
-11
lines changed
 

‎base/org.codehaus.groovy25/.checkstyle

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<file-match-pattern match-pattern="groovy/classgen/AsmClassGenerator.java" include-pattern="false" />
4242
<file-match-pattern match-pattern="groovy/classgen/EnumVisitor.java" include-pattern="false" />
4343
<file-match-pattern match-pattern="groovy/classgen/ExtendedVerifier.java" include-pattern="false" />
44+
<file-match-pattern match-pattern="groovy/classgen/InnerClassVisitorHelper.java" include-pattern="false" />
4445
<file-match-pattern match-pattern="groovy/classgen/Verifier.java" include-pattern="false" />
4546
<file-match-pattern match-pattern="groovy/classgen/asm/BinaryExpressionHelper.java" include-pattern="false" />
4647
<file-match-pattern match-pattern="groovy/classgen/asm/BytecodeHelper.java" include-pattern="false" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.groovy.classgen;
20+
21+
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
22+
import org.codehaus.groovy.ast.ClassHelper;
23+
import org.codehaus.groovy.ast.ClassNode;
24+
import org.codehaus.groovy.ast.FieldNode;
25+
import org.codehaus.groovy.ast.InnerClassNode;
26+
import org.codehaus.groovy.ast.Parameter;
27+
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
28+
import org.codehaus.groovy.ast.expr.BinaryExpression;
29+
import org.codehaus.groovy.ast.expr.ConstantExpression;
30+
import org.codehaus.groovy.ast.expr.Expression;
31+
import org.codehaus.groovy.ast.expr.FieldExpression;
32+
import org.codehaus.groovy.ast.expr.GStringExpression;
33+
import org.codehaus.groovy.ast.expr.MethodCallExpression;
34+
import org.codehaus.groovy.ast.expr.PropertyExpression;
35+
import org.codehaus.groovy.ast.expr.SpreadExpression;
36+
import org.codehaus.groovy.ast.expr.VariableExpression;
37+
import org.codehaus.groovy.ast.stmt.BlockStatement;
38+
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
39+
import org.codehaus.groovy.ast.stmt.ReturnStatement;
40+
import org.codehaus.groovy.syntax.Token;
41+
import org.codehaus.groovy.syntax.Types;
42+
import groovyjarjarasm.asm.Opcodes;
43+
44+
import java.util.ArrayList;
45+
import java.util.List;
46+
47+
public abstract class InnerClassVisitorHelper extends ClassCodeVisitorSupport {
48+
49+
protected static void setPropertyGetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) {
50+
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
51+
gStringStrings.add(new ConstantExpression(""));
52+
gStringStrings.add(new ConstantExpression(""));
53+
List<Expression> gStringValues = new ArrayList<Expression>();
54+
gStringValues.add(new VariableExpression(parameters[0]));
55+
block.addStatement(
56+
new ReturnStatement(
57+
new PropertyExpression(
58+
thiz,
59+
new GStringExpression("$name", gStringStrings, gStringValues)
60+
)
61+
)
62+
);
63+
}
64+
65+
protected static void setPropertySetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) {
66+
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
67+
gStringStrings.add(new ConstantExpression(""));
68+
gStringStrings.add(new ConstantExpression(""));
69+
List<Expression> gStringValues = new ArrayList<Expression>();
70+
gStringValues.add(new VariableExpression(parameters[0]));
71+
block.addStatement(
72+
new ExpressionStatement(
73+
new BinaryExpression(
74+
new PropertyExpression(
75+
thiz,
76+
new GStringExpression("$name", gStringStrings, gStringValues)
77+
),
78+
Token.newSymbol(Types.ASSIGN, -1, -1),
79+
new VariableExpression(parameters[1])
80+
)
81+
)
82+
);
83+
}
84+
85+
protected static void setMethodDispatcherCode(BlockStatement block, Expression thiz, Parameter[] parameters) {
86+
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
87+
gStringStrings.add(new ConstantExpression(""));
88+
gStringStrings.add(new ConstantExpression(""));
89+
List<Expression> gStringValues = new ArrayList<Expression>();
90+
gStringValues.add(new VariableExpression(parameters[0]));
91+
block.addStatement(
92+
new ReturnStatement(
93+
new MethodCallExpression(
94+
thiz,
95+
new GStringExpression("$name", gStringStrings, gStringValues),
96+
new ArgumentListExpression(
97+
new SpreadExpression(new VariableExpression(parameters[1]))
98+
)
99+
)
100+
)
101+
);
102+
}
103+
104+
protected static boolean isStatic(InnerClassNode node) {
105+
return node.getDeclaredField("this$0") == null;
106+
}
107+
108+
protected static ClassNode getClassNode(ClassNode node, boolean isStatic) {
109+
if (isStatic) node = ClassHelper.CLASS_Type;
110+
return node;
111+
}
112+
113+
protected static int getObjectDistance(ClassNode node) {
114+
int count = 0;
115+
while (node != null && node != ClassHelper.OBJECT_TYPE) {
116+
count++;
117+
node = node.getSuperClass();
118+
}
119+
return count;
120+
}
121+
122+
protected static void addFieldInit(Parameter p, FieldNode fn, BlockStatement block) {
123+
VariableExpression ve = new VariableExpression(p);
124+
FieldExpression fe = new FieldExpression(fn);
125+
block.addStatement(new ExpressionStatement(
126+
new BinaryExpression(fe, Token.newSymbol(Types.ASSIGN, -1, -1), ve)
127+
));
128+
}
129+
130+
protected static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) {
131+
/* GRECLIPSE edit
132+
if (cn.isEnum() || cn.isInterface()) return false;
133+
if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false;
134+
135+
if (!(cn instanceof InnerClassNode)) return false;
136+
InnerClassNode innerClass = (InnerClassNode) cn;
137+
// scope != null means aic, we don't handle that here
138+
if (innerClass.getVariableScope() != null) return false;
139+
// static inner classes don't need this$0
140+
return (innerClass.getModifiers() & Opcodes.ACC_STATIC) == 0;
141+
*/
142+
final int explicitOrImplicitStatic = Opcodes.ACC_STATIC | Opcodes.ACC_INTERFACE | Opcodes.ACC_ENUM;
143+
return (cn.getModifiers() & explicitOrImplicitStatic) == 0 && (cn instanceof InnerClassNode && !((InnerClassNode) cn).isAnonymous());
144+
// GRECLIPSE end
145+
}
146+
}

‎base/org.codehaus.groovy30/.checkstyle

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
<file-match-pattern match-pattern="groovy/ast/expr/ClassExpression.java" include-pattern="false" />
3636
<file-match-pattern match-pattern="groovy/ast/expr/ConstantExpression.java" include-pattern="false" />
3737
<file-match-pattern match-pattern="groovy/ast/expr/ConstructorCallExpression.java" include-pattern="false" />
38-
<file-match-pattern match-pattern="groovy/ast/expr/MethodCallExpression.java" include-pattern="false" />
3938
<file-match-pattern match-pattern="groovy/ast/expr/RangeExpression.java" include-pattern="false" />
40-
<file-match-pattern match-pattern="groovy/ast/expr/StaticMethodCallExpression.java" include-pattern="false" />
39+
<file-match-pattern match-pattern="groovy/ast/expr/(Static)?MethodCallExpression.java" include-pattern="false" />
4140
<file-match-pattern match-pattern="groovy/ast/tools/ExpressionUtils.java" include-pattern="false" />
4241
<file-match-pattern match-pattern="groovy/ast/tools/GenericsUtils.java" include-pattern="false" />
4342
<file-match-pattern match-pattern="groovy/classgen/AnnotationVisitor.java" include-pattern="false" />
4443
<file-match-pattern match-pattern="groovy/classgen/EnumVisitor.java" include-pattern="false" />
4544
<file-match-pattern match-pattern="groovy/classgen/ExtendedVerifier.java" include-pattern="false" />
45+
<file-match-pattern match-pattern="groovy/classgen/InnerClassVisitorHelper.java" include-pattern="false" />
4646
<file-match-pattern match-pattern="groovy/classgen/Verifier.java" include-pattern="false" />
4747
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticInvocationWriter.java" include-pattern="false" />
4848
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.groovy.classgen;
20+
21+
import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
22+
import org.codehaus.groovy.ast.ClassHelper;
23+
import org.codehaus.groovy.ast.ClassNode;
24+
import org.codehaus.groovy.ast.FieldNode;
25+
import org.codehaus.groovy.ast.InnerClassNode;
26+
import org.codehaus.groovy.ast.Parameter;
27+
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
28+
import org.codehaus.groovy.ast.expr.BinaryExpression;
29+
import org.codehaus.groovy.ast.expr.ConstantExpression;
30+
import org.codehaus.groovy.ast.expr.Expression;
31+
import org.codehaus.groovy.ast.expr.GStringExpression;
32+
import org.codehaus.groovy.ast.expr.MethodCallExpression;
33+
import org.codehaus.groovy.ast.expr.PropertyExpression;
34+
import org.codehaus.groovy.ast.expr.SpreadExpression;
35+
import org.codehaus.groovy.ast.expr.VariableExpression;
36+
import org.codehaus.groovy.ast.stmt.BlockStatement;
37+
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
38+
import org.codehaus.groovy.ast.stmt.ReturnStatement;
39+
import org.codehaus.groovy.syntax.Token;
40+
import org.codehaus.groovy.syntax.Types;
41+
import groovyjarjarasm.asm.Opcodes;
42+
43+
import java.util.ArrayList;
44+
import java.util.List;
45+
46+
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
47+
import static org.codehaus.groovy.ast.tools.GeneralUtils.fieldX;
48+
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
49+
50+
public abstract class InnerClassVisitorHelper extends ClassCodeVisitorSupport {
51+
52+
protected static void setPropertyGetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) {
53+
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
54+
gStringStrings.add(new ConstantExpression(""));
55+
gStringStrings.add(new ConstantExpression(""));
56+
List<Expression> gStringValues = new ArrayList<Expression>();
57+
gStringValues.add(new VariableExpression(parameters[0]));
58+
block.addStatement(
59+
new ReturnStatement(
60+
new PropertyExpression(
61+
thiz,
62+
new GStringExpression("$name", gStringStrings, gStringValues)
63+
)
64+
)
65+
);
66+
}
67+
68+
protected static void setPropertySetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) {
69+
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
70+
gStringStrings.add(new ConstantExpression(""));
71+
gStringStrings.add(new ConstantExpression(""));
72+
List<Expression> gStringValues = new ArrayList<Expression>();
73+
gStringValues.add(new VariableExpression(parameters[0]));
74+
block.addStatement(
75+
new ExpressionStatement(
76+
new BinaryExpression(
77+
new PropertyExpression(
78+
thiz,
79+
new GStringExpression("$name", gStringStrings, gStringValues)
80+
),
81+
Token.newSymbol(Types.ASSIGN, -1, -1),
82+
new VariableExpression(parameters[1])
83+
)
84+
)
85+
);
86+
}
87+
88+
protected static void setMethodDispatcherCode(BlockStatement block, Expression thiz, Parameter[] parameters) {
89+
List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
90+
gStringStrings.add(new ConstantExpression(""));
91+
gStringStrings.add(new ConstantExpression(""));
92+
List<Expression> gStringValues = new ArrayList<Expression>();
93+
gStringValues.add(new VariableExpression(parameters[0]));
94+
block.addStatement(
95+
new ReturnStatement(
96+
new MethodCallExpression(
97+
thiz,
98+
new GStringExpression("$name", gStringStrings, gStringValues),
99+
new ArgumentListExpression(
100+
new SpreadExpression(new VariableExpression(parameters[1]))
101+
)
102+
)
103+
)
104+
);
105+
}
106+
107+
protected static boolean isStatic(InnerClassNode node) {
108+
return node.getDeclaredField("this$0") == null;
109+
}
110+
111+
protected static ClassNode getClassNode(ClassNode node, boolean isStatic) {
112+
if (isStatic) node = ClassHelper.CLASS_Type;
113+
return node;
114+
}
115+
116+
protected static int getObjectDistance(ClassNode node) {
117+
int count = 0;
118+
while (node != null && node != ClassHelper.OBJECT_TYPE) {
119+
count++;
120+
node = node.getSuperClass();
121+
}
122+
return count;
123+
}
124+
125+
protected static void addFieldInit(Parameter p, FieldNode fn, BlockStatement block) {
126+
block.addStatement(assignS(fieldX(fn), varX(p)));
127+
}
128+
129+
protected static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) {
130+
/* GRECLIPSE edit
131+
if (cn.isEnum() || cn.isInterface()) return false;
132+
if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false;
133+
134+
if (!(cn instanceof InnerClassNode)) return false;
135+
InnerClassNode innerClass = (InnerClassNode) cn;
136+
// scope != null means aic, we don't handle that here
137+
if (innerClass.getVariableScope() != null) return false;
138+
// static inner classes don't need this$0
139+
return (innerClass.getModifiers() & Opcodes.ACC_STATIC) == 0;
140+
*/
141+
final int explicitOrImplicitStatic = Opcodes.ACC_STATIC | Opcodes.ACC_INTERFACE | Opcodes.ACC_ENUM;
142+
return (cn.getModifiers() & explicitOrImplicitStatic) == 0 && (cn instanceof InnerClassNode && !((InnerClassNode) cn).isAnonymous());
143+
// GRECLIPSE end
144+
}
145+
}

‎base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
import org.codehaus.groovy.runtime.MetaClassHelper;
105105
import org.codehaus.groovy.syntax.Token;
106106
import org.codehaus.groovy.syntax.Types;
107-
import org.codehaus.groovy.transform.ASTTestTransformation;
108107
import org.codehaus.groovy.transform.AnnotationCollectorTransform;
109108
import org.codehaus.groovy.transform.FieldASTTransformation;
110109
import org.codehaus.groovy.transform.sc.ListOfExpressionsExpression;
@@ -685,7 +684,7 @@ public void visitAnnotation(final AnnotationNode node) {
685684
// visit attribute values
686685
super.visitAnnotation(node);
687686

688-
ClosureExpression test = node.getNodeMetaData(ASTTestTransformation.class);
687+
ClosureExpression test = node.getNodeMetaData(org.codehaus.groovy.transform.ASTTestTransformation.class);
689688
if (test != null) {
690689
Deque<VariableScope> saved = new java.util.ArrayDeque<>(scopes);
691690

‎ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/CleanUpPostSaveListener.java

+3
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ private CompilationUnit createAst(ICompilationUnit unit, Map<String, String> cle
611611
}
612612

613613
ASTParser parser= CleanUpRefactoring.createCleanUpASTParser();
614+
// GROOVY add -- stop after semantic analysis
615+
parser.setIgnoreMethodBodies(true);
616+
// GROOVY end
614617
parser.setSource(unit);
615618

616619
Map<String, String> compilerOptions= RefactoringASTParser.getCompilerOptions(unit.getJavaProject());

‎ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/DelegatingCleanUpPostSaveListener.java

-7
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ public static void installCleanUp() {
6161
ReflectionUtils.setPrivateField(SaveParticipantDescriptor.class, "fPostSaveListener", descriptor, delegatingCleanUp);
6262
}
6363
} catch (Exception e) {
64-
// a ClassCastException can be thrown when changing compilers, so ignore it
65-
if (e instanceof ClassCastException) {
66-
if (e.getStackTrace()[0].getLineNumber() == 55) {
67-
return;
68-
}
69-
}
70-
7164
// if an exception is thrown, then the groovy post save listener will not be used.
7265
GroovyCore.logException("Exception thrown while trying to install GroovyCleanUpPostSaveListener", e);
7366
}

0 commit comments

Comments
 (0)
Please sign in to comment.