Search in sources :

Example 1 with STRING_TYPE

use of org.codehaus.groovy.ast.ClassHelper.STRING_TYPE in project groovy by apache.

the class StaticTypeCheckingSupport method evaluateExpression.

/**
 * A helper method that can be used to evaluate expressions as found in annotation
 * parameters. For example, it will evaluate a constant, be it referenced directly as
 * an integer or as a reference to a field.
 * <p>
 * If this method throws an exception, then the expression cannot be evaluated on its own.
 *
 * @param expr   the expression to be evaluated
 * @param config the compiler configuration
 * @return the result of the expression
 */
public static Object evaluateExpression(final Expression expr, final CompilerConfiguration config) {
    Expression ce = expr instanceof CastExpression ? ((CastExpression) expr).getExpression() : expr;
    if (ce instanceof ConstantExpression) {
        if (expr.getType().equals(ce.getType()))
            return ((ConstantExpression) ce).getValue();
    } else if (ce instanceof ListExpression) {
        if (expr.getType().isArray() && expr.getType().getComponentType().equals(STRING_TYPE))
            return ((ListExpression) ce).getExpressions().stream().map(e -> evaluateExpression(e, config)).toArray(String[]::new);
    }
    String className = "Expression$" + UUID.randomUUID().toString().replace('-', '$');
    ClassNode simpleClass = new ClassNode(className, Opcodes.ACC_PUBLIC, OBJECT_TYPE);
    addGeneratedMethod(simpleClass, "eval", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new ReturnStatement(expr));
    // adjust configuration so class can be inspected by this JVM
    CompilerConfiguration cc = new CompilerConfiguration(config);
    // unlikely to be required by expression
    cc.setPreviewFeatures(false);
    cc.setTargetBytecode(CompilerConfiguration.DEFAULT.getTargetBytecode());
    CompilationUnit cu = new CompilationUnit(cc);
    try {
        cu.addClassNode(simpleClass);
        cu.compile(Phases.CLASS_GENERATION);
        List<GroovyClass> classes = cu.getClasses();
        Class<?> aClass = cu.getClassLoader().defineClass(className, classes.get(0).getBytes());
        try {
            return aClass.getMethod("eval").invoke(null);
        } catch (ReflectiveOperationException e) {
            throw new GroovyBugError(e);
        }
    } finally {
        closeQuietly(cu.getClassLoader());
    }
}
Also used : Arrays(java.util.Arrays) BigInteger_TYPE(org.codehaus.groovy.ast.ClassHelper.BigInteger_TYPE) DEPRECATED_TYPE(org.codehaus.groovy.ast.ClassHelper.DEPRECATED_TYPE) COMPARE_GREATER_THAN(org.codehaus.groovy.syntax.Types.COMPARE_GREATER_THAN) GSTRING_TYPE(org.codehaus.groovy.ast.ClassHelper.GSTRING_TYPE) WideningCategories.isFloatingCategory(org.codehaus.groovy.ast.tools.WideningCategories.isFloatingCategory) Character_TYPE(org.codehaus.groovy.ast.ClassHelper.Character_TYPE) ClassHelper.make(org.codehaus.groovy.ast.ClassHelper.make) POWER_EQUAL(org.codehaus.groovy.syntax.Types.POWER_EQUAL) Matcher(java.util.regex.Matcher) INTDIV_EQUAL(org.codehaus.groovy.syntax.Types.INTDIV_EQUAL) Map(java.util.Map) COMPARE_GREATER_THAN_EQUAL(org.codehaus.groovy.syntax.Types.COMPARE_GREATER_THAN_EQUAL) VOID_TYPE(org.codehaus.groovy.ast.ClassHelper.VOID_TYPE) ClassHelper.isBigIntegerType(org.codehaus.groovy.ast.ClassHelper.isBigIntegerType) Traits(org.codehaus.groovy.transform.trait.Traits) ClassHelper.char_TYPE(org.codehaus.groovy.ast.ClassHelper.char_TYPE) COMPARE_TO(org.codehaus.groovy.syntax.Types.COMPARE_TO) COLLECTION_TYPE(org.codehaus.groovy.ast.ClassHelper.COLLECTION_TYPE) BITWISE_AND(org.codehaus.groovy.syntax.Types.BITWISE_AND) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PLUS(org.codehaus.groovy.syntax.Types.PLUS) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Set(java.util.Set) LOGICAL_AND(org.codehaus.groovy.syntax.Types.LOGICAL_AND) ClassHelper.getUnwrapper(org.codehaus.groovy.ast.ClassHelper.getUnwrapper) RIGHT_SHIFT_EQUAL(org.codehaus.groovy.syntax.Types.RIGHT_SHIFT_EQUAL) CLASS_Type(org.codehaus.groovy.ast.ClassHelper.CLASS_Type) COMPARE_NOT_EQUAL(org.codehaus.groovy.syntax.Types.COMPARE_NOT_EQUAL) OBJECT_TYPE(org.codehaus.groovy.ast.ClassHelper.OBJECT_TYPE) ClassHelper.makeWithoutCaching(org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching) ClassHelper.byte_TYPE(org.codehaus.groovy.ast.ClassHelper.byte_TYPE) ClassNodeUtils.samePackageName(org.apache.groovy.ast.tools.ClassNodeUtils.samePackageName) STRING_TYPE(org.codehaus.groovy.ast.ClassHelper.STRING_TYPE) ClassHelper.long_TYPE(org.codehaus.groovy.ast.ClassHelper.long_TYPE) RIGHT_SHIFT_UNSIGNED(org.codehaus.groovy.syntax.Types.RIGHT_SHIFT_UNSIGNED) LEFT_SQUARE_BRACKET(org.codehaus.groovy.syntax.Types.LEFT_SQUARE_BRACKET) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ClassHelper.getWrapper(org.codehaus.groovy.ast.ClassHelper.getWrapper) PLUS_EQUAL(org.codehaus.groovy.syntax.Types.PLUS_EQUAL) RIGHT_SHIFT(org.codehaus.groovy.syntax.Types.RIGHT_SHIFT) DefaultGroovyMethods.asBoolean(org.codehaus.groovy.runtime.DefaultGroovyMethods.asBoolean) COMPARE_NOT_IN(org.codehaus.groovy.syntax.Types.COMPARE_NOT_IN) COMPARE_EQUAL(org.codehaus.groovy.syntax.Types.COMPARE_EQUAL) INTDIV(org.codehaus.groovy.syntax.Types.INTDIV) DIVIDE_EQUAL(org.codehaus.groovy.syntax.Types.DIVIDE_EQUAL) MINUS(org.codehaus.groovy.syntax.Types.MINUS) TreeSet(java.util.TreeSet) MOD_EQUAL(org.codehaus.groovy.syntax.Types.MOD_EQUAL) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) BITWISE_OR_EQUAL(org.codehaus.groovy.syntax.Types.BITWISE_OR_EQUAL) LOGICAL_OR(org.codehaus.groovy.syntax.Types.LOGICAL_OR) DefaultGroovyMethodsSupport.closeQuietly(org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport.closeQuietly) COMPARE_IDENTICAL(org.codehaus.groovy.syntax.Types.COMPARE_IDENTICAL) MethodNode(org.codehaus.groovy.ast.MethodNode) BITWISE_AND_EQUAL(org.codehaus.groovy.syntax.Types.BITWISE_AND_EQUAL) Expression(org.codehaus.groovy.ast.expr.Expression) ExpressionUtils.isNullConstant(org.apache.groovy.ast.tools.ExpressionUtils.isNullConstant) ClassHelper.short_TYPE(org.codehaus.groovy.ast.ClassHelper.short_TYPE) KEYWORD_IN(org.codehaus.groovy.syntax.Types.KEYWORD_IN) LinkedHashSet(java.util.LinkedHashSet) Opcodes(org.objectweb.asm.Opcodes) Parameter(org.codehaus.groovy.ast.Parameter) POWER(org.codehaus.groovy.syntax.Types.POWER) ClassNode(org.codehaus.groovy.ast.ClassNode) ClassHelper.isStringType(org.codehaus.groovy.ast.ClassHelper.isStringType) ClassHelper.float_TYPE(org.codehaus.groovy.ast.ClassHelper.float_TYPE) GenericsType(org.codehaus.groovy.ast.GenericsType) ClassHelper.isPrimitiveBoolean(org.codehaus.groovy.ast.ClassHelper.isPrimitiveBoolean) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) Phases(org.codehaus.groovy.control.Phases) ClassHelper.isClassType(org.codehaus.groovy.ast.ClassHelper.isClassType) ClassHelper.isPrimitiveType(org.codehaus.groovy.ast.ClassHelper.isPrimitiveType) ClassHelper.double_TYPE(org.codehaus.groovy.ast.ClassHelper.double_TYPE) ClassHelper.isGroovyObjectType(org.codehaus.groovy.ast.ClassHelper.isGroovyObjectType) Float_TYPE(org.codehaus.groovy.ast.ClassHelper.Float_TYPE) MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl) WideningCategories.isNumberCategory(org.codehaus.groovy.ast.tools.WideningCategories.isNumberCategory) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassHelper.isWrapperBoolean(org.codehaus.groovy.ast.ClassHelper.isWrapperBoolean) KEYWORD_INSTANCEOF(org.codehaus.groovy.syntax.Types.KEYWORD_INSTANCEOF) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ClassHelper.isGStringType(org.codehaus.groovy.ast.ClassHelper.isGStringType) GenericsTypeName(org.codehaus.groovy.ast.GenericsType.GenericsTypeName) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Long_TYPE(org.codehaus.groovy.ast.ClassHelper.Long_TYPE) LEFT_SHIFT_EQUAL(org.codehaus.groovy.syntax.Types.LEFT_SHIFT_EQUAL) Number_TYPE(org.codehaus.groovy.ast.ClassHelper.Number_TYPE) ClassHelper.findSAM(org.codehaus.groovy.ast.ClassHelper.findSAM) Enum_Type(org.codehaus.groovy.ast.ClassHelper.Enum_Type) ClassHelper.isBigDecimalType(org.codehaus.groovy.ast.ClassHelper.isBigDecimalType) Variable(org.codehaus.groovy.ast.Variable) ClassHelper.isSAMType(org.codehaus.groovy.ast.ClassHelper.isSAMType) MULTIPLY_EQUAL(org.codehaus.groovy.syntax.Types.MULTIPLY_EQUAL) GeneralUtils(org.codehaus.groovy.ast.tools.GeneralUtils) MULTIPLY(org.codehaus.groovy.syntax.Types.MULTIPLY) MATCH_REGEX(org.codehaus.groovy.syntax.Types.MATCH_REGEX) Integer_TYPE(org.codehaus.groovy.ast.ClassHelper.Integer_TYPE) ClassHelper.void_WRAPPER_TYPE(org.codehaus.groovy.ast.ClassHelper.void_WRAPPER_TYPE) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) Collection(java.util.Collection) BITWISE_XOR(org.codehaus.groovy.syntax.Types.BITWISE_XOR) Types(org.codehaus.groovy.syntax.Types) LEFT_SHIFT(org.codehaus.groovy.syntax.Types.LEFT_SHIFT) UUID(java.util.UUID) RIGHT_SHIFT_UNSIGNED_EQUAL(org.codehaus.groovy.syntax.Types.RIGHT_SHIFT_UNSIGNED_EQUAL) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) Short_TYPE(org.codehaus.groovy.ast.ClassHelper.Short_TYPE) GroovyBugError(org.codehaus.groovy.GroovyBugError) List(java.util.List) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Optional(java.util.Optional) Double_TYPE(org.codehaus.groovy.ast.ClassHelper.Double_TYPE) BITWISE_XOR_EQUAL(org.codehaus.groovy.syntax.Types.BITWISE_XOR_EQUAL) ClassHelper.int_TYPE(org.codehaus.groovy.ast.ClassHelper.int_TYPE) WideningCategories.lowestUpperBound(org.codehaus.groovy.ast.tools.WideningCategories.lowestUpperBound) HashMap(java.util.HashMap) DIVIDE(org.codehaus.groovy.syntax.Types.DIVIDE) Maps(org.apache.groovy.util.Maps) HashSet(java.util.HashSet) ClassHelper.isNumberType(org.codehaus.groovy.ast.ClassHelper.isNumberType) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) CLOSURE_TYPE(org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE) ClassHelper.boolean_TYPE(org.codehaus.groovy.ast.ClassHelper.boolean_TYPE) WideningCategories.isBigIntCategory(org.codehaus.groovy.ast.tools.WideningCategories.isBigIntCategory) ClassNodeUtils.addGeneratedMethod(org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod) LinkedList(java.util.LinkedList) Byte_TYPE(org.codehaus.groovy.ast.ClassHelper.Byte_TYPE) Iterator(java.util.Iterator) COMPARE_NOT_INSTANCEOF(org.codehaus.groovy.syntax.Types.COMPARE_NOT_INSTANCEOF) ParameterUtils(org.codehaus.groovy.ast.tools.ParameterUtils) GroovyClass(org.codehaus.groovy.tools.GroovyClass) GenericsUtils.makeClassSafe0(org.codehaus.groovy.ast.tools.GenericsUtils.makeClassSafe0) COMPARE_NOT_IDENTICAL(org.codehaus.groovy.syntax.Types.COMPARE_NOT_IDENTICAL) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) GenericsUtils(org.codehaus.groovy.ast.tools.GenericsUtils) MINUS_EQUAL(org.codehaus.groovy.syntax.Types.MINUS_EQUAL) StringJoiner(java.util.StringJoiner) WideningCategories(org.codehaus.groovy.ast.tools.WideningCategories) MOD(org.codehaus.groovy.syntax.Types.MOD) BaseStream(java.util.stream.BaseStream) COMPARE_LESS_THAN(org.codehaus.groovy.syntax.Types.COMPARE_LESS_THAN) COMPARE_LESS_THAN_EQUAL(org.codehaus.groovy.syntax.Types.COMPARE_LESS_THAN_EQUAL) Comparator(java.util.Comparator) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Collections(java.util.Collections) ClassHelper.isObjectType(org.codehaus.groovy.ast.ClassHelper.isObjectType) BITWISE_OR(org.codehaus.groovy.syntax.Types.BITWISE_OR) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 StringJoiner (java.util.StringJoiner)1 TreeSet (java.util.TreeSet)1 UUID (java.util.UUID)1 Matcher (java.util.regex.Matcher)1 BaseStream (java.util.stream.BaseStream)1