You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/10/30 15:55:34 UTC
[groovy] 10/17: trivial refactor
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 392228ff328142a492bb559f23cbad387756e6be
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Oct 29 15:57:26 2019 -0500
trivial refactor
(cherry picked from commit 712def09a6e0a7219aa412fafafd523d644a5469)
---
.../classgen/asm/sc/StaticTypesCallSiteWriter.java | 189 ++++++++++-----------
1 file changed, 94 insertions(+), 95 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
index fc035de..1b2ab80 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
@@ -53,12 +53,11 @@ import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
-import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
-import java.util.Set;
+import java.util.Objects;
import static org.apache.groovy.util.BeanUtils.capitalize;
import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
@@ -80,8 +79,7 @@ import static org.codehaus.groovy.ast.ClassHelper.getUnwrapper;
import static org.codehaus.groovy.ast.ClassHelper.getWrapper;
import static org.codehaus.groovy.ast.ClassHelper.int_TYPE;
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
-import static org.codehaus.groovy.ast.ClassHelper.make;
-import static org.codehaus.groovy.classgen.AsmClassGenerator.samePackages;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf;
@@ -94,14 +92,14 @@ import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isClas
*/
public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes {
- private static final ClassNode COLLECTION_TYPE = make(Collection.class);
- private static final ClassNode INVOKERHELPER_TYPE = make(InvokerHelper.class);
+ private static final ClassNode COLLECTION_TYPE = ClassHelper.make(Collection.class);
+ private static final ClassNode INVOKERHELPER_TYPE = ClassHelper.make(InvokerHelper.class);
private static final MethodNode COLLECTION_SIZE_METHOD = COLLECTION_TYPE.getMethod("size", Parameter.EMPTY_ARRAY);
private static final MethodNode CLOSURE_GETTHISOBJECT_METHOD = CLOSURE_TYPE.getMethod("getThisObject", Parameter.EMPTY_ARRAY);
- private static final MethodNode MAP_GET_METHOD = MAP_TYPE.getMethod("get", new Parameter[] {new Parameter(OBJECT_TYPE, "key")});
- private static final MethodNode GROOVYOBJECT_GETPROPERTY_METHOD = GROOVY_OBJECT_TYPE.getMethod("getProperty", new Parameter[] {new Parameter(STRING_TYPE, "propertyName")});
- private static final MethodNode INVOKERHELPER_GETPROPERTY_METHOD = INVOKERHELPER_TYPE.getMethod("getProperty", new Parameter[] {new Parameter(OBJECT_TYPE, "object"), new Parameter(STRING_TYPE, "propertyName")});
- private static final MethodNode INVOKERHELPER_GETPROPERTYSAFE_METHOD = INVOKERHELPER_TYPE.getMethod("getPropertySafe", new Parameter[] {new Parameter(OBJECT_TYPE, "object"), new Parameter(STRING_TYPE, "propertyName")});
+ private static final MethodNode MAP_GET_METHOD = MAP_TYPE.getMethod("get", new Parameter[]{new Parameter(OBJECT_TYPE, "key")});
+ private static final MethodNode GROOVYOBJECT_GETPROPERTY_METHOD = GROOVY_OBJECT_TYPE.getMethod("getProperty", new Parameter[]{new Parameter(STRING_TYPE, "propertyName")});
+ private static final MethodNode INVOKERHELPER_GETPROPERTY_METHOD = INVOKERHELPER_TYPE.getMethod("getProperty", new Parameter[]{new Parameter(OBJECT_TYPE, "object"), new Parameter(STRING_TYPE, "propertyName")});
+ private static final MethodNode INVOKERHELPER_GETPROPERTYSAFE_METHOD = INVOKERHELPER_TYPE.getMethod("getPropertySafe", new Parameter[]{new Parameter(OBJECT_TYPE, "object"), new Parameter(STRING_TYPE, "propertyName")});
private final StaticTypesWriterController controller;
@@ -123,26 +121,26 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
}
@Override
- public void makeGetPropertySite(Expression receiver, final String methodName, final boolean safe, final boolean implicitThis) {
+ public void makeGetPropertySite(Expression receiver, final String propertyName, final boolean safe, final boolean implicitThis) {
Object dynamic = receiver.getNodeMetaData(StaticCompilationMetadataKeys.RECEIVER_OF_DYNAMIC_PROPERTY);
- if (dynamic !=null) {
- makeDynamicGetProperty(receiver, methodName, safe);
+ if (dynamic != null) {
+ makeDynamicGetProperty(receiver, propertyName, safe);
return;
}
TypeChooser typeChooser = controller.getTypeChooser();
ClassNode classNode = controller.getClassNode();
ClassNode receiverType = receiver.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
- if (receiverType==null) {
+ if (receiverType == null) {
receiverType = typeChooser.resolveType(receiver, classNode);
}
Object type = receiver.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
- if (type==null && receiver instanceof VariableExpression) {
+ if (type == null && receiver instanceof VariableExpression) {
Variable variable = ((VariableExpression) receiver).getAccessedVariable();
if (variable instanceof Expression) {
type = ((Expression) variable).getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
}
}
- if (type!=null) {
+ if (type != null) {
// in case a "flow type" is found, it is preferred to use it instead of
// the declaration type
receiverType = (ClassNode) type;
@@ -160,21 +158,15 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
MethodVisitor mv = controller.getMethodVisitor();
- if (receiverType.isArray() && methodName.equals("length")) {
+ if (receiverType.isArray() && "length".equals(propertyName)) {
receiver.visit(controller.getAcg());
ClassNode arrayGetReturnType = typeChooser.resolveType(receiver, classNode);
controller.getOperandStack().doGroovyCast(arrayGetReturnType);
mv.visitInsn(ARRAYLENGTH);
controller.getOperandStack().replace(int_TYPE);
return;
- } else if (
- (receiverType.implementsInterface(COLLECTION_TYPE)
- || COLLECTION_TYPE.equals(receiverType)) && ("size".equals(methodName) || "length".equals(methodName))) {
- MethodCallExpression expr = new MethodCallExpression(
- receiver,
- "size",
- ArgumentListExpression.EMPTY_ARGUMENTS
- );
+ } else if (isOrImplements(receiverType, COLLECTION_TYPE) && ("size".equals(propertyName) || "length".equals(propertyName))) {
+ MethodCallExpression expr = new MethodCallExpression(receiver, "size", ArgumentListExpression.EMPTY_ARGUMENTS);
expr.setMethodTarget(COLLECTION_SIZE_METHOD);
expr.setImplicitThis(implicitThis);
expr.setSafe(safe);
@@ -185,32 +177,31 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
boolean isStaticProperty = receiver instanceof ClassExpression
&& (receiverType.isDerivedFrom(receiver.getType()) || receiverType.implementsInterface(receiver.getType()));
- if (!isStaticProperty && (receiverType.implementsInterface(MAP_TYPE) || MAP_TYPE.equals(receiverType))) {
+ if (!isStaticProperty && isOrImplements(receiverType, MAP_TYPE)) {
// for maps, replace map.foo with map.get('foo')
- writeMapDotProperty(receiver, methodName, mv, safe);
+ writeMapDotProperty(receiver, propertyName, mv, safe);
return;
}
- if (makeGetPropertyWithGetter(receiver, receiverType, methodName, safe, implicitThis)) return;
- if (makeGetField(receiver, receiverType, methodName, safe, implicitThis)) return;
+ if (makeGetPropertyWithGetter(receiver, receiverType, propertyName, safe, implicitThis)) return;
+ if (makeGetField(receiver, receiverType, propertyName, safe, implicitThis)) return;
if (receiver instanceof ClassExpression) {
- if (makeGetField(receiver, receiver.getType(), methodName, safe, implicitThis)) return;
- if (makeGetPropertyWithGetter(receiver, receiver.getType(), methodName, safe, implicitThis)) return;
- if (makeGetPrivateFieldWithBridgeMethod(receiver, receiver.getType(), methodName, safe, implicitThis)) return;
+ if (makeGetField(receiver, receiver.getType(), propertyName, safe, implicitThis)) return;
+ if (makeGetPropertyWithGetter(receiver, receiver.getType(), propertyName, safe, implicitThis)) return;
+ if (makeGetPrivateFieldWithBridgeMethod(receiver, receiver.getType(), propertyName, safe, implicitThis)) return;
}
if (isClassReceiver) {
// we are probably looking for a property of the class
- if (makeGetPropertyWithGetter(receiver, CLASS_Type, methodName, safe, implicitThis)) return;
- if (makeGetField(receiver, CLASS_Type, methodName, safe, false)) return;
+ if (makeGetPropertyWithGetter(receiver, CLASS_Type, propertyName, safe, implicitThis)) return;
+ if (makeGetField(receiver, CLASS_Type, propertyName, safe, false)) return;
}
- if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, methodName, safe, implicitThis)) return;
+ if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, propertyName, safe, implicitThis)) return;
// GROOVY-5580, it is still possible that we're calling a superinterface property
- String getterName = "get" + capitalize(methodName);
- String altGetterName = "is" + capitalize(methodName);
+ String getterName = "get" + capitalize(propertyName);
+ String altGetterName = "is" + capitalize(propertyName);
if (receiverType.isInterface()) {
- Set<ClassNode> allInterfaces = receiverType.getAllInterfaces();
MethodNode getterMethod = null;
- for (ClassNode anInterface : allInterfaces) {
+ for (ClassNode anInterface : receiverType.getAllInterfaces()) {
getterMethod = anInterface.getGetterMethod(getterName);
if (getterMethod == null) getterMethod = anInterface.getGetterMethod(altGetterName);
if (getterMethod != null) break;
@@ -233,13 +224,12 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
call.visit(controller.getAcg());
return;
}
-
}
// GROOVY-5568, we would be facing a DGM call, but instead of foo.getText(), have foo.text
List<MethodNode> methods = findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, getterName, ClassNode.EMPTY_ARRAY);
- for (MethodNode m: findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, altGetterName, ClassNode.EMPTY_ARRAY)) {
- if (Boolean_TYPE.equals(getWrapper(m.getReturnType()))) methods.add(m);
+ for (MethodNode dgm : findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), receiverType, altGetterName, ClassNode.EMPTY_ARRAY)) {
+ if (Boolean_TYPE.equals(getWrapper(dgm.getReturnType()))) methods.add(dgm);
}
if (!methods.isEmpty()) {
List<MethodNode> methodNodes = chooseBestMethod(receiverType, methods, ClassNode.EMPTY_ARRAY);
@@ -259,26 +249,25 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
}
}
- if (!isStaticProperty && (receiverType.implementsInterface(LIST_TYPE) || LIST_TYPE.equals(receiverType))) {
- writeListDotProperty(receiver, methodName, mv, safe);
+ if (!isStaticProperty && isOrImplements(receiverType, LIST_TYPE)) {
+ writeListDotProperty(receiver, propertyName, mv, safe);
return;
}
+ String receiverName = (receiver instanceof ClassExpression ? receiver.getType() : receiverType).toString(false);
controller.getSourceUnit().addError(
- new SyntaxException("Access to "+
- (receiver instanceof ClassExpression ?receiver.getType():receiverType).toString(false)
- +"#"+methodName+" is forbidden", receiver.getLineNumber(), receiver.getColumnNumber(), receiver.getLastLineNumber(), receiver.getLastColumnNumber())
- );
+ new SyntaxException("Access to " + receiverName + "#" + propertyName + " is forbidden",
+ receiver.getLineNumber(), receiver.getColumnNumber(), receiver.getLastLineNumber(), receiver.getLastColumnNumber()) );
controller.getMethodVisitor().visitInsn(ACONST_NULL);
controller.getOperandStack().push(OBJECT_TYPE);
}
- private void makeDynamicGetProperty(final Expression receiver, final String methodName, final boolean safe) {
- MethodNode target = safe?INVOKERHELPER_GETPROPERTYSAFE_METHOD:INVOKERHELPER_GETPROPERTY_METHOD;
+ private void makeDynamicGetProperty(final Expression receiver, final String propertyName, final boolean safe) {
+ MethodNode target = safe ? INVOKERHELPER_GETPROPERTYSAFE_METHOD : INVOKERHELPER_GETPROPERTY_METHOD;
MethodCallExpression mce = new MethodCallExpression(
new ClassExpression(INVOKERHELPER_TYPE),
target.getName(),
- new ArgumentListExpression(receiver, new ConstantExpression(methodName))
+ new ArgumentListExpression(receiver, new ConstantExpression(propertyName))
);
mce.setSafe(false);
mce.setImplicitThis(false);
@@ -286,7 +275,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
mce.visit(controller.getAcg());
}
- private void writeMapDotProperty(final Expression receiver, final String methodName, final MethodVisitor mv, final boolean safe) {
+ private void writeMapDotProperty(final Expression receiver, final String propertyName, final MethodVisitor mv, final boolean safe) {
receiver.visit(controller.getAcg()); // load receiver
Label exit = new Label();
@@ -300,7 +289,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
receiver.visit(controller.getAcg());
}
- mv.visitLdcInsn(methodName); // load property name
+ mv.visitLdcInsn(propertyName); // load property name
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
if (safe) {
mv.visitLabel(exit);
@@ -308,9 +297,9 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
controller.getOperandStack().replace(OBJECT_TYPE);
}
- private void writeListDotProperty(final Expression receiver, final String methodName, final MethodVisitor mv, final boolean safe) {
+ private void writeListDotProperty(final Expression receiver, final String propertyName, final MethodVisitor mv, final boolean safe) {
ClassNode componentType = receiver.getNodeMetaData(StaticCompilationMetadataKeys.COMPONENT_TYPE);
- if (componentType==null) {
+ if (componentType == null) {
componentType = OBJECT_TYPE;
}
// for lists, replace list.foo with:
@@ -330,7 +319,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
mv.visitLabel(doGet);
}
- Variable tmpList = new VariableExpression("tmpList", make(ArrayList.class));
+ Variable tmpList = new VariableExpression("tmpList", ClassHelper.make(ArrayList.class));
int var = compileStack.defineTemporaryVariable(tmpList, false);
Variable iterator = new VariableExpression("iterator", Iterator_TYPE);
int it = compileStack.defineTemporaryVariable(iterator, false);
@@ -374,7 +363,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
public ClassNode getType() {
return finalComponentType;
}
- }, methodName);
+ }, propertyName);
pexp.visit(controller.getAcg());
controller.getOperandStack().box();
controller.getOperandStack().remove(1);
@@ -388,7 +377,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
if (safe) {
mv.visitLabel(exit);
}
- controller.getOperandStack().push(make(ArrayList.class));
+ controller.getOperandStack().push(ClassHelper.make(ArrayList.class));
controller.getCompileStack().removeVar(next);
controller.getCompileStack().removeVar(it);
controller.getCompileStack().removeVar(var);
@@ -398,7 +387,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
private boolean makeGetPrivateFieldWithBridgeMethod(final Expression receiver, final ClassNode receiverType, final String fieldName, final boolean safe, final boolean implicitThis) {
FieldNode field = receiverType.getField(fieldName);
ClassNode outerClass = receiverType.getOuterClass();
- if (field==null && implicitThis && outerClass !=null && !receiverType.isStaticClass()) {
+ if (field == null && implicitThis && outerClass != null && !receiverType.isStaticClass()) {
Expression pexp;
if (controller.isInClosure()) {
MethodCallExpression mce = new MethodCallExpression(
@@ -422,7 +411,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
return makeGetPrivateFieldWithBridgeMethod(pexp, outerClass, fieldName, safe, true);
}
ClassNode classNode = controller.getClassNode();
- if (field!=null && Modifier.isPrivate(field.getModifiers())
+ if (field != null && field.isPrivate()
&& (StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(receiverType, classNode) || StaticInvocationWriter.isPrivateBridgeMethodsCallAllowed(classNode,receiverType))
&& !receiverType.equals(classNode)) {
Map<String, MethodNode> accessors = receiverType.redirect().getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_FIELDS_ACCESSORS);
@@ -443,15 +432,15 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
}
@Override
- public void makeGroovyObjectGetPropertySite(final Expression receiver, final String methodName, final boolean safe, final boolean implicitThis) {
+ public void makeGroovyObjectGetPropertySite(final Expression receiver, final String propertyName, final boolean safe, final boolean implicitThis) {
TypeChooser typeChooser = controller.getTypeChooser();
ClassNode classNode = controller.getClassNode();
ClassNode receiverType = typeChooser.resolveType(receiver, classNode);
if (receiver instanceof VariableExpression && ((VariableExpression) receiver).isThisExpression() && !controller.isInClosure()) {
receiverType = classNode;
}
-
- String property = methodName;
+
+ String property = propertyName;
if (implicitThis) {
if (controller.getInvocationWriter() instanceof StaticInvocationWriter) {
MethodCallExpression currentCall = ((StaticInvocationWriter) controller.getInvocationWriter()).getCurrentCall();
@@ -466,7 +455,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
};
thisLoader.setType(CLOSURE_TYPE);
Expression pexp = new PropertyExpression(thisLoader, new ConstantExpression(props[0]), safe);
- for (int i = 1, propsLength = props.length; i < propsLength; i++) {
+ for (int i = 1, n = props.length; i < n; i += 1) {
final String prop = props[i];
pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, CLOSURE_TYPE);
pexp = new PropertyExpression(pexp, prop);
@@ -496,29 +485,29 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
public void makeCallSiteArrayInitializer() {
}
- private boolean makeGetPropertyWithGetter(final Expression receiver, final ClassNode receiverType, final String methodName, final boolean safe, final boolean implicitThis) {
- // does a getter exists ?
- String getterName = "get" + capitalize(methodName);
+ private boolean makeGetPropertyWithGetter(final Expression receiver, final ClassNode receiverType, final String propertyName, final boolean safe, final boolean implicitThis) {
+ // does a getter exist?
+ String getterName = "get" + capitalize(propertyName);
MethodNode getterNode = receiverType.getGetterMethod(getterName);
- if (getterNode==null) {
- getterName = "is" + capitalize(methodName);
+ if (getterNode == null) {
+ getterName = "is" + capitalize(propertyName);
getterNode = receiverType.getGetterMethod(getterName);
}
- if (getterNode!=null && receiver instanceof ClassExpression && !CLASS_Type.equals(receiverType) && !getterNode.isStatic()) {
+ if (getterNode != null && receiver instanceof ClassExpression && !CLASS_Type.equals(receiverType) && !getterNode.isStatic()) {
return false;
}
// GROOVY-5561: if two files are compiled in the same source unit
// and that one references the other, the getters for properties have not been
// generated by the compiler yet (generated by the Verifier)
- PropertyNode propertyNode = receiverType.getProperty(methodName);
+ PropertyNode propertyNode = receiverType.getProperty(propertyName);
if (getterNode == null && propertyNode != null) {
// it is possible to use a getter
String prefix = "get";
if (boolean_TYPE.equals(propertyNode.getOriginType())) {
prefix = "is";
}
- getterName = prefix + capitalize(methodName);
+ getterName = prefix + capitalize(propertyName);
getterNode = new MethodNode(
getterName,
ACC_PUBLIC,
@@ -529,7 +518,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
getterNode.setDeclaringClass(receiverType);
if (propertyNode.isStatic()) getterNode.setModifiers(ACC_PUBLIC + ACC_STATIC);
}
- if (getterNode!=null) {
+ if (getterNode != null) {
MethodCallExpression call = new MethodCallExpression(
receiver,
getterName,
@@ -544,21 +533,21 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
}
if (receiverType instanceof InnerClassNode && !receiverType.isStaticClass()) {
- if (makeGetPropertyWithGetter(receiver, receiverType.getOuterClass(), methodName, safe, implicitThis)) {
+ if (makeGetPropertyWithGetter(receiver, receiverType.getOuterClass(), propertyName, safe, implicitThis)) {
return true;
}
}
// check direct interfaces (GROOVY-7149)
for (ClassNode node : receiverType.getInterfaces()) {
- if (makeGetPropertyWithGetter(receiver, node, methodName, safe, implicitThis)) {
+ if (makeGetPropertyWithGetter(receiver, node, propertyName, safe, implicitThis)) {
return true;
}
}
// go upper level
ClassNode superClass = receiverType.getSuperClass();
- if (superClass !=null) {
- return makeGetPropertyWithGetter(receiver, superClass, methodName, safe, implicitThis);
+ if (superClass != null) {
+ return makeGetPropertyWithGetter(receiver, superClass, propertyName, safe, implicitThis);
}
return false;
@@ -566,8 +555,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
boolean makeGetField(final Expression receiver, final ClassNode receiverType, final String fieldName, final boolean safe, final boolean implicitThis) {
FieldNode field = receiverType.getField(fieldName);
- // direct access is allowed if we are in the same class as the declaring class
- // or we are in an inner class
+
if (field != null && isDirectAccessAllowed(field, controller.getClassNode())) {
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
@@ -579,9 +567,11 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
} else {
if (implicitThis) {
compileStack.pushImplicitThis(implicitThis);
+ receiver.visit(controller.getAcg());
+ compileStack.popImplicitThis();
+ } else {
+ receiver.visit(controller.getAcg());
}
- receiver.visit(controller.getAcg());
- if (implicitThis) compileStack.popImplicitThis();
Label exit = new Label();
if (safe) {
mv.visitInsn(DUP);
@@ -609,39 +599,48 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
return true;
}
- for (ClassNode intf : receiverType.getInterfaces()) {
+ for (ClassNode face : receiverType.getInterfaces()) {
// GROOVY-7039
- if (intf != receiverType && makeGetField(receiver, intf, fieldName, safe, implicitThis)) {
+ if (face != receiverType && makeGetField(receiver, face, fieldName, safe, implicitThis)) {
return true;
}
}
ClassNode superClass = receiverType.getSuperClass();
- if (superClass != null) {
+ if (superClass != null && !OBJECT_TYPE.equals(superClass)) {
return makeGetField(receiver, superClass, fieldName, safe, implicitThis);
}
return false;
}
- private static boolean isDirectAccessAllowed(FieldNode field, ClassNode receiver) {
- ClassNode declaringClass = field.getDeclaringClass().redirect();
- ClassNode receiverType = receiver.redirect();
+ /**
+ * Direct access is allowed from the declaring class of the field and sometimes from inner and peer types.
+ *
+ * @return {@code true} if GETFIELD or GETSTATIC is safe for given field and receiver
+ */
+ private static boolean isDirectAccessAllowed(final FieldNode field, final ClassNode receiver) {
+ // first, direct access from anywhere for public fields
+ if (field.isPublic()) return true;
+
+ ClassNode declaringType = field.getDeclaringClass().redirect(), receiverType = receiver.redirect();
+
+ // next, direct access from within the declaring class
+ if (receiverType.equals(declaringType)) return true;
- // first, direct access from within the class
- if (declaringClass.equals(receiverType)) return true;
if (field.isPrivate()) return false;
- // now, inner class node access to outer class fields
+ // next, direct access from within the declaring package
+ if (Objects.equals(receiver.getPackageName(), declaringType.getPackageName())) return true;
+
+ // last, inner class access to outer class fields
receiverType = receiverType.getOuterClass();
while (receiverType != null) {
- if (declaringClass.equals(receiverType)) {
+ if (receiverType.equals(declaringType)) {
return true;
}
receiverType = receiverType.getOuterClass();
}
-
- // finally public and visible
- return field.isPublic() || samePackages(receiver.getPackageName(), declaringClass.getPackageName());
+ return false;
}
@Override
@@ -749,7 +748,7 @@ public class StaticTypesCallSiteWriter extends CallSiteWriter implements Opcodes
nodes = StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), rType, message, args);
}
nodes = StaticTypeCheckingSupport.chooseBestMethod(rType, nodes, args);
- if (nodes.size()==1 || nodes.size()>1 && acceptAnyMethod) {
+ if (nodes.size() == 1 || (nodes.size() > 1 && acceptAnyMethod)) {
MethodNode methodNode = nodes.get(0);
MethodCallExpression call = new MethodCallExpression(
receiver,