You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2020/08/18 18:09:06 UTC
[groovy] branch master updated: fix parameter generics and modifiers
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 5a3bf18 fix parameter generics and modifiers
5a3bf18 is described below
commit 5a3bf184104c65e6a4bb5d30a13a93e1629bc82e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Aug 18 13:08:48 2020 -0500
fix parameter generics and modifiers
---
.../org/codehaus/groovy/classgen/Verifier.java | 401 ++++++++++-----------
1 file changed, 189 insertions(+), 212 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 1be8f1f..6a97311 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -74,7 +74,6 @@ import org.objectweb.asm.Opcodes;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -90,17 +89,23 @@ import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import static java.util.stream.Collectors.joining;
import static org.apache.groovy.ast.tools.AnnotatedNodeUtils.markAsGenerated;
-import static org.apache.groovy.ast.tools.MethodNodeUtils.getCodeAsBlock;
import static org.apache.groovy.ast.tools.ConstructorNodeUtils.getFirstIfSpecialConstructorCall;
import static org.apache.groovy.ast.tools.ExpressionUtils.transformInlineConstants;
+import static org.apache.groovy.ast.tools.MethodNodeUtils.getCodeAsBlock;
import static org.apache.groovy.ast.tools.MethodNodeUtils.getPropertyName;
import static org.apache.groovy.ast.tools.MethodNodeUtils.methodDescriptorWithoutReturnType;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.block;
import static org.codehaus.groovy.ast.tools.GeneralUtils.bytecodeX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.constX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.declS;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.fieldX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName;
import static org.codehaus.groovy.ast.tools.GeneralUtils.localVarX;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.codehaus.groovy.ast.tools.GenericsUtils.addMethodGenerics;
import static org.codehaus.groovy.ast.tools.GenericsUtils.correctToGenericsSpec;
@@ -130,22 +135,19 @@ import static org.codehaus.groovy.ast.tools.PropertyNodeUtils.adjustPropertyModi
*/
public class Verifier implements GroovyClassVisitor, Opcodes {
- public static final String STATIC_METACLASS_BOOL = "__$stMC";
public static final String SWAP_INIT = "__$swapInit";
+ public static final String STATIC_METACLASS_BOOL = "__$stMC";
public static final String INITIAL_EXPRESSION = "INITIAL_EXPRESSION";
public static final String DEFAULT_PARAMETER_GENERATED = "DEFAULT_PARAMETER_GENERATED";
-
- // NOTE: timeStamp constants shouldn't belong to Verifier but kept here
- // for binary compatibility
- public static final String __TIMESTAMP = "__timeStamp";
- public static final String __TIMESTAMP__ = "__timeStamp__239_neverHappen";
- private static final Parameter[] SET_METACLASS_PARAMS = new Parameter[]{
- new Parameter(ClassHelper.METACLASS_TYPE, "mc")
- };
+ private static final Parameter[] SET_METACLASS_PARAMS = {new Parameter(ClassHelper.METACLASS_TYPE, "mc")};
private static final Class<?> GENERATED_ANNOTATION = Generated.class;
private static final Class<?> INTERNAL_ANNOTATION = Internal.class;
+ // NOTE: timeStamp constants shouldn't belong to Verifier but kept here for binary compatibility
+ public static final String __TIMESTAMP = "__timeStamp";
+ public static final String __TIMESTAMP__ = "__timeStamp__239_neverHappen";
+
private ClassNode classNode;
private MethodNode methodNode;
@@ -153,7 +155,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return classNode;
}
- protected void setClassNode(ClassNode classNode) {
+ protected void setClassNode(final ClassNode classNode) {
this.classNode = classNode;
}
@@ -161,7 +163,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return methodNode;
}
- private static FieldNode setMetaClassFieldIfNotExists(ClassNode node, FieldNode metaClassField) {
+ private static FieldNode setMetaClassFieldIfNotExists(final ClassNode node, FieldNode metaClassField) {
if (metaClassField != null) return metaClassField;
final String classInternalName = BytecodeHelper.getClassInternalName(node);
metaClassField =
@@ -175,7 +177,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return metaClassField;
}
- private static FieldNode getMetaClassField(ClassNode node) {
+ private static FieldNode getMetaClassField(final ClassNode node) {
FieldNode ret = node.getDeclaredField("metaClass");
if (ret != null) {
ClassNode mcFieldType = ret.getType();
@@ -216,11 +218,11 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
ClassNode[] classNodes = classNode.getInterfaces();
- List<String> interfaces = new ArrayList<String>();
+ List<String> interfaces = new ArrayList<>();
for (ClassNode classNode : classNodes) {
interfaces.add(classNode.getName());
}
- Set<String> interfaceSet = new HashSet<String>(interfaces);
+ Set<String> interfaceSet = new HashSet<>(interfaces);
if (interfaceSet.size() != interfaces.size()) {
throw new RuntimeParserException("Duplicate interfaces in implements list: " + interfaces, classNode);
}
@@ -251,16 +253,15 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
checkFinalVariables(node);
}
- private void checkFinalVariables(ClassNode node) {
- FinalVariableAnalyzer analyzer = new FinalVariableAnalyzer(null, getFinalVariablesCallback());
- analyzer.visitClass(node);
-
+ private void checkFinalVariables(final ClassNode node) {
+ GroovyClassVisitor visitor = new FinalVariableAnalyzer(null, getFinalVariablesCallback());
+ visitor.visitClass(node);
}
protected FinalVariableAnalyzer.VariableNotFinalCallback getFinalVariablesCallback() {
return new FinalVariableAnalyzer.VariableNotFinalCallback() {
@Override
- public void variableNotFinal(Variable var, Expression bexp) {
+ public void variableNotFinal(Variable var, final Expression bexp) {
if (var instanceof VariableExpression) {
var = ((VariableExpression) var).getAccessedVariable();
}
@@ -280,8 +281,8 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
};
}
- private static void checkForDuplicateMethods(ClassNode cn) {
- Set<String> descriptors = new HashSet<String>();
+ private static void checkForDuplicateMethods(final ClassNode cn) {
+ Set<String> descriptors = new HashSet<>();
for (MethodNode mn : cn.getMethods()) {
if (mn.isSynthetic()) continue;
String mySig = methodDescriptorWithoutReturnType(mn);
@@ -298,14 +299,14 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- private static String scriptBodySignatureWithoutReturnType(ClassNode cn) {
+ private static String scriptBodySignatureWithoutReturnType(final ClassNode cn) {
for (MethodNode mn : cn.getMethods()) {
if (mn.isScriptBody()) return methodDescriptorWithoutReturnType(mn);
}
return null;
}
- private static FieldNode checkFieldDoesNotExist(ClassNode node, String fieldName) {
+ private static FieldNode checkFieldDoesNotExist(final ClassNode node, final String fieldName) {
FieldNode ret = node.getDeclaredField(fieldName);
if (ret != null) {
if (isPublic(ret.getModifiers()) &&
@@ -319,7 +320,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return null;
}
- private static void addFastPathHelperFieldsAndHelperMethod(ClassNode node, final String classInternalName, boolean knownSpecialCase) {
+ private static void addFastPathHelperFieldsAndHelperMethod(final ClassNode node, final String classInternalName, final boolean knownSpecialCase) {
if (node.getNodeMetaData(ClassNodeSkip.class) != null) return;
FieldNode stMCB = checkFieldDoesNotExist(node, STATIC_METACLASS_BOOL);
if (stMCB == null) {
@@ -331,7 +332,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- protected void addDefaultConstructor(ClassNode node) {
+ protected void addDefaultConstructor(final ClassNode node) {
if (!node.getDeclaredConstructors().isEmpty()) return;
ConstructorNode constructor = new ConstructorNode(ACC_PUBLIC, new BlockStatement());
@@ -357,7 +358,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
ClassNode.EMPTY_ARRAY,
new BytecodeSequence(new BytecodeInstruction() {
@Override
- public void visit(MethodVisitor mv) {
+ public void visit(final MethodVisitor mv) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
if (BytecodeHelper.isClassLiteralPossible(node) || BytecodeHelper.isSameCompilationUnit(classNode, node)) {
@@ -397,7 +398,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
);
}
- protected void addGroovyObjectInterfaceAndMethods(ClassNode node, final String classInternalName) {
+ protected void addGroovyObjectInterfaceAndMethods(final ClassNode node, final String classInternalName) {
if (!node.isDerivedFromGroovyObject()) node.addInterface(ClassHelper.make(GroovyObject.class));
FieldNode metaClassField = getMetaClassField(node);
@@ -415,7 +416,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
ClassNode.EMPTY_ARRAY,
new BytecodeSequence(new BytecodeInstruction() {
@Override
- public void visit(MethodVisitor mv) {
+ public void visit(final MethodVisitor mv) {
Label nullLabel = new Label();
/*
* the code is:
@@ -464,7 +465,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
} else {
setMetaClassCode = new BytecodeSequence(new BytecodeInstruction() {
@Override
- public void visit(MethodVisitor mv) {
+ public void visit(final MethodVisitor mv) {
/*
* the code is (meta class is stored in 1):
* this.metaClass = <1>
@@ -496,8 +497,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
* call will either be made to ClassNode.addSyntheticMethod() or ClassNode.addMethod(). If a non-synthetic method
* is to be added the ACC_SYNTHETIC modifier is removed if it has been accidentally supplied.
*/
- protected MethodNode addMethod(ClassNode node, boolean shouldBeSynthetic, String name, int modifiers, ClassNode returnType, Parameter[] parameters,
- ClassNode[] exceptions, Statement code) {
+ protected MethodNode addMethod(final ClassNode node, final boolean shouldBeSynthetic, final String name, final int modifiers, final ClassNode returnType, final Parameter[] parameters, final ClassNode[] exceptions, final Statement code) {
if (shouldBeSynthetic) {
return node.addSyntheticMethod(name, modifiers, returnType, parameters, exceptions, code);
} else {
@@ -506,23 +506,22 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
// for binary compatibility only, don't use or override this
- protected void addMethod$$bridge(ClassNode node, boolean shouldBeSynthetic, String name, int modifiers, ClassNode returnType, Parameter[] parameters,
- ClassNode[] exceptions, Statement code) {
+ protected void addMethod$$bridge(final ClassNode node, final boolean shouldBeSynthetic, final String name, final int modifiers, final ClassNode returnType, final Parameter[] parameters, final ClassNode[] exceptions, final Statement code) {
addMethod(node, shouldBeSynthetic, name, modifiers, returnType, parameters, exceptions, code);
}
@Deprecated
- protected void addTimeStamp(ClassNode node) {
+ protected void addTimeStamp(final ClassNode node) {
}
- private static void checkReturnInObjectInitializer(List<Statement> init) {
+ private static void checkReturnInObjectInitializer(final List<Statement> init) {
GroovyCodeVisitor visitor = new CodeVisitorSupport() {
@Override
- public void visitClosureExpression(ClosureExpression expression) {
+ public void visitClosureExpression(final ClosureExpression expression) {
// return is OK in closures in object initializers
}
@Override
- public void visitReturnStatement(ReturnStatement statement) {
+ public void visitReturnStatement(final ReturnStatement statement) {
throw new RuntimeParserException("'return' is not allowed in object initializer", statement);
}
};
@@ -532,14 +531,14 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
@Override
- public void visitConstructor(ConstructorNode node) {
+ public void visitConstructor(final ConstructorNode node) {
Statement stmt = node.getCode();
if (stmt != null) {
stmt.visit(new VerifierCodeVisitor(getClassNode()));
// check for uninitialized-this references
stmt.visit(new CodeVisitorSupport() {
@Override
- public void visitClosureExpression(ClosureExpression ce) {
+ public void visitClosureExpression(final ClosureExpression ce) {
boolean oldInClosure = inClosure;
inClosure = true;
super.visitClosureExpression(ce);
@@ -547,7 +546,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
@Override
- public void visitConstructorCallExpression(ConstructorCallExpression cce) {
+ public void visitConstructorCallExpression(final ConstructorCallExpression cce) {
boolean oldIsSpecialConstructorCall = inSpecialConstructorCall;
inSpecialConstructorCall |= cce.isSpecialCall();
super.visitConstructorCallExpression(cce);
@@ -555,7 +554,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
@Override
- public void visitMethodCallExpression(MethodCallExpression mce) {
+ public void visitMethodCallExpression(final MethodCallExpression mce) {
if (inSpecialConstructorCall && isThisObjectExpression(mce)) {
MethodNode methodTarget = mce.getMethodTarget();
if (methodTarget == null || !(methodTarget.isStatic() || classNode.getOuterClasses().contains(methodTarget.getDeclaringClass()))) {
@@ -573,7 +572,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
@Override
- public void visitVariableExpression(VariableExpression ve) {
+ public void visitVariableExpression(final VariableExpression ve) {
// before this/super ctor call completes, only params and static or outer members are accessible
if (inSpecialConstructorCall && (ve.isThisExpression() || ve.isSuperExpression() || isNonStaticMemberAccess(ve))) {
throw newVariableError(ve.getName(), ve.getLineNumber() > 0 ? ve : node); // TODO: context for default argument
@@ -584,13 +583,13 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
private boolean inClosure, inSpecialConstructorCall;
- private boolean isNonStaticMemberAccess(VariableExpression ve) {
+ private boolean isNonStaticMemberAccess(final VariableExpression ve) {
Variable variable = ve.getAccessedVariable();
return !inClosure && variable != null && !isStatic(variable.getModifiers())
&& !(variable instanceof DynamicVariable) && !(variable instanceof Parameter);
}
- private boolean isThisObjectExpression(MethodCallExpression mce) {
+ private boolean isThisObjectExpression(final MethodCallExpression mce) {
if (mce.isImplicitThis()) {
return true;
} else if (mce.getObjectExpression() instanceof VariableExpression) {
@@ -601,7 +600,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- private GroovyRuntimeException newVariableError(String name, ASTNode node) {
+ private GroovyRuntimeException newVariableError(final String name, final ASTNode node) {
RuntimeParserException rpe = new RuntimeParserException("Cannot reference '" + name +
"' before supertype constructor has been called. Possible causes:\n" +
"You attempted to access an instance field, method, or property.\n" +
@@ -614,8 +613,8 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
@Override
- public void visitMethod(MethodNode node) {
- // GROOVY-3712 - if it's an MOP method, it's an error as they aren't supposed to exist before ACG is invoked
+ public void visitMethod(final MethodNode node) {
+ // GROOVY-3712: if it's an MOP method, it's an error as they aren't supposed to exist before ACG is invoked
if (MopWriter.isMopMethod(node.getName())) {
throw new RuntimeParserException("Found unexpected MOP methods in the class node for " + classNode.getName() + "(" + node.getName() + ")", classNode);
}
@@ -630,7 +629,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- private static void adjustTypesIfStaticMainMethod(MethodNode node) {
+ private static void adjustTypesIfStaticMainMethod(final MethodNode node) {
if (node.getName().equals("main") && node.isStatic()) {
Parameter[] params = node.getParameters();
if (params.length == 1) {
@@ -646,16 +645,16 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- protected void addReturnIfNeeded(MethodNode node) {
+ protected void addReturnIfNeeded(final MethodNode node) {
ReturnAdder adder = new ReturnAdder();
adder.visitMethod(node);
}
@Override
- public void visitField(FieldNode node) {
+ public void visitField(final FieldNode node) {
}
- private boolean methodNeedsReplacement(MethodNode m) {
+ private boolean methodNeedsReplacement(final MethodNode m) {
// no method found, we need to replace
if (m == null) return true;
// method is in current class, nothing to be done
@@ -666,7 +665,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
@Override
- public void visitProperty(PropertyNode node) {
+ public void visitProperty(final PropertyNode node) {
String name = node.getName();
FieldNode field = node.getField();
@@ -698,35 +697,32 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
int getterModifiers = accessorModifiers;
// don't make static accessors final
if (node.isStatic()) {
- getterModifiers = ~ACC_FINAL & getterModifiers;
+ getterModifiers &= ~ACC_FINAL;
}
if (getterBlock != null) {
visitGetter(node, getterBlock, getterModifiers, getterName);
- if (ClassHelper.boolean_TYPE == node.getType() || ClassHelper.Boolean_TYPE == node.getType()) {
- String secondGetterName = "is" + capitalize(name);
- visitGetter(node, getterBlock, getterModifiers, secondGetterName);
+ if (node.getType().equals(ClassHelper.boolean_TYPE) || node.getType().equals(ClassHelper.Boolean_TYPE)) {
+ visitGetter(node, getterBlock, getterModifiers, "is" + capitalize(name));
}
}
if (setterBlock != null) {
Parameter[] setterParameterTypes = {new Parameter(node.getType(), "value")};
- MethodNode setter =
- new MethodNode(setterName, accessorModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
+ MethodNode setter = new MethodNode(setterName, accessorModifiers, ClassHelper.VOID_TYPE, setterParameterTypes, ClassNode.EMPTY_ARRAY, setterBlock);
setter.setSynthetic(true);
addPropertyMethod(setter);
visitMethod(setter);
}
}
- private void visitGetter(PropertyNode node, Statement getterBlock, int getterModifiers, String getterName) {
- MethodNode getter =
- new MethodNode(getterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
+ private void visitGetter(final PropertyNode node, final Statement getterBlock, final int getterModifiers, final String getterName) {
+ MethodNode getter = new MethodNode(getterName, getterModifiers, node.getType(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, getterBlock);
getter.setSynthetic(true);
addPropertyMethod(getter);
visitMethod(getter);
}
- protected void addPropertyMethod(MethodNode method) {
+ protected void addPropertyMethod(final MethodNode method) {
classNode.addMethod(method);
markAsGenerated(classNode, method);
// GROOVY-4415 / GROOVY-4645: check that there's no abstract method which corresponds to this one
@@ -763,7 +759,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
/**
* Creates a new method for each combination of default parameter expressions.
*/
- protected void addDefaultParameterMethods(ClassNode type) {
+ protected void addDefaultParameterMethods(final ClassNode type) {
List<MethodNode> methods = new ArrayList<>(type.getMethods());
addDefaultParameters(methods, (arguments, params, method) -> {
BlockStatement code = new BlockStatement();
@@ -790,14 +786,14 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
private boolean inClosure;
@Override
- public void visitClosureExpression(ClosureExpression e) {
+ public void visitClosureExpression(final ClosureExpression e) {
boolean prev = inClosure; inClosure = true;
super.visitClosureExpression(e);
inClosure = prev;
}
@Override
- public void visitVariableExpression(VariableExpression e) {
+ public void visitVariableExpression(final VariableExpression e) {
if (e.getAccessedVariable() instanceof Parameter) {
Parameter p = (Parameter) e.getAccessedVariable();
if (p.hasInitialExpression() && !Arrays.asList(params).contains(p)) {
@@ -821,7 +817,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
visitor.visitArgumentlistExpression(arguments);
// if variable was created to capture an initial value expression, reference it in arguments as well
- for (ListIterator<Expression> it = arguments.getExpressions().listIterator(); it.hasNext();) {
+ for (ListIterator<Expression> it = arguments.getExpressions().listIterator(); it.hasNext(); ) {
Expression argument = it.next();
if (argument instanceof CastExpression) {
argument = ((CastExpression) argument).getExpression();
@@ -851,7 +847,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
// GROOVY-5681: set anon. inner enclosing method reference
visitor = new CodeVisitorSupport() {
@Override
- public void visitConstructorCallExpression(ConstructorCallExpression call) {
+ public void visitConstructorCallExpression(final ConstructorCallExpression call) {
if (call.isUsingAnonymousInnerClass()) {
call.getType().setEnclosingMethod(newMethod);
}
@@ -868,11 +864,11 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
/**
* Creates a new constructor for each combination of default parameter expressions.
*/
- protected void addDefaultParameterConstructors(ClassNode type) {
+ protected void addDefaultParameterConstructors(final ClassNode type) {
List<ConstructorNode> constructors = new ArrayList<>(type.getDeclaredConstructors());
addDefaultParameters(constructors, (arguments, params, method) -> {
// GROOVY-9151: check for references to parameters that have been removed
- for (ListIterator<Expression> it = arguments.getExpressions().listIterator(); it.hasNext();) {
+ for (ListIterator<Expression> it = arguments.getExpressions().listIterator(); it.hasNext(); ) {
Expression argument = it.next();
if (argument instanceof CastExpression) {
argument = ((CastExpression) argument).getExpression();
@@ -891,7 +887,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
GroovyCodeVisitor visitor = new CodeVisitorSupport() {
@Override
- public void visitVariableExpression(VariableExpression e) {
+ public void visitVariableExpression(final VariableExpression e) {
if (e.getAccessedVariable() instanceof Parameter) {
Parameter p = (Parameter) e.getAccessedVariable();
if (p.hasInitialExpression() && !Arrays.asList(params).contains(p)) {
@@ -913,7 +909,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
});
}
- protected void addConstructor(Parameter[] newParams, ConstructorNode ctor, Statement code, ClassNode type) {
+ protected void addConstructor(final Parameter[] newParams, final ConstructorNode ctor, final Statement code, final ClassNode type) {
ConstructorNode newConstructor = type.addConstructor(ctor.getModifiers(), newParams, ctor.getExceptions(), code);
newConstructor.putNodeMetaData(DEFAULT_PARAMETER_GENERATED, Boolean.TRUE);
markAsGenerated(type, newConstructor);
@@ -922,7 +918,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
// set anon. inner enclosing method reference
code.visit(new CodeVisitorSupport() {
@Override
- public void visitConstructorCallExpression(ConstructorCallExpression call) {
+ public void visitConstructorCallExpression(final ConstructorCallExpression call) {
if (call.isUsingAnonymousInnerClass()) {
call.getType().setEnclosingMethod(newConstructor);
}
@@ -934,7 +930,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
/**
* Creates a new helper method for each combination of default parameter expressions.
*/
- protected void addDefaultParameters(List<? extends MethodNode> methods, DefaultArgsAction action) {
+ protected void addDefaultParameters(final List<? extends MethodNode> methods, final DefaultArgsAction action) {
for (MethodNode method : methods) {
if (method.hasDefaultValue()) {
addDefaultParameters(action, method);
@@ -942,7 +938,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- protected void addDefaultParameters(DefaultArgsAction action, MethodNode method) {
+ protected void addDefaultParameters(final DefaultArgsAction action, final MethodNode method) {
Parameter[] parameters = method.getParameters();
long n = Arrays.stream(parameters).filter(Parameter::hasInitialExpression).count();
@@ -1006,7 +1002,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- protected void addInitialization(ClassNode node, ConstructorNode constructorNode) {
+ protected void addInitialization(final ClassNode node, final ConstructorNode constructorNode) {
Statement firstStatement = constructorNode.getFirstStatement();
// if some transformation decided to generate constructor then it probably knows who it does
if (firstStatement instanceof BytecodeSequence)
@@ -1064,7 +1060,6 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
newBlock.setSourcePosition(block);
constructorNode.setCode(newBlock);
-
if (!staticStatements.isEmpty()) {
if (isEnum) {
/*
@@ -1086,7 +1081,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
* When InnerClassVisitor adds <code>this.this$0 = $p$n</code>, it adds it
* as a BlockStatement having that ExpressionStatement.
*/
- private Statement getImplicitThis$0StmtIfInnerClass(List<Statement> otherStatements) {
+ private Statement getImplicitThis$0StmtIfInnerClass(final List<Statement> otherStatements) {
if (!(classNode instanceof InnerClassNode)) return null;
for (Statement stmt : otherStatements) {
if (stmt instanceof BlockStatement) {
@@ -1117,20 +1112,19 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return false;
}
- protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode,
- boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) {
+ // TODO: add generics to collections
+ protected void addFieldInitialization(final List list, final List staticList, final FieldNode fieldNode, final boolean isEnumClassNode, final List initStmtsAfterEnumValuesInit, final Set explicitStaticPropsInEnum) {
Expression expression = fieldNode.getInitialExpression();
if (expression != null) {
- final FieldExpression fe = new FieldExpression(fieldNode);
+ final FieldExpression fe = fieldX(fieldNode);
if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & ACC_SYNTHETIC) != 0)) {
fe.setUseReferenceDirectly(true);
}
- ExpressionStatement statement =
- new ExpressionStatement(
- new BinaryExpression(
- fe,
- Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()),
- expression));
+ Statement statement = stmt(binX(
+ fe,
+ Token.newSymbol(Types.ASSIGN, fieldNode.getLineNumber(), fieldNode.getColumnNumber()),
+ expression
+ ));
if (fieldNode.isStatic()) {
// GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be
// initialized first so that code dependent on it does not see their values as empty
@@ -1165,13 +1159,13 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
/**
* Capitalizes the start of the given bean property name.
*/
- public static String capitalize(String name) {
+ public static String capitalize(final String name) {
return BeanUtils.capitalize(name);
}
- protected Statement createGetterBlock(PropertyNode propertyNode, final FieldNode field) {
+ protected Statement createGetterBlock(final PropertyNode propertyNode, final FieldNode field) {
return new BytecodeSequence(new BytecodeInstruction() {
- public void visit(MethodVisitor mv) {
+ public void visit(final MethodVisitor mv) {
if (field.isStatic()) {
mv.visitFieldInsn(GETSTATIC, BytecodeHelper.getClassInternalName(classNode), field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
} else {
@@ -1183,10 +1177,10 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
});
}
- protected Statement createSetterBlock(PropertyNode propertyNode, final FieldNode field) {
+ protected Statement createSetterBlock(final PropertyNode propertyNode, final FieldNode field) {
return new BytecodeSequence(new BytecodeInstruction() {
@Override
- public void visit(MethodVisitor mv) {
+ public void visit(final MethodVisitor mv) {
if (field.isStatic()) {
BytecodeHelper.load(mv, field.getType(), 0);
mv.visitFieldInsn(PUTSTATIC, BytecodeHelper.getClassInternalName(classNode), field.getName(), BytecodeHelper.getTypeDescription(field.getType()));
@@ -1200,10 +1194,10 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
});
}
- public void visitGenericType(GenericsType genericsType) {
+ public void visitGenericType(final GenericsType genericsType) {
}
- public static Long getTimestampFromFieldName(String fieldName) {
+ public static Long getTimestampFromFieldName(final String fieldName) {
if (fieldName.startsWith(__TIMESTAMP__)) {
try {
return Long.decode(fieldName.substring(__TIMESTAMP__.length()));
@@ -1214,16 +1208,15 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return null;
}
- public static long getTimestamp(Class<?> clazz) {
+ public static long getTimestamp(final Class<?> clazz) {
if (clazz.getClassLoader() instanceof GroovyClassLoader.InnerLoader) {
GroovyClassLoader.InnerLoader innerLoader = (GroovyClassLoader.InnerLoader) clazz.getClassLoader();
return innerLoader.getTimeStamp();
}
- final Field[] fields = clazz.getFields();
- for (int i = 0; i != fields.length; ++i) {
- if (isStatic(fields[i].getModifiers())) {
- Long timestamp = getTimestampFromFieldName(fields[i].getName());
+ for (Field field : clazz.getFields()) {
+ if (isStatic(field.getModifiers())) {
+ Long timestamp = getTimestampFromFieldName(field.getName());
if (timestamp != null) {
return timestamp;
}
@@ -1232,19 +1225,18 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return Long.MAX_VALUE;
}
- protected void addCovariantMethods(ClassNode classNode) {
- Map methodsToAdd = new HashMap();
- Map genericsSpec = new HashMap();
+ protected void addCovariantMethods(final ClassNode classNode) {
+ Map<String, MethodNode> methodsToAdd = new HashMap<>();
+ Map<String, ClassNode> genericsSpec = new HashMap<>();
// unimplemented abstract methods from interfaces
Map<String, MethodNode> abstractMethods = ClassNodeUtils.getDeclaredMethodsFromInterfaces(classNode);
- Map<String, MethodNode> allInterfaceMethods = new HashMap<String, MethodNode>(abstractMethods);
+ Map<String, MethodNode> allInterfaceMethods = new HashMap<>(abstractMethods);
ClassNodeUtils.addDeclaredMethodsFromAllInterfaces(classNode, allInterfaceMethods);
-
- List<MethodNode> declaredMethods = new ArrayList<MethodNode>(classNode.getMethods());
+ List<MethodNode> declaredMethods = new ArrayList<>(classNode.getMethods());
// remove all static, private and package private methods
- for (Iterator methodsIterator = declaredMethods.iterator(); methodsIterator.hasNext(); ) {
- MethodNode m = (MethodNode) methodsIterator.next();
+ for (Iterator<MethodNode> methodsIterator = declaredMethods.iterator(); methodsIterator.hasNext(); ) {
+ MethodNode m = methodsIterator.next();
abstractMethods.remove(m.getTypeDescriptor());
if (m.isStatic() || !(m.isPublic() || m.isProtected())) {
methodsIterator.remove();
@@ -1261,63 +1253,56 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
addCovariantMethods(classNode, declaredMethods, abstractMethods, methodsToAdd, genericsSpec);
- Map<String, MethodNode> declaredMethodsMap = new HashMap<String, MethodNode>();
+ Map<String, MethodNode> declaredMethodsMap = new HashMap<>();
if (!methodsToAdd.isEmpty()) {
for (MethodNode mn : declaredMethods) {
declaredMethodsMap.put(mn.getTypeDescriptor(), mn);
}
}
- for (Object o : methodsToAdd.entrySet()) {
- Map.Entry entry = (Map.Entry) o;
- MethodNode method = (MethodNode) entry.getValue();
+ for (Map.Entry<String, MethodNode> entry : methodsToAdd.entrySet()) {
// we skip bridge methods implemented in current class already
MethodNode mn = declaredMethodsMap.get(entry.getKey());
- if (mn != null && mn.getDeclaringClass().equals(classNode)) continue;
- addPropertyMethod(method);
+ if (mn == null || !mn.getDeclaringClass().equals(classNode)) {
+ addPropertyMethod(entry.getValue());
+ }
}
}
- private void addCovariantMethods(ClassNode classNode, List declaredMethods, Map abstractMethods, Map methodsToAdd, Map oldGenericsSpec) {
+ private void addCovariantMethods(final ClassNode classNode, final List<MethodNode> declaredMethods, final Map<String, MethodNode> abstractMethods, final Map<String, MethodNode> methodsToAdd, final Map<String, ClassNode> oldGenericsSpec) {
ClassNode sn = classNode.getUnresolvedSuperClass(false);
-
if (sn != null) {
- Map genericsSpec = createGenericsSpec(sn, oldGenericsSpec);
+ Map<String, ClassNode> genericsSpec = createGenericsSpec(sn, oldGenericsSpec);
List<MethodNode> classMethods = sn.getMethods();
// original class causing bridge methods for methods in super class
storeMissingCovariantMethods(declaredMethods, methodsToAdd, genericsSpec, classMethods);
// super class causing bridge methods for abstract methods in original class
if (!abstractMethods.isEmpty()) {
- for (Object classMethod : classMethods) {
- MethodNode method = (MethodNode) classMethod;
+ for (MethodNode method : classMethods) {
if (method.isStatic()) continue;
- storeMissingCovariantMethods(abstractMethods.values(), method, methodsToAdd, Collections.EMPTY_MAP, true);
+ storeMissingCovariantMethods(abstractMethods.values(), method, methodsToAdd, Collections.emptyMap(), true);
}
}
addCovariantMethods(sn.redirect(), declaredMethods, abstractMethods, methodsToAdd, genericsSpec);
}
- ClassNode[] interfaces = classNode.getInterfaces();
- for (ClassNode anInterface : interfaces) {
- List interfacesMethods = anInterface.getMethods();
- Map genericsSpec = createGenericsSpec(anInterface, oldGenericsSpec);
+ for (ClassNode anInterface : classNode.getInterfaces()) {
+ List<MethodNode> interfacesMethods = anInterface.getMethods();
+ Map<String, ClassNode> genericsSpec = createGenericsSpec(anInterface, oldGenericsSpec);
storeMissingCovariantMethods(declaredMethods, methodsToAdd, genericsSpec, interfacesMethods);
addCovariantMethods(anInterface, declaredMethods, abstractMethods, methodsToAdd, genericsSpec);
}
-
}
- private void storeMissingCovariantMethods(List declaredMethods, Map methodsToAdd, Map genericsSpec, List<MethodNode> methodNodeList) {
- for (Object declaredMethod : declaredMethods) {
- MethodNode method = (MethodNode) declaredMethod;
+ private void storeMissingCovariantMethods(final List<MethodNode> declaredMethods, final Map<String, MethodNode> methodsToAdd, final Map<String, ClassNode> genericsSpec, final List<MethodNode> methodNodeList) {
+ for (MethodNode method : declaredMethods) {
if (method.isStatic()) continue;
storeMissingCovariantMethods(methodNodeList, method, methodsToAdd, genericsSpec, false);
}
}
- private MethodNode getCovariantImplementation(final MethodNode oldMethod, final MethodNode overridingMethod, Map genericsSpec, boolean ignoreError) {
- // method name
+ private MethodNode getCovariantImplementation(final MethodNode oldMethod, final MethodNode overridingMethod, Map<String, ClassNode> genericsSpec, final boolean ignoreError) {
if (!oldMethod.getName().equals(overridingMethod.getName())) return null;
if ((overridingMethod.getModifiers() & ACC_BRIDGE) != 0) return null;
if ((oldMethod.getModifiers() & ACC_BRIDGE) != 0) return null;
@@ -1327,30 +1312,30 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
genericsSpec = addMethodGenerics(oldMethod, genericsSpec);
// parameters
- boolean normalEqualParameters = equalParametersNormal(overridingMethod, oldMethod);
- boolean genericEqualParameters = equalParametersWithGenerics(overridingMethod, oldMethod, genericsSpec);
- if (!normalEqualParameters && !genericEqualParameters) return null;
+ boolean equalParameters = equalParametersNormal(overridingMethod, oldMethod);
+ if (!equalParameters && !equalParametersWithGenerics(overridingMethod, oldMethod, genericsSpec)) return null;
// return type
- ClassNode mr = overridingMethod.getReturnType();
+ ClassNode nmr = overridingMethod.getReturnType();
ClassNode omr = oldMethod.getReturnType();
- boolean equalReturnType = mr.equals(omr);
+ boolean equalReturnType = nmr.equals(omr);
- ClassNode testmr = correctToGenericsSpec(genericsSpec, omr);
- if (!isAssignable(mr, testmr)) {
+ // TODO: ClassNode nmrCorrected = correctToGenericsSpec(..., nmr)?
+ ClassNode omrCorrected = correctToGenericsSpec(genericsSpec, omr);
+ if (!isAssignable(nmr, omrCorrected)) {
if (ignoreError) return null;
throw new RuntimeParserException(
"The return type of " +
overridingMethod.getTypeDescriptor() +
" in " + overridingMethod.getDeclaringClass().getName() +
- " is incompatible with " + testmr.getName() +
+ " is incompatible with " + omrCorrected.getName() +
" in " + oldMethod.getDeclaringClass().getName(),
sourceOf(overridingMethod));
}
- if (equalReturnType && normalEqualParameters) return null;
+ if (equalReturnType && equalParameters) return null;
- if ((oldMethod.getModifiers() & ACC_FINAL) != 0) {
+ if (oldMethod.isFinal()) {
throw new RuntimeParserException(
"Cannot override final method " +
oldMethod.getTypeDescriptor() +
@@ -1366,15 +1351,15 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
sourceOf(overridingMethod));
}
if (!equalReturnType) {
- boolean oldM = ClassHelper.isPrimitiveType(oldMethod.getReturnType());
- boolean newM = ClassHelper.isPrimitiveType(overridingMethod.getReturnType());
+ boolean oldM = ClassHelper.isPrimitiveType(omr);
+ boolean newM = ClassHelper.isPrimitiveType(nmr);
if (oldM || newM) {
String message;
if (oldM && newM) {
message = " with old and new method having different primitive return types";
} else if (newM) {
message = " with new method having a primitive return type and old method not";
- } else /* oldM */ {
+ } else /*oldM*/ {
message = " with old method having a primitive return type and new method not";
}
throw new RuntimeParserException(
@@ -1386,43 +1371,41 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
}
}
- // if we reach this point we have at least one parameter or return type, that
- // is different in its specified form. That means we have to create a bridge method!
- MethodNode newMethod = new MethodNode(
+ // if we reach this point there is least one parameter or return type
+ // that is different in its specified form, so create a bridge method
+ return new MethodNode(
oldMethod.getName(),
overridingMethod.getModifiers() | ACC_SYNTHETIC | ACC_BRIDGE,
- cleanType(oldMethod.getReturnType()),
+ cleanType(omr),
cleanParameters(oldMethod.getParameters()),
oldMethod.getExceptions(),
- null
- );
- newMethod.setCode(new BytecodeSequence(new BytecodeInstruction() {
- @Override
- public void visit(MethodVisitor mv) {
- mv.visitVarInsn(ALOAD, 0);
- Parameter[] para = oldMethod.getParameters();
- Parameter[] goal = overridingMethod.getParameters();
- int doubleSlotOffset = 0;
- for (int i = 0; i < para.length; i++) {
- ClassNode type = para[i].getType();
- BytecodeHelper.load(mv, type, i + 1 + doubleSlotOffset);
- if (type.redirect() == ClassHelper.double_TYPE ||
- type.redirect() == ClassHelper.long_TYPE) {
- doubleSlotOffset++;
- }
- if (!type.equals(goal[i].getType())) {
- BytecodeHelper.doCast(mv, goal[i].getType());
- }
- }
- mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(classNode), overridingMethod.getName(), BytecodeHelper.getMethodDescriptor(overridingMethod.getReturnType(), overridingMethod.getParameters()), false);
+ new BytecodeSequence(new BytecodeInstruction() {
+ @Override
+ public void visit(final MethodVisitor mv) {
+ mv.visitVarInsn(ALOAD, 0);
+ Parameter[] para = oldMethod.getParameters();
+ Parameter[] goal = overridingMethod.getParameters();
+ int doubleSlotOffset = 0;
+ for (int i = 0, n = para.length; i < n; i += 1) {
+ ClassNode type = para[i].getType();
+ BytecodeHelper.load(mv, type, i + 1 + doubleSlotOffset);
+ if (type.redirect() == ClassHelper.double_TYPE
+ || type.redirect() == ClassHelper.long_TYPE) {
+ doubleSlotOffset += 1;
+ }
+ if (!type.equals(goal[i].getType())) {
+ BytecodeHelper.doCast(mv, goal[i].getType());
+ }
+ }
+ mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(classNode), overridingMethod.getName(), BytecodeHelper.getMethodDescriptor(nmr, overridingMethod.getParameters()), false);
- BytecodeHelper.doReturn(mv, oldMethod.getReturnType());
- }
- }));
- return newMethod;
+ BytecodeHelper.doReturn(mv, oldMethod.getReturnType());
+ }
+ })
+ );
}
- private boolean isAssignable(ClassNode node, ClassNode testNode) {
+ private static boolean isAssignable(final ClassNode node, final ClassNode testNode) {
if (node.isArray() && testNode.isArray()) {
return isArrayAssignable(node.getComponentType(), testNode.getComponentType());
}
@@ -1432,42 +1415,38 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return node.isDerivedFrom(testNode);
}
- private boolean isArrayAssignable(ClassNode node, ClassNode testNode) {
+ private static boolean isArrayAssignable(final ClassNode node, final ClassNode testNode) {
if (node.isArray() && testNode.isArray()) {
return isArrayAssignable(node.getComponentType(), testNode.getComponentType());
}
return isAssignable(node, testNode);
}
- private static Parameter[] cleanParameters(Parameter[] parameters) {
- Parameter[] params = new Parameter[parameters.length];
- for (int i = 0; i < params.length; i++) {
- params[i] = new Parameter(cleanType(parameters[i].getType()), parameters[i].getName());
- }
- return params;
+ private static Parameter[] cleanParameters(final Parameter[] parameters) {
+ return Arrays.stream(parameters).map(p -> param(cleanType(p.getType()), p.getName())).toArray(Parameter[]::new);
}
- private static ClassNode cleanType(ClassNode type) {
- // todo: should this be directly handled by getPlainNodeReference?
+ private static ClassNode cleanType(final ClassNode type) {
+ // TODO: Should this be directly handled by getPlainNodeReference?
if (type.isArray()) return cleanType(type.getComponentType()).makeArray();
return type.getPlainNodeReference();
}
- private void storeMissingCovariantMethods(Collection methods, MethodNode method, Map methodsToAdd, Map genericsSpec, boolean ignoreError) {
- for (Object next : methods) {
- MethodNode toOverride = (MethodNode) next;
+ private void storeMissingCovariantMethods(final Iterable<MethodNode> methods, final MethodNode method, final Map<String, MethodNode> methodsToAdd, final Map<String, ClassNode> genericsSpec, final boolean ignoreError) {
+ for (MethodNode toOverride : methods) {
MethodNode bridgeMethod = getCovariantImplementation(toOverride, method, genericsSpec, ignoreError);
- if (bridgeMethod == null) continue;
- methodsToAdd.put(bridgeMethod.getTypeDescriptor(), bridgeMethod);
- return;
+ if (bridgeMethod != null) {
+ methodsToAdd.put(bridgeMethod.getTypeDescriptor(), bridgeMethod);
+ return;
+ }
}
}
- private static boolean equalParametersNormal(MethodNode m1, MethodNode m2) {
+ private static boolean equalParametersNormal(final MethodNode m1, final MethodNode m2) {
Parameter[] p1 = m1.getParameters();
Parameter[] p2 = m2.getParameters();
if (p1.length != p2.length) return false;
- for (int i = 0; i < p2.length; i++) {
+ for (int i = 0, n = p2.length; i < n; i += 1) {
ClassNode type = p2[i].getType();
ClassNode parameterType = p1[i].getType();
if (!parameterType.equals(type)) return false;
@@ -1475,11 +1454,11 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return true;
}
- private static boolean equalParametersWithGenerics(MethodNode m1, MethodNode m2, Map genericsSpec) {
+ private static boolean equalParametersWithGenerics(final MethodNode m1, final MethodNode m2, final Map<String, ClassNode> genericsSpec) {
Parameter[] p1 = m1.getParameters();
Parameter[] p2 = m2.getParameters();
if (p1.length != p2.length) return false;
- for (int i = 0; i < p2.length; i++) {
+ for (int i = 0, n = p2.length; i < n; i += 1) {
ClassNode type = p2[i].getType();
ClassNode genericsType = correctToGenericsSpec(genericsSpec, type);
ClassNode parameterType = p1[i].getType();
@@ -1491,25 +1470,23 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
private static boolean moveOptimizedConstantsInitialization(final ClassNode node) {
if (node.isInterface() && !Traits.isTrait(node)) return false;
- final int mods = ACC_STATIC | ACC_SYNTHETIC | ACC_PUBLIC;
String name = SWAP_INIT;
- BlockStatement methodCode = new BlockStatement();
+ int mods = ACC_STATIC | ACC_SYNTHETIC | ACC_PUBLIC;
+ BlockStatement methodCode = block(new SwapInitStatement());
- methodCode.addStatement(new SwapInitStatement());
boolean swapInitRequired = false;
for (FieldNode fn : node.getFields()) {
if (!fn.isStatic() || !fn.isSynthetic() || !fn.getName().startsWith("$const$")) continue;
if (fn.getInitialExpression() == null) continue;
- final FieldExpression fe = new FieldExpression(fn);
+ final FieldExpression fe = fieldX(fn);
if (fn.getType().equals(ClassHelper.REFERENCE_TYPE)) fe.setUseReferenceDirectly(true);
ConstantExpression init = (ConstantExpression) fn.getInitialExpression();
- init = new ConstantExpression(init.getValue(), true);
- ExpressionStatement statement =
- new ExpressionStatement(
- new BinaryExpression(
- fe,
- Token.newSymbol(Types.EQUAL, fn.getLineNumber(), fn.getColumnNumber()),
- init));
+ init = constX(init.getValue(), true);
+ Statement statement = stmt(binX(
+ fe,
+ Token.newSymbol(Types.ASSIGN, fn.getLineNumber(), fn.getColumnNumber()),
+ init
+ ));
fn.setInitialValueExpression(null);
methodCode.addStatement(statement);
swapInitRequired = true;
@@ -1524,7 +1501,7 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
return swapInitRequired;
}
- private static ASTNode sourceOf(MethodNode methodNode) {
+ private static ASTNode sourceOf(final MethodNode methodNode) {
if (methodNode.getLineNumber() < 1) {
ClassNode declaringClass = methodNode.getDeclaringClass();
if (methodNode.isSynthetic()) {
@@ -1553,28 +1530,29 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
* @return the same instance of constant expression if the type is already primitive, or a primitive
* constant if possible.
*/
- public static ConstantExpression transformToPrimitiveConstantIfPossible(ConstantExpression constantExpression) {
+ public static ConstantExpression transformToPrimitiveConstantIfPossible(final ConstantExpression constantExpression) {
Object value = constantExpression.getValue();
if (value == null) return constantExpression;
ConstantExpression result;
ClassNode type = constantExpression.getType();
if (ClassHelper.isPrimitiveType(type)) return constantExpression;
if (value instanceof String && ((String) value).length() == 1) {
- result = new ConstantExpression(((String) value).charAt(0));
+ result = constX(((String) value).charAt(0));
result.setType(ClassHelper.char_TYPE);
} else {
type = ClassHelper.getUnwrapper(type);
- result = new ConstantExpression(value, true);
+ result = constX(value, true);
result.setType(type);
}
return result;
}
- private static class SwapInitStatement extends BytecodeSequence {
+ //--------------------------------------------------------------------------
+ private static class SwapInitStatement extends BytecodeSequence {
private WriterController controller;
- public SwapInitStatement() {
+ SwapInitStatement() {
super(new SwapInitInstruction());
((SwapInitInstruction) getInstructions().get(0)).statement = this;
}
@@ -1582,14 +1560,13 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
@Override
public void visit(final GroovyCodeVisitor visitor) {
if (visitor instanceof AsmClassGenerator) {
- AsmClassGenerator generator = (AsmClassGenerator) visitor;
- controller = generator.getController();
+ controller = ((AsmClassGenerator) visitor).getController();
}
super.visit(visitor);
}
private static class SwapInitInstruction extends BytecodeInstruction {
- SwapInitStatement statement;
+ private SwapInitStatement statement;
@Override
public void visit(final MethodVisitor mv) {