use of org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor in project drools by kiegroup.
the class ConditionAnalyzer method analyzeAccessorInvocation.
private Invocation analyzeAccessorInvocation(AccessorNode accessorNode, ASTNode containingNode, Invocation formerInvocation, Class<?> variableType) {
if (accessorNode instanceof GetterAccessor) {
return new MethodInvocation(((GetterAccessor) accessorNode).getMethod(), variableType == null ? conditionClass : variableType.getName());
}
if (accessorNode instanceof MethodAccessor) {
MethodAccessor methodAccessor = (MethodAccessor) accessorNode;
Method method = methodAccessor.getMethod();
MethodInvocation invocation = new MethodInvocation(method);
boolean isVarArgs = method.isVarArgs();
readInvocationParams(invocation, methodAccessor.getParms(), methodAccessor.getParameterTypes(), isVarArgs);
return invocation;
}
if (accessorNode instanceof ConstructorAccessor) {
ConstructorAccessor constructorAccessor = (ConstructorAccessor) accessorNode;
Constructor constructor = constructorAccessor.getConstructor();
ConstructorInvocation invocation = new ConstructorInvocation(constructor);
readInvocationParams(invocation, constructorAccessor.getParameters(), constructorAccessor.getParameterTypes(), constructor.isVarArgs());
return invocation;
}
if (accessorNode instanceof ArrayAccessor) {
ArrayAccessor arrayAccessor = (ArrayAccessor) accessorNode;
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, new FixedExpression(int.class, arrayAccessor.getIndex()));
}
if (accessorNode instanceof ArrayAccessorNest) {
ArrayAccessorNest arrayAccessorNest = (ArrayAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) arrayAccessorNest.getIndex();
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, analyzeNode(index.getNode()));
}
if (accessorNode instanceof ArrayLength) {
return new ArrayLengthInvocation();
}
if (accessorNode instanceof ListAccessor) {
Class<?> listType = getListType(formerInvocation);
ListAccessor listAccessor = (ListAccessor) accessorNode;
return new ListAccessInvocation(listType, new FixedExpression(int.class, listAccessor.getIndex()));
}
if (accessorNode instanceof ListAccessorNest) {
Class<?> listType = getListType(formerInvocation);
ListAccessorNest listAccessorNest = (ListAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) listAccessorNest.getIndex();
return new ListAccessInvocation(listType, analyzeNode(index.getNode()));
}
if (accessorNode instanceof MapAccessor) {
MapAccessor mapAccessor = (MapAccessor) accessorNode;
return new MapAccessInvocation(Object.class, Object.class, new FixedExpression(Object.class, mapAccessor.getProperty()));
}
if (accessorNode instanceof MapAccessorNest) {
Class<?> keyType = Object.class;
Class<?> valueType = Object.class;
Type[] generics = getGenerics(formerInvocation);
if (generics != null && generics.length == 2 && generics[0] instanceof Class) {
keyType = (Class<?>) generics[0];
if (generics[1] instanceof Class)
valueType = (Class<?>) generics[1];
}
MapAccessorNest mapAccessor = (MapAccessorNest) accessorNode;
ExecutableStatement statement = mapAccessor.getProperty();
if (statement instanceof ExecutableLiteral) {
return new MapAccessInvocation(keyType, valueType, new FixedExpression(keyType, ((ExecutableLiteral) statement).getLiteral()));
} else {
return new MapAccessInvocation(keyType, valueType, analyzeNode(((ExecutableAccessor) statement).getNode()));
}
}
if (accessorNode instanceof FieldAccessor) {
return new FieldAccessInvocation(((FieldAccessor) accessorNode).getField());
}
if (accessorNode instanceof StaticVarAccessor) {
Field field = ((StaticVarAccessor) accessorNode).getField();
return new FieldAccessInvocation(field);
}
if (accessorNode instanceof ThisValueAccessor) {
return new ThisInvocation(accessorNode.getNextNode() == null ? containingNode.getEgressType() : Object.class);
}
throw new RuntimeException("Unknown AccessorNode type: " + accessorNode.getClass().getName());
}
use of org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor in project drools by kiegroup.
the class ConditionAnalyzer method analyzeAccessorInvocation.
private Invocation analyzeAccessorInvocation(AccessorNode accessorNode, ASTNode containingNode, Invocation formerInvocation, Class<?> variableType) {
if (accessorNode instanceof GetterAccessor) {
return new MethodInvocation(((GetterAccessor) accessorNode).getMethod(), variableType == null ? conditionClass : variableType.getName());
}
if (accessorNode instanceof MethodAccessor) {
MethodAccessor methodAccessor = (MethodAccessor) accessorNode;
Method method = methodAccessor.getMethod();
MethodInvocation invocation = new MethodInvocation(method);
boolean isVarArgs = method.isVarArgs();
readInvocationParams(invocation, methodAccessor.getParms(), methodAccessor.getParameterTypes(), isVarArgs);
return invocation;
}
if (accessorNode instanceof ConstructorAccessor) {
ConstructorAccessor constructorAccessor = (ConstructorAccessor) accessorNode;
Constructor constructor = constructorAccessor.getConstructor();
ConstructorInvocation invocation = new ConstructorInvocation(constructor);
readInvocationParams(invocation, constructorAccessor.getParameters(), constructorAccessor.getParameterTypes(), constructor.isVarArgs());
return invocation;
}
if (accessorNode instanceof ArrayAccessor) {
ArrayAccessor arrayAccessor = (ArrayAccessor) accessorNode;
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, new FixedExpression(int.class, arrayAccessor.getIndex()));
}
if (accessorNode instanceof ArrayAccessorNest) {
ArrayAccessorNest arrayAccessorNest = (ArrayAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) arrayAccessorNest.getIndex();
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, analyzeNode(index.getNode()));
}
if (accessorNode instanceof ArrayLength) {
return new ArrayLengthInvocation();
}
if (accessorNode instanceof ListAccessor) {
Class<?> listType = getListType(formerInvocation);
ListAccessor listAccessor = (ListAccessor) accessorNode;
return new ListAccessInvocation(listType, new FixedExpression(int.class, listAccessor.getIndex()));
}
if (accessorNode instanceof ListAccessorNest) {
Class<?> listType = getListType(formerInvocation);
ListAccessorNest listAccessorNest = (ListAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) listAccessorNest.getIndex();
return new ListAccessInvocation(listType, analyzeNode(index.getNode()));
}
if (accessorNode instanceof MapAccessor) {
MapAccessor mapAccessor = (MapAccessor) accessorNode;
return new MapAccessInvocation(Object.class, Object.class, new FixedExpression(Object.class, mapAccessor.getProperty()));
}
if (accessorNode instanceof MapAccessorNest) {
Class<?> keyType = Object.class;
Class<?> valueType = Object.class;
Type[] generics = getGenerics(formerInvocation);
if (generics != null && generics.length == 2 && generics[0] instanceof Class) {
keyType = (Class<?>) generics[0];
if (generics[1] instanceof Class)
valueType = (Class<?>) generics[1];
}
MapAccessorNest mapAccessor = (MapAccessorNest) accessorNode;
ExecutableStatement statement = mapAccessor.getProperty();
if (statement instanceof ExecutableLiteral) {
return new MapAccessInvocation(keyType, valueType, new FixedExpression(keyType, ((ExecutableLiteral) statement).getLiteral()));
} else {
return new MapAccessInvocation(keyType, valueType, analyzeNode(((ExecutableAccessor) statement).getNode()));
}
}
if (accessorNode instanceof FieldAccessor) {
return new FieldAccessInvocation(((FieldAccessor) accessorNode).getField());
}
if (accessorNode instanceof StaticVarAccessor) {
Field field = ((StaticVarAccessor) accessorNode).getField();
return new FieldAccessInvocation(field);
}
if (accessorNode instanceof ThisValueAccessor) {
return new ThisInvocation(accessorNode.getNextNode() == null ? containingNode.getEgressType() : Object.class);
}
throw new RuntimeException("Unknown AccessorNode type: " + accessorNode.getClass().getName());
}
use of org.mvel2.optimizers.impl.refl.nodes.StaticVarAccessor in project mvel by mvel.
the class ReflectiveAccessorOptimizer method getBeanProperty.
private Object getBeanProperty(Object ctx, String property) throws Exception {
if ((pCtx == null ? currType : pCtx.getVarOrInputTypeOrNull(property)) == Object.class && !pCtx.isStrongTyping()) {
currType = null;
}
if (first) {
if ("this".equals(property)) {
addAccessorNode(new ThisValueAccessor());
return this.thisRef;
} else if (variableFactory != null && variableFactory.isResolveable(property)) {
if (variableFactory.isIndexedFactory() && variableFactory.isTarget(property)) {
int idx;
addAccessorNode(new IndexedVariableAccessor(idx = variableFactory.variableIndexOf(property)));
VariableResolver vr = variableFactory.getIndexedVariableResolver(idx);
if (vr == null) {
variableFactory.setIndexedVariableResolver(idx, variableFactory.getVariableResolver(property));
}
return variableFactory.getIndexedVariableResolver(idx).getValue();
} else {
addAccessorNode(new VariableAccessor(property));
return variableFactory.getVariableResolver(property).getValue();
}
}
}
boolean classRef = false;
Class<?> cls;
if (ctx instanceof Class) {
if (MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS && "class".equals(property)) {
return ctx;
}
cls = (Class<?>) ctx;
classRef = true;
} else if (ctx != null) {
cls = ctx.getClass();
} else {
cls = currType;
}
if (hasPropertyHandler(cls)) {
PropertyHandlerAccessor acc = new PropertyHandlerAccessor(property, cls, getPropertyHandler(cls));
addAccessorNode(acc);
return acc.getValue(ctx, thisRef, variableFactory);
}
Member member = cls != null ? getFieldOrAccessor(cls, property) : null;
if (member != null && classRef && (member.getModifiers() & Modifier.STATIC) == 0) {
member = null;
}
Object o;
if (member instanceof Method) {
try {
o = ctx != null ? ((Method) member).invoke(ctx, EMPTYARG) : null;
if (hasNullPropertyHandler()) {
addAccessorNode(new GetterAccessorNH((Method) member, getNullPropertyHandler()));
if (o == null)
o = getNullPropertyHandler().getProperty(member.getName(), ctx, variableFactory);
} else {
addAccessorNode(new GetterAccessor((Method) member));
}
} catch (IllegalAccessException e) {
Method iFaceMeth = determineActualTargetMethod((Method) member);
if (iFaceMeth == null)
throw new PropertyAccessException("could not access field: " + cls.getName() + "." + property, this.expr, this.start, pCtx);
o = iFaceMeth.invoke(ctx, EMPTYARG);
if (hasNullPropertyHandler()) {
addAccessorNode(new GetterAccessorNH((Method) member, getNullMethodHandler()));
if (o == null)
o = getNullMethodHandler().getProperty(member.getName(), ctx, variableFactory);
} else {
addAccessorNode(new GetterAccessor(iFaceMeth));
}
} catch (IllegalArgumentException e) {
if (member.getDeclaringClass().equals(ctx)) {
try {
Class c = Class.forName(member.getDeclaringClass().getName() + "$" + property);
throw new CompileException("name collision between innerclass: " + c.getCanonicalName() + "; and bean accessor: " + property + " (" + member.toString() + ")", expr, tkStart);
} catch (ClassNotFoundException e2) {
// fallthru
}
}
throw e;
}
currType = toNonPrimitiveType(((Method) member).getReturnType());
return o;
} else if (member != null) {
Field f = (Field) member;
if ((f.getModifiers() & Modifier.STATIC) != 0) {
o = f.get(null);
if (hasNullPropertyHandler()) {
addAccessorNode(new StaticVarAccessorNH((Field) member, getNullMethodHandler()));
if (o == null)
o = getNullMethodHandler().getProperty(member.getName(), ctx, variableFactory);
} else {
addAccessorNode(new StaticVarAccessor((Field) member));
}
} else {
o = ctx != null ? f.get(ctx) : null;
if (hasNullPropertyHandler()) {
addAccessorNode(new FieldAccessorNH((Field) member, getNullMethodHandler()));
if (o == null)
o = getNullMethodHandler().getProperty(member.getName(), ctx, variableFactory);
} else {
addAccessorNode(new FieldAccessor((Field) member));
}
}
currType = toNonPrimitiveType(f.getType());
return o;
} else if (ctx instanceof Map && (((Map) ctx).containsKey(property) || nullSafe)) {
addAccessorNode(new MapAccessor(property));
return ((Map) ctx).get(property);
} else if (ctx != null && "length".equals(property) && ctx.getClass().isArray()) {
addAccessorNode(new ArrayLength());
return getLength(ctx);
} else if (LITERALS.containsKey(property)) {
addAccessorNode(new StaticReferenceAccessor(ctx = LITERALS.get(property)));
return ctx;
} else {
Object tryStaticMethodRef = tryStaticAccess();
staticAccess = true;
if (tryStaticMethodRef != null) {
if (tryStaticMethodRef instanceof Class) {
addAccessorNode(new StaticReferenceAccessor(tryStaticMethodRef));
return tryStaticMethodRef;
} else if (tryStaticMethodRef instanceof Field) {
addAccessorNode(new StaticVarAccessor((Field) tryStaticMethodRef));
return ((Field) tryStaticMethodRef).get(null);
} else {
addAccessorNode(new StaticReferenceAccessor(tryStaticMethodRef));
return tryStaticMethodRef;
}
} else if (ctx instanceof Class) {
Class c = (Class) ctx;
for (Method m : c.getMethods()) {
if (property.equals(m.getName())) {
if (pCtx != null && pCtx.getParserConfiguration() != null ? pCtx.getParserConfiguration().isAllowNakedMethCall() : MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL) {
o = m.invoke(null, EMPTY_OBJ_ARR);
if (hasNullMethodHandler()) {
addAccessorNode(new MethodAccessorNH(m, new ExecutableStatement[0], getNullMethodHandler()));
if (o == null)
o = getNullMethodHandler().getProperty(m.getName(), ctx, variableFactory);
} else {
addAccessorNode(new MethodAccessor(m, new ExecutableStatement[0]));
}
return o;
} else {
addAccessorNode(new StaticReferenceAccessor(m));
return m;
}
}
}
try {
Class subClass = findClass(variableFactory, c.getName() + "$" + property, pCtx);
addAccessorNode(new StaticReferenceAccessor(subClass));
return subClass;
} catch (ClassNotFoundException cnfe) {
// fall through.
}
} else if (pCtx != null && pCtx.getParserConfiguration() != null ? pCtx.getParserConfiguration().isAllowNakedMethCall() : MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL) {
return getMethod(ctx, property);
}
if (ctx == null) {
throw new PropertyAccessException("unresolvable property or identifier: " + property, expr, start, pCtx);
} else {
throw new PropertyAccessException("could not access: " + property + "; in class: " + ctx.getClass().getName(), expr, start, pCtx);
}
}
}
Aggregations