public static boolean isInvocationWithImplicitThis(Expression invocation) { if (invocation instanceof PropertyExpression) { return ((PropertyExpression) invocation).isImplicitThis(); } if (invocation instanceof MethodCallExpression) { return ((MethodCallExpression) invocation).isImplicitThis(); } return false; }
private boolean isInternalFieldAccess(final Expression exp) { if (exp instanceof VariableExpression) { Variable accessedVariable = ((VariableExpression) exp).getAccessedVariable(); if (accessedVariable instanceof FieldNode) { return knownFields.contains(accessedVariable.getName()); } } if (exp instanceof PropertyExpression) { if (((PropertyExpression) exp).isImplicitThis() || "this".equals(((PropertyExpression) exp).getObjectExpression().getText())) { return knownFields.contains(((PropertyExpression) exp).getProperty().getText()); } } return false; }
@Override public void visitPropertyExpression(PropertyExpression expr) { result = new ExpressionInfo( TextRegion.of(expr), TextPosition.startOf(expr.getProperty()), expr.getPropertyAsString(), expr.isImplicitThis() ? Collections.<ExpressionInfo>emptyList() : Collections.singletonList(convert(expr.getObjectExpression()))); }
List<Receiver<String>> receivers = new LinkedList<Receiver<String>>(); List<Receiver<String>> owners = makeOwnerList(objectExpression); addReceivers(receivers, owners, pexp.isImplicitThis());
@Override public void visitPropertyExpression(PropertyExpression expr) { PropertyExpression conversion = new PropertyExpression( expr.isImplicitThis() ? expr.getObjectExpression() : convert(expr.getObjectExpression()), expr.getProperty(), expr.isSafe()); conversion.setSourcePosition(expr); conversion.setSpreadSafe(expr.isSpreadSafe()); conversion.setStatic(expr.isStatic()); conversion.setImplicitThis(expr.isImplicitThis()); result = record(conversion); }
public void fallbackAttributeOrPropertySite(PropertyExpression expression, Expression objectExpression, String name, MethodCallerMultiAdapter adapter) { if (controller.getCompileStack().isLHS()) controller.getOperandStack().box(); controller.getInvocationWriter().makeCall( expression, objectExpression, // receiver new CastExpression(ClassHelper.STRING_TYPE, expression.getProperty()), // messageName MethodCallExpression.NO_ARGUMENTS, adapter, expression.isSafe(), expression.isSpreadSafe(), expression.isImplicitThis() ); } }
private void checkThisAndSuperAsPropertyAccess(PropertyExpression expression) { if (expression.isImplicitThis()) return; String prop = expression.getPropertyAsString(); if (prop == null) return; if (!prop.equals("this") && !prop.equals("super")) return; ClassNode type = expression.getObjectExpression().getType(); if (expression.getObjectExpression() instanceof ClassExpression) { if (!(currentClass instanceof InnerClassNode) && !Traits.isTrait(type)) { addError("The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.", expression); return; } if (currentScope!=null && !currentScope.isInStaticContext() && Traits.isTrait(type) && "super".equals(prop) && directlyImplementsTrait(type)) { return; } ClassNode iterType = currentClass; while (iterType != null) { if (iterType.equals(type)) break; iterType = iterType.getOuterClass(); } if (iterType == null) { addError("The class '" + type.getName() + "' needs to be an outer class of '" + currentClass.getName() + "' when using '.this' or '.super'.", expression); } if ((currentClass.getModifiers() & Opcodes.ACC_STATIC) == 0) return; if (currentScope != null && !currentScope.isInStaticContext()) return; addError("The usage of 'Class.this' and 'Class.super' within static nested class '" + currentClass.getName() + "' is not allowed in a static context.", expression); } }
@Override public void visitPropertyExpression(PropertyExpression expr) { PropertyExpression result = new PropertyExpression( replaceExpr(expr.getObjectExpression()), replaceExpr(expr.getProperty()), expr.isSafe() ); result.setSpreadSafe(expr.isSpreadSafe()); result.setStatic(expr.isStatic()); result.setImplicitThis(expr.isImplicitThis()); result.setType(expr.getType()); result.setSourcePosition(expr); replaceVisitedExpressionWith(result); }
List<Receiver<String>> receivers = new LinkedList<Receiver<String>>(); List<Receiver<String>> owners = makeOwnerList(objectExpression); addReceivers(receivers, owners, pexp.isImplicitThis()); return true; MethodNode getter = findGetter(current, "get" + capName, pexp.isImplicitThis()); getter = allowStaticAccessToMember(getter, staticOnly); if (getter == null) getter = findGetter(current, "is" + capName, pexp.isImplicitThis()); getter = allowStaticAccessToMember(getter, staticOnly); final String setterName = "set" + capName;
@Override public void evaluateEqual(final BinaryExpression expression, final boolean defineVariable) { if (!defineVariable) { Expression leftExpression = expression.getLeftExpression(); if (leftExpression instanceof PropertyExpression) { PropertyExpression pexp = (PropertyExpression) leftExpression; if (makeSetProperty( pexp.getObjectExpression(), pexp.getProperty(), expression.getRightExpression(), pexp.isSafe(), pexp.isSpreadSafe(), pexp.isImplicitThis(), pexp instanceof AttributeExpression)) return; } } // GROOVY-5620: Spread safe/Null safe operator on LHS is not supported if (expression.getLeftExpression() instanceof PropertyExpression && ((PropertyExpression) expression.getLeftExpression()).isSpreadSafe() && StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType())) { // rewrite it so that it can be statically compiled transformSpreadOnLHS(expression); return; } super.evaluateEqual(expression, defineVariable); }
leftFieldName = ((FieldExpression) leftExpression).getFieldName(); } else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) { leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString(); FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
private boolean getField(PropertyExpression expression, Expression receiver, ClassNode receiverType, String name) { ClassNode classNode = controller.getClassNode(); boolean safe = expression.isSafe(); boolean implicitThis = expression.isImplicitThis(); if (makeGetField(receiver, receiverType, name, safe, implicitThis, samePackages(receiverType.getPackageName(), classNode.getPackageName()))) return true; if (receiver instanceof ClassExpression) { if (makeGetField(receiver, receiver.getType(), name, safe, implicitThis, samePackages(receiver.getType().getPackageName(), classNode.getPackageName()))) return true; if (makeGetPrivateFieldWithBridgeMethod(receiver, receiver.getType(), name, safe, implicitThis)) return true; } if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, name, safe, implicitThis)) return true; boolean isClassReceiver = false; if (isClassClassNodeWrappingConcreteType(receiverType)) { isClassReceiver = true; receiverType = receiverType.getGenericsTypes()[0].getType(); } if (isClassReceiver && makeGetField(receiver, CLASS_Type, name, safe, false, true)) return true; if (receiverType.isEnum()) { controller.getMethodVisitor().visitFieldInsn(GETSTATIC, BytecodeHelper.getClassInternalName(receiverType), name, BytecodeHelper.getTypeDescription(receiverType)); controller.getOperandStack().push(receiverType); return true; } return false; } }
new AttributeExpression(iteratorAsVar, spreadExpression.getProperty(), true): new PropertyExpression(iteratorAsVar, spreadExpression.getProperty(), true); pexp.setImplicitThis(spreadExpression.isImplicitThis()); pexp.setSourcePosition(spreadExpression); BinaryExpression assignment = new BinaryExpression(
PropertyExpression pexp = (PropertyExpression) exp; Expression object = pexp.getObjectExpression(); if (pexp.isImplicitThis() || "this".equals(object.getText())) { String propName = pexp.getPropertyAsString(); if (knownFields.contains(propName)) {
if (controller.isNotExplicitThisInClosure(expression.isImplicitThis())) { field = classNode.getDeclaredField(name); if (field==null && classNode instanceof InnerClassNode) { controller.getCallSiteWriter().makeGetPropertySite(objectExpression, propName, expression.isSafe(), expression.isImplicitThis()); } else if (adapter == getGroovyObjectProperty && !expression.isSpreadSafe()) { controller.getCallSiteWriter().makeGroovyObjectGetPropertySite(objectExpression, propName, expression.isSafe(), expression.isImplicitThis()); } else { controller.getCallSiteWriter().fallbackAttributeOrPropertySite(expression, objectExpression, propName, adapter);
/** * See {@link #visitingClosureBody} for the details of what this method is about. */ private Expression transformObjectExpression(PropertyExpression exp) { if (exp.isImplicitThis() && visitingClosureBody && !isLocalVariableExpression(exp.getObjectExpression())) { return CLOSURE_THIS; } else { return transform(exp.getObjectExpression()); } }
private void checkThisAndSuperAsPropertyAccess(PropertyExpression expression) { if (expression.isImplicitThis()) return; String prop = expression.getPropertyAsString(); if (prop == null) return; if (!prop.equals("this") && !prop.equals("super")) return; if (expression.getObjectExpression() instanceof ClassExpression) { if (!(currentClass instanceof InnerClassNode)) { addError("The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.", expression); return; } ClassNode type = expression.getObjectExpression().getType(); ClassNode iterType = currentClass; while (iterType != null) { if (iterType.equals(type)) break; iterType = iterType.getOuterClass(); } if (iterType == null) { addError("The class '" + type.getName() + "' needs to be an outer class of '" + currentClass.getName() + "' when using '.this' or '.super'.", expression); } if ((currentClass.getModifiers() & Opcodes.ACC_STATIC) == 0) return; if (!currentScope.isInStaticContext()) return; addError("The usage of 'Class.this' and 'Class.super' within static nested class '" + currentClass.getName() + "' is not allowed in a static context.", expression); } }
if (objectExpression instanceof VariableExpression) { VariableExpression ve = (VariableExpression) objectExpression; isExplicitThisOrSuper = !pe.isImplicitThis() && (ve.getName().equals("this") || ve.getName().equals("super")); if (isExplicitThisOrSuper && currentMethod != null && currentMethod.isStatic()) { addError("Non-static variable '" + ve.getName() + "' cannot be referenced from the static method " + currentMethod.getName() + ".", pe);
if (objectExpression instanceof VariableExpression) { VariableExpression ve = (VariableExpression) objectExpression; isExplicitThisOrSuper = !pe.isImplicitThis() && (ve.getName().equals("this") || ve.getName().equals("super")); if (isExplicitThisOrSuper && currentMethod != null && currentMethod.isStatic()) { addError("Non-static variable '" + ve.getName() + "' cannot be referenced from the static method " + currentMethod.getName() + ".", pe);
@Override public void visitPropertyExpression(PropertyExpression expr) { PropertyExpression result = new PropertyExpression( replaceExpr(expr.getObjectExpression()), replaceExpr(expr.getProperty()), expr.isSafe() ); result.setSpreadSafe(expr.isSpreadSafe()); result.setStatic(expr.isStatic()); result.setImplicitThis(expr.isImplicitThis()); result.setType(expr.getType()); result.setSourcePosition(expr); replaceVisitedExpressionWith(result); }