Java Code Examples for org.codehaus.groovy.ast.expr.Expression#getText()

The following examples show how to use org.codehaus.groovy.ast.expr.Expression#getText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression transformMethodCallOnThis(final MethodCallExpression call) {
    Expression method = call.getMethod();
    Expression arguments = call.getArguments();
    if (method instanceof ConstantExpression) {
        String methodName = method.getText();
        List<MethodNode> methods = traitClass.getMethods(methodName);
        for (MethodNode methodNode : methods) {
            if (methodName.equals(methodNode.getName()) && methodNode.isPrivate()) {
                if (inClosure) {
                    return transformPrivateMethodCallOnThisInClosure(call, arguments, methodName);
                }
                return transformPrivateMethodCallOnThis(call, arguments, methodName);
            }
        }
    }

    if (inClosure) {
        return transformMethodCallOnThisInClosure(call);
    }

    return transformMethodCallOnThisFallBack(call, method, arguments);

}
 
Example 2
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private ConstantExpression getConstantExpression(Expression exp, ClassNode attrType) {
    Expression result = exp;
    if (!(result instanceof ConstantExpression)) {
        result = transformInlineConstants(result, attrType);
    }
    if (result instanceof ConstantExpression) {
        return (ConstantExpression) result;
    }
    String base = "Expected '" + exp.getText() + "' to be an inline constant of type " + attrType.getName();
    if (exp instanceof PropertyExpression) {
        addError(base + " not a property expression", exp);
    } else if (exp instanceof VariableExpression && ((VariableExpression)exp).getAccessedVariable() instanceof FieldNode) {
        addError(base + " not a field expression", exp);
    } else {
        addError(base, exp);
    }
    ConstantExpression ret = new ConstantExpression(null);
    ret.setSourcePosition(exp);
    return ret;
}
 
Example 3
Source File: InvocationWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected String getMethodName(final Expression message) {
    String methodName = null;
    if (message instanceof CastExpression) {
        CastExpression msg = (CastExpression) message;
        if (msg.getType() == ClassHelper.STRING_TYPE) {
            final Expression methodExpr = msg.getExpression();
            if (methodExpr instanceof ConstantExpression) {
                methodName = methodExpr.getText();
            }
        }
    }

    if (methodName == null && message instanceof ConstantExpression) {
        ConstantExpression constantExpression = (ConstantExpression) message;
        methodName = constantExpression.getText();
    }
    return methodName;
}
 
Example 4
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void makeSingleArgumentCall(final Expression receiver, final String message, final Expression arguments, final boolean safe) {
    TypeChooser typeChooser = controller.getTypeChooser();
    ClassNode classNode = controller.getClassNode();
    ClassNode rType = typeChooser.resolveType(receiver, classNode);
    ClassNode aType = typeChooser.resolveType(arguments, classNode);
    if (trySubscript(receiver, message, arguments, rType, aType, safe)) {
        return;
    }
    // now try with flow type instead of declaration type
    rType = receiver.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    if (receiver instanceof VariableExpression && rType == null) {
        // TODO: can STCV be made smarter to avoid this check?
        VariableExpression ve = (VariableExpression) ((VariableExpression)receiver).getAccessedVariable();
        rType = ve.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
    }
    if (rType!=null && trySubscript(receiver, message, arguments, rType, aType, safe)) {
        return;
    }
    // todo: more cases
    throw new GroovyBugError(
            "At line " + receiver.getLineNumber() + " column " + receiver.getColumnNumber() + "\n" +
            "On receiver: " + receiver.getText() + " with message: " + message + " and arguments: " + arguments.getText() + "\n" +
            "This method should not have been called. Please try to create a simple example reproducing\n" +
            "this error and file a bug report at https://issues.apache.org/jira/browse/GROOVY");
}
 
Example 5
Source File: ContractInputProposalsCodeVisitorSupport.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    proposals = completionComputer.computeCompletionProposals(context, monitor);
    final BinaryExpression binaryExpression = (BinaryExpression) contentAssistContext.getPerceivedCompletionNode();
    final Expression leftExpression = binaryExpression.getLeftExpression();
    String multipleInputName = null;
    if (leftExpression instanceof PropertyExpression) {
        final PropertyExpression propertyExpr = (PropertyExpression) leftExpression;
        final Expression objectExpression = propertyExpr.getProperty();
        multipleInputName = objectExpression.getText();
    } else if (leftExpression instanceof VariableExpression) {
        multipleInputName = ((VariableExpression) leftExpression).getName();
    }
    if (multipleInputName != null) {
        final ContractInput multipleInput = getInputWithName(multipleInputName, inputs);
        final ContractInput copy = EcoreUtil.copy(multipleInput);
        copy.setMultiple(false);
        final String fullyQualifiedType = ExpressionHelper.getContractInputReturnType(copy);
        if (fullyQualifiedType != null && prefix.isEmpty()) {
            proposals = getMethodProposals(contentAssistContext, context, inputs, prefix, multipleInputName, fullyQualifiedType);
        }
    }
}
 
Example 6
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 5 votes vote down vote up
/** Handles a groovy BinaryExpression such as foo = true, or bar.baz.foo = true. */
@Override
public void visitBinaryExpression(BinaryExpression binaryExpression) {
  if (!methodCallStack.isEmpty()) {
    MethodCallExpression call = Iterables.getLast(methodCallStack);
    String parent = call.getMethodAsString();
    String parentParent = getParentParent();
    Expression leftExpression = binaryExpression.getLeftExpression();
    Expression rightExpression = binaryExpression.getRightExpression();
    if (rightExpression instanceof ConstantExpression
        && (leftExpression instanceof PropertyExpression
            || leftExpression instanceof VariableExpression)) {
      String value = rightExpression.getText();
      String property = "";
      if (leftExpression instanceof PropertyExpression) {
        Expression leftPropertyExpression = ((PropertyExpression) leftExpression).getProperty();
        if (!(leftPropertyExpression instanceof ConstantExpression)) {
          return;
        }
        property = ((ConstantExpression) leftPropertyExpression).getText();
        Expression leftObjectExpression =
            ((PropertyExpression) leftExpression).getObjectExpression();
        parentParent = parent;
        parent = getValidParentString(leftObjectExpression);
        if (leftObjectExpression instanceof PropertyExpression) {
          parentParent =
              getValidParentString(
                  ((PropertyExpression) leftObjectExpression).getObjectExpression());
        }
      } else {
        property = ((VariableExpression) leftExpression).getName();
      }
      checkDslPropertyAssignment(
          property, value, parent, parentParent, binaryExpression.getLineNumber());
    }
  }
  super.visitBinaryExpression(binaryExpression);
}
 
Example 7
Source File: LogASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static String lookupLogFieldName(AnnotationNode logAnnotation) {
    Expression member = logAnnotation.getMember("value");
    if (member != null && member.getText() != null) {
        return member.getText();
    } else {
        return "log";
    }
}
 
Example 8
Source File: LogASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static String lookupCategoryName(AnnotationNode logAnnotation) {
    Expression member = logAnnotation.getMember("category");
    if (member != null && member.getText() != null) {
        return member.getText();
    }
    return DEFAULT_CATEGORY_NAME;
}
 
Example 9
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void printValue(PrintWriter out, Expression re, boolean assumeClass) {
    if (assumeClass) {
        if (re.getType().getName().equals("groovy.lang.Closure")) {
            out.print("groovy.lang.Closure.class");
            return;
        }
        String className = re.getText();
        out.print(className);
        if (!className.endsWith(".class")) {
            out.print(".class");
        }
    } else {
        if (re instanceof ConstantExpression) {
            ConstantExpression ce = (ConstantExpression) re;
            Object value = ce.getValue();
            if (ClassHelper.STRING_TYPE.equals(ce.getType())) {
                out.print(formatString((String)value));
            } else if (ClassHelper.char_TYPE.equals(ce.getType()) || ClassHelper.Character_TYPE.equals(ce.getType())) {
                out.print(formatChar(value.toString()));
            } else if (ClassHelper.long_TYPE.equals(ce.getType())) {
                out.print("" + value + "L");
            } else if (ClassHelper.float_TYPE.equals(ce.getType())) {
                out.print("" + value + "f");
            } else if (ClassHelper.double_TYPE.equals(ce.getType())) {
                out.print("" + value + "d");
            } else {
                out.print(re.getText());
            }
        } else {
            out.print(re.getText());
        }
    }
}
 
Example 10
Source File: BaseStreamCompilerAutoConfiguration.java    From spring-cloud-cli with Apache License 2.0 5 votes vote down vote up
static boolean isTransport(ClassNode node, String type) {
	for (AnnotationNode annotationNode : node.getAnnotations()) {
		String annotation = "EnableBinding";
		if (PatternMatchUtils.simpleMatch(annotation,
				annotationNode.getClassNode().getName())) {
			Expression expression = annotationNode.getMembers().get("transport");
			String transport = expression == null ? "rabbit" : expression.getText();
			if (transport != null) {
				transport = SystemPropertyUtils.resolvePlaceholders(transport);
			}
			return transport.equals(type);
		}
	}
	return false;
}
 
Example 11
Source File: StaticTypeCheckingSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static boolean checkCompatibleAssignmentTypes(final ClassNode left, final ClassNode right, final Expression rightExpression, final boolean allowConstructorCoercion) {
    ClassNode leftRedirect = left.redirect();
    ClassNode rightRedirect = right.redirect();
    if (leftRedirect == rightRedirect) return true;

    if (leftRedirect.isArray() && rightRedirect.isArray()) {
        return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType(), rightExpression, false);
    }

    if (right == VOID_TYPE || right == void_WRAPPER_TYPE) {
        return left == VOID_TYPE || left == void_WRAPPER_TYPE;
    }

    if ((isNumberType(rightRedirect) || WideningCategories.isNumberCategory(rightRedirect))) {
        if (BigDecimal_TYPE == leftRedirect) {
            // any number can be assigned to a big decimal
            return true;
        }
        if (BigInteger_TYPE == leftRedirect) {
            return WideningCategories.isBigIntCategory(getUnwrapper(rightRedirect)) || rightRedirect.isDerivedFrom(BigInteger_TYPE);
        }
    }

    // if rightExpression is null and leftExpression is not a primitive type, it's ok
    boolean rightExpressionIsNull = (rightExpression instanceof ConstantExpression && ((ConstantExpression) rightExpression).isNullExpression());
    if (rightExpressionIsNull && !isPrimitiveType(left)) {
        return true;
    }

    // on an assignment everything that can be done by a GroovyCast is allowed

    // anything can be assigned to an Object, String, Boolean or Class typed variable
    if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;

    // char as left expression
    if (leftRedirect == char_TYPE && rightRedirect == STRING_TYPE) {
        if (rightExpression instanceof ConstantExpression) {
            String value = rightExpression.getText();
            return value.length() == 1;
        }
    }
    if (leftRedirect == Character_TYPE && (rightRedirect == STRING_TYPE || rightExpressionIsNull)) {
        return rightExpressionIsNull || (rightExpression instanceof ConstantExpression && rightExpression.getText().length() == 1);
    }

    // if left is Enum and right is String or GString we do valueOf
    if (leftRedirect.isDerivedFrom(Enum_Type) && (rightRedirect == GSTRING_TYPE || rightRedirect == STRING_TYPE)) {
        return true;
    }

    // if right is array, map or collection we try invoking the constructor
    if (allowConstructorCoercion && isGroovyConstructorCompatible(rightExpression)) {
        // TODO: in case of the array we could maybe make a partial check
        if (leftRedirect.isArray() && rightRedirect.isArray()) {
            return checkCompatibleAssignmentTypes(leftRedirect.getComponentType(), rightRedirect.getComponentType());
        } else if (rightRedirect.isArray() && !leftRedirect.isArray()) {
            return false;
        }
        return true;
    }

    // simple check on being subclass
    if (right.isDerivedFrom(left) || (left.isInterface() && right.implementsInterface(left))) return true;

    // if left and right are primitives or numbers allow
    if (isPrimitiveType(leftRedirect) && isPrimitiveType(rightRedirect)) return true;
    if (isNumberType(leftRedirect) && isNumberType(rightRedirect)) return true;

    // left is a float/double and right is a BigDecimal
    if (WideningCategories.isFloatingCategory(leftRedirect) && BigDecimal_TYPE.equals(rightRedirect)) {
        return true;
    }

    if (GROOVY_OBJECT_TYPE.equals(leftRedirect) && isBeingCompiled(right)) {
        return true;
    }

    if (left.isGenericsPlaceHolder()) {
        // GROOVY-7307
        GenericsType[] genericsTypes = left.getGenericsTypes();
        if (genericsTypes != null && genericsTypes.length == 1) {
            // should always be the case, but safe guard is better
            return genericsTypes[0].isCompatibleWith(right);
        }
    }

    // GROOVY-7316: It is an apparently legal thing to allow this. It's not type safe, but it is allowed...
    return right.isGenericsPlaceHolder();
}