Java Code Examples for org.codehaus.groovy.ast.expr.ConstantExpression#getValue()
The following examples show how to use
org.codehaus.groovy.ast.expr.ConstantExpression#getValue() .
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: DefaultScriptCompilationHandler.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
private boolean isEmpty(SourceUnit source) { if (!source.getAST().getMethods().isEmpty()) { return false; } List<Statement> statements = source.getAST().getStatementBlock().getStatements(); if (statements.size() > 1) { return false; } if (statements.isEmpty()) { return true; } Statement statement = statements.get(0); if (statement instanceof ReturnStatement) { ReturnStatement returnStatement = (ReturnStatement) statement; if (returnStatement.getExpression() instanceof ConstantExpression) { ConstantExpression constantExpression = (ConstantExpression) returnStatement.getExpression(); if (constantExpression.getValue() == null) { return true; } } } return false; }
Example 2
Source File: DefaultScriptCompilationHandler.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
private boolean isEmpty(SourceUnit source) { if (!source.getAST().getMethods().isEmpty()) { return false; } List<Statement> statements = source.getAST().getStatementBlock().getStatements(); if (statements.size() > 1) { return false; } if (statements.isEmpty()) { return true; } Statement statement = statements.get(0); if (statement instanceof ReturnStatement) { ReturnStatement returnStatement = (ReturnStatement) statement; if (returnStatement.getExpression() instanceof ConstantExpression) { ConstantExpression constantExpression = (ConstantExpression) returnStatement.getExpression(); if (constantExpression.getValue() == null) { return true; } } } return false; }
Example 3
Source File: DefaultScriptCompilationHandler.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
private boolean isEmpty(SourceUnit source) { if (!source.getAST().getMethods().isEmpty()) { return false; } List<Statement> statements = source.getAST().getStatementBlock().getStatements(); if (statements.size() > 1) { return false; } if (statements.isEmpty()) { return true; } Statement statement = statements.get(0); if (statement instanceof ReturnStatement) { ReturnStatement returnStatement = (ReturnStatement) statement; if (returnStatement.getExpression() instanceof ConstantExpression) { ConstantExpression constantExpression = (ConstantExpression) returnStatement.getExpression(); if (constantExpression.getValue() == null) { return true; } } } return false; }
Example 4
Source File: StatementWriter.java From groovy with Apache License 2.0 | 6 votes |
private void visitConditionOfLoopingStatement(final BooleanExpression expression, final Label breakLabel, final MethodVisitor mv) { boolean boolHandled = false; if (expression.getExpression() instanceof ConstantExpression) { ConstantExpression constant = (ConstantExpression) expression.getExpression(); if (constant.getValue() == Boolean.TRUE) { boolHandled = true; // do nothing } else if (constant.getValue() == Boolean.FALSE) { boolHandled = true; mv.visitJumpInsn(GOTO, breakLabel); } } if (!boolHandled) { expression.visit(controller.getAcg()); controller.getOperandStack().jump(IFEQ, breakLabel); } }
Example 5
Source File: ResolveVisitor.java From groovy with Apache License 2.0 | 6 votes |
private static Expression transformInlineConstants(final Expression exp) { if (exp instanceof AnnotationConstantExpression) { ConstantExpression ce = (ConstantExpression) exp; if (ce.getValue() instanceof AnnotationNode) { // replicate a little bit of AnnotationVisitor here // because we can't wait until later to do this AnnotationNode an = (AnnotationNode) ce.getValue(); for (Map.Entry<String, Expression> member : an.getMembers().entrySet()) { member.setValue(transformInlineConstants(member.getValue())); } } } else { return ExpressionUtils.transformInlineConstants(exp); } return exp; }
Example 6
Source File: DefaultScriptCompilationHandler.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
private boolean isEmpty(SourceUnit source) { if (!source.getAST().getMethods().isEmpty()) { return false; } List<Statement> statements = source.getAST().getStatementBlock().getStatements(); if (statements.size() > 1) { return false; } if (statements.isEmpty()) { return true; } Statement statement = statements.get(0); if (statement instanceof ReturnStatement) { ReturnStatement returnStatement = (ReturnStatement) statement; if (returnStatement.getExpression() instanceof ConstantExpression) { ConstantExpression constantExpression = (ConstantExpression) returnStatement.getExpression(); if (constantExpression.getValue() == null) { return true; } } } return false; }
Example 7
Source File: AnnotationConstantsVisitor.java From groovy with Apache License 2.0 | 5 votes |
private static Expression revertType(Expression val, ClassNode returnWrapperType) { ConstantExpression ce = (ConstantExpression) val; if (ClassHelper.Character_TYPE.equals(returnWrapperType) && ClassHelper.STRING_TYPE.equals(val.getType())) { return configure(val, Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) val)); } ClassNode valWrapperType = ClassHelper.getWrapper(val.getType()); if (ClassHelper.Integer_TYPE.equals(valWrapperType)) { Integer i = (Integer) ce.getValue(); if (ClassHelper.Character_TYPE.equals(returnWrapperType)) { return configure(val, new ConstantExpression((char) i.intValue(), true)); } if (ClassHelper.Short_TYPE.equals(returnWrapperType)) { return configure(val, new ConstantExpression(i.shortValue(), true)); } if (ClassHelper.Byte_TYPE.equals(returnWrapperType)) { return configure(val, new ConstantExpression(i.byteValue(), true)); } } if (ClassHelper.BigDecimal_TYPE.equals(valWrapperType)) { BigDecimal bd = (BigDecimal) ce.getValue(); if (ClassHelper.Float_TYPE.equals(returnWrapperType)) { return configure(val, new ConstantExpression(bd.floatValue(), true)); } if (ClassHelper.Double_TYPE.equals(returnWrapperType)) { return configure(val, new ConstantExpression(bd.doubleValue(), true)); } } return null; }
Example 8
Source File: NewifyASTTransformation.java From groovy with Apache License 2.0 | 5 votes |
private Pattern determineClassNamePattern(Expression expr) { if (!(expr instanceof ConstantExpression)) { return null; } final ConstantExpression constExpr = (ConstantExpression) expr; final String text = constExpr.getText(); if (constExpr.getValue() == null || text.isEmpty()) { return null; } try { final Pattern pattern = Pattern.compile(text); return pattern; } catch (PatternSyntaxException e) { addError("Invalid class name pattern: " + e.getMessage(), expr); return null; } }
Example 9
Source File: ClassCompletionVerifier.java From groovy with Apache License 2.0 | 5 votes |
private void checkStringExceedingMaximumLength(ConstantExpression expression) { Object value = expression.getValue(); if (value instanceof String) { String s = (String) value; if (s.length() > 65535) { addError("String too long. The given string is " + s.length() + " Unicode code units long, but only a maximum of 65535 is allowed.", expression); } } }
Example 10
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitField(final FieldNode fieldNode) { onLineNumber(fieldNode, "visitField: " + fieldNode.getName()); ClassNode t = fieldNode.getType(); String signature = BytecodeHelper.getGenericsBounds(t); Expression initialValueExpression = fieldNode.getInitialValueExpression(); ConstantExpression cexp = initialValueExpression instanceof ConstantExpression? (ConstantExpression) initialValueExpression :null; if (cexp!=null) { cexp = Verifier.transformToPrimitiveConstantIfPossible(cexp); } Object value = cexp != null && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(t) && fieldNode.isStatic() && fieldNode.isFinal() ? cexp.getValue() : null; // GROOVY-5150 if (value != null) { // byte, char and short require an extra cast if (ClassHelper.byte_TYPE.equals(t) || ClassHelper.short_TYPE.equals(t)) { value = ((Number) value).intValue(); } else if (ClassHelper.char_TYPE.equals(t)) { value = Integer.valueOf((Character)value); } } FieldVisitor fv = classVisitor.visitField( fieldNode.getModifiers(), fieldNode.getName(), BytecodeHelper.getTypeDescription(t), signature, value); visitAnnotations(fieldNode, fv); fv.visitEnd(); }
Example 11
Source File: OperandStack.java From groovy with Apache License 2.0 | 5 votes |
/** * load the constant on the operand stack. */ public void pushConstant(ConstantExpression expression) { MethodVisitor mv = controller.getMethodVisitor(); Object value = expression.getValue(); ClassNode origType = expression.getType().redirect(); ClassNode type = ClassHelper.getUnwrapper(origType); boolean boxing = origType!=type; boolean asPrimitive = boxing || ClassHelper.isPrimitiveType(type); if (value == null) { mv.visitInsn(ACONST_NULL); } else if (boxing && value instanceof Boolean) { // special path for boxed boolean Boolean bool = (Boolean) value; String text = bool ? "TRUE" : "FALSE"; mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", text, "Ljava/lang/Boolean;"); boxing = false; type = origType; } else if (asPrimitive) { pushPrimitiveConstant(mv, value, type); } else if (value instanceof BigDecimal) { newInstance(mv, value); } else if (value instanceof BigInteger) { newInstance(mv, value); } else if (value instanceof String) { mv.visitLdcInsn(value); } else { throw new ClassGeneratorException( "Cannot generate bytecode for constant: " + value + " of type: " + type.getName()); } push(type); if (boxing) box(); }
Example 12
Source File: OperandStack.java From groovy with Apache License 2.0 | 5 votes |
public void pushDynamicName(Expression name) { if (name instanceof ConstantExpression) { ConstantExpression ce = (ConstantExpression) name; Object value = ce.getValue(); if (value instanceof String) { pushConstant(ce); return; } } new CastExpression(ClassHelper.STRING_TYPE, name).visit(controller.getAcg()); }
Example 13
Source File: MacroGroovyMethods.java From groovy with Apache License 2.0 | 5 votes |
@Macro public static Expression macro(MacroContext macroContext, PropertyExpression phaseExpression, ConstantExpression asIsConstantExpression, ClosureExpression closureExpression) { if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) { macroContext.getSourceUnit().addError(new SyntaxException("Macro closure arguments are not allowed" + '\n', closureExpression)); return macroContext.getCall(); } final String source; try { source = ClosureUtils.convertClosureToSource(macroContext.getSourceUnit().getSource(), closureExpression); } catch (Exception e) { throw new RuntimeException(e); } BlockStatement closureBlock = (BlockStatement) closureExpression.getCode(); Boolean asIs = (Boolean) asIsConstantExpression.getValue(); return callX( propX(classX(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"), "macro", args( phaseExpression != null ? phaseExpression : constX(null), asIsConstantExpression, constX(source), buildSubstitutions(macroContext.getSourceUnit(), closureExpression), classX(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false)) ) ); }
Example 14
Source File: JavaStubGenerator.java From groovy with Apache License 2.0 | 5 votes |
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 15
Source File: GrabAnnotationTransformation.java From groovy with Apache License 2.0 | 5 votes |
private void extractGrab(Expression init, ConstantExpression ce) { if (ce.getValue() instanceof AnnotationNode) { AnnotationNode annotation = (AnnotationNode) ce.getValue(); if ((init != null) && (annotation.getMember("initClass") != null)) { annotation.setMember("initClass", init); } String name = annotation.getClassNode().getName(); if ((GRAB_CLASS_NAME.equals(name)) || (allowShortGrab && GRAB_SHORT_NAME.equals(name)) || (grabAliases.contains(name))) { grabAnnotations.add(annotation); } if ((GRABEXCLUDE_CLASS_NAME.equals(name)) || (allowShortGrabExcludes && GRABEXCLUDE_SHORT_NAME.equals(name)) || (grabExcludeAliases.contains(name))) { grabExcludeAnnotations.add(annotation); } if ((GRABCONFIG_CLASS_NAME.equals(name)) || (allowShortGrabConfig && GRABCONFIG_SHORT_NAME.equals(name)) || (grabConfigAliases.contains(name))) { grabConfigAnnotations.add(annotation); } if ((GRABRESOLVER_CLASS_NAME.equals(name)) || (allowShortGrabResolver && GRABRESOLVER_SHORT_NAME.equals(name)) || (grabResolverAliases.contains(name))) { grabResolverAnnotations.add(annotation); } } }
Example 16
Source File: ExpressionUtils.java From groovy with Apache License 2.0 | 4 votes |
private static Number safeNumber(final ConstantExpression constX) { Object value = constX.getValue(); if (value instanceof Number) return (Number) value; return null; }
Example 17
Source File: JavaStubGenerator.java From groovy with Apache License 2.0 | 4 votes |
private void printSpecialConstructorArgs(PrintWriter out, ConstructorNode node, ConstructorCallExpression constrCall) { // Select a constructor from our class, or super-class which is legal to call, // then write out an invoke w/nulls using casts to avoid ambiguous crapo Parameter[] params = selectAccessibleConstructorFromSuper(node); if (params != null) { out.print("super ("); for (int i = 0; i < params.length; i++) { printDefaultValue(out, params[i].getType()); if (i + 1 < params.length) { out.print(", "); } } out.println(");"); return; } // Otherwise try the older method based on the constructor's call expression Expression arguments = constrCall.getArguments(); if (constrCall.isSuperCall()) { out.print("super("); } else { out.print("this("); } // Else try to render some arguments if (arguments instanceof ArgumentListExpression) { ArgumentListExpression argumentListExpression = (ArgumentListExpression) arguments; List<Expression> args = argumentListExpression.getExpressions(); for (Expression arg : args) { if (arg instanceof ConstantExpression) { ConstantExpression expression = (ConstantExpression) arg; Object o = expression.getValue(); if (o instanceof String) { out.print("(String)null"); } else { out.print(expression.getText()); } } else { ClassNode type = getConstructorArgumentType(arg, node); printDefaultValue(out, type); } if (arg != args.get(args.size() - 1)) { out.print(", "); } } } out.println(");"); }
Example 18
Source File: JavaStubGenerator.java From groovy with Apache License 2.0 | 4 votes |
private String getAnnotationValue(Object memberValue) { String val = "null"; if (memberValue instanceof ListExpression) { StringBuilder sb = new StringBuilder("{"); boolean first = true; ListExpression le = (ListExpression) memberValue; for (Expression e : le.getExpressions()) { if (first) first = false; else sb.append(","); sb.append(getAnnotationValue(e)); } sb.append("}"); val = sb.toString(); } else if (memberValue instanceof ConstantExpression) { ConstantExpression ce = (ConstantExpression) memberValue; Object constValue = ce.getValue(); if (constValue instanceof AnnotationNode) { Writer writer = new StringBuilderWriter(); PrintWriter out = new PrintWriter(writer); printAnnotation(out, (AnnotationNode) constValue); val = writer.toString(); } else if (constValue instanceof Number || constValue instanceof Boolean) val = constValue.toString(); else val = "\"" + escapeSpecialChars(constValue.toString()) + "\""; } else if (memberValue instanceof PropertyExpression) { // assume must be static class field or enum value or class that Java can resolve val = ((Expression) memberValue).getText(); } else if (memberValue instanceof VariableExpression) { val = ((Expression) memberValue).getText(); //check for an alias ImportNode alias = currentModule.getStaticImports().get(val); if (alias != null) val = alias.getClassName() + "." + alias.getFieldName(); } else if (memberValue instanceof ClosureExpression) { // annotation closure; replaced with this specific class literal to cover the // case where annotation type uses Class<? extends Closure> for the closure's type val = "groovy.lang.Closure.class"; } else if (memberValue instanceof ClassExpression) { val = ((Expression) memberValue).getText() + ".class"; } return val; }
Example 19
Source File: GroovyVirtualSourceProvider.java From netbeans with Apache License 2.0 | 4 votes |
private void genSpecialConstructorArgs(PrintWriter out, ConstructorNode node, ConstructorCallExpression constrCall) { // Select a constructor from our class, or super-class which is legal to call, // then write out an invoke w/nulls using casts to avoid abigous crapo Parameter[] params = selectAccessibleConstructorFromSuper(node); if (params != null) { out.print("super ("); for (int i = 0; i < params.length; i++) { printDefaultValue(out, params[i].getType()); if (i + 1 < params.length) { out.print(", "); } } out.println(");"); return; } // Otherwise try the older method based on the constructor's call expression Expression arguments = constrCall.getArguments(); if (constrCall.isSuperCall()) { out.print("super("); } else { out.print("this("); } // Else try to render some arguments if (arguments instanceof ArgumentListExpression) { ArgumentListExpression argumentListExpression = (ArgumentListExpression) arguments; List<Expression> args = argumentListExpression.getExpressions(); for (Expression arg : args) { if (arg instanceof ConstantExpression) { ConstantExpression expression = (ConstantExpression) arg; Object o = expression.getValue(); if (o instanceof String) { out.print("(String)null"); } else { out.print(expression.getText()); } } else { printDefaultValue(out, arg.getType()); } if (arg != args.get(args.size() - 1)) { out.print(", "); } } } out.println(");"); }