use of org.mvel2.ParserContext in project mvel by mvel.
the class NewObjectNode method getReducedValueAccelerated.
public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
if (newObjectOptimizer == null) {
if (egressType == null) {
if (factory != null && factory.isResolveable(typeDescr.getClassName())) {
try {
egressType = (Class) factory.getVariableResolver(typeDescr.getClassName()).getValue();
rewriteClassReferenceToFQCN(COMPILE_IMMEDIATE);
if (typeDescr.isArray()) {
try {
egressType = findClass(factory, repeatChar('[', typeDescr.getArrayLength()) + "L" + egressType.getName() + ";", pCtx);
} catch (Exception e) {
// for now, don't handle this.
}
}
} catch (ClassCastException e) {
throw new CompileException("cannot construct object: " + typeDescr.getClassName() + " is not a class reference", expr, start, e);
}
}
}
if (typeDescr.isArray()) {
return (newObjectOptimizer = new NewObjectArray(getBaseComponentType(egressType.getComponentType()), typeDescr.getCompiledArraySize())).getValue(ctx, thisValue, factory);
}
try {
AccessorOptimizer optimizer = getThreadAccessorOptimizer();
ParserContext pCtx = this.pCtx;
if (pCtx == null) {
pCtx = new ParserContext();
pCtx.getParserConfiguration().setAllImports(getInjectedImports(factory));
}
newObjectOptimizer = optimizer.optimizeObjectCreation(pCtx, name, 0, name.length, ctx, thisValue, factory);
/**
* Check to see if the optimizer actually produced the object during optimization. If so,
* we return that value now.
*/
if (optimizer.getResultOptPass() != null) {
egressType = optimizer.getEgressType();
return optimizer.getResultOptPass();
}
} catch (CompileException e) {
throw ErrorUtil.rewriteIfNeeded(e, expr, start);
} finally {
OptimizerFactory.clearThreadAccessorOptimizer();
}
}
return newObjectOptimizer.getValue(ctx, thisValue, factory);
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class ExpressionCompiler method verify.
protected ASTNode verify(ParserContext pCtx, ASTNode tk) {
if (tk.isOperator() && (tk.getOperator().equals(Operator.AND) || tk.getOperator().equals(Operator.OR))) {
secondPassOptimization = true;
}
if (tk.isDiscard() || tk.isOperator()) {
return tk;
} else if (tk.isLiteral()) {
/**
* Convert literal values from the default ASTNode to the more-efficient LiteralNode.
*/
if ((fields & COMPILE_IMMEDIATE) != 0 && tk.getClass() == ASTNode.class) {
return new LiteralNode(tk.getLiteralValue(), pCtx);
} else {
return tk;
}
}
if (verifying) {
if (tk.isIdentifier()) {
PropertyVerifier propVerifier = new PropertyVerifier(expr, tk.getStart(), tk.getOffset(), pCtx);
if (tk instanceof Union) {
propVerifier.setCtx(((Union) tk).getLeftEgressType());
tk.setEgressType(returnType = propVerifier.analyze());
} else {
tk.setEgressType(returnType = propVerifier.analyze());
if (propVerifier.isFqcn()) {
tk.setAsFQCNReference();
}
if (propVerifier.isClassLiteral()) {
return new LiteralNode(returnType, pCtx);
}
if (propVerifier.isInput()) {
pCtx.addInput(tk.getAbsoluteName(), propVerifier.isDeepProperty() ? Object.class : returnType);
}
if (!propVerifier.isMethodCall() && !returnType.isEnum() && !pCtx.isOptimizerNotified() && pCtx.isStrongTyping() && !pCtx.isVariableVisible(tk.getAbsoluteName()) && !tk.isFQCN()) {
throw new CompileException("no such identifier: " + tk.getAbsoluteName(), expr, tk.getStart());
}
}
} else if (tk.isAssignment()) {
Assignment a = (Assignment) tk;
if (a.getAssignmentVar() != null) {
// pCtx.makeVisible(a.getAssignmentVar());
PropertyVerifier propVerifier = new PropertyVerifier(a.getAssignmentVar(), pCtx);
tk.setEgressType(returnType = propVerifier.analyze());
if (!a.isNewDeclaration() && propVerifier.isResolvedExternally()) {
pCtx.addInput(tk.getAbsoluteName(), returnType);
}
ExecutableStatement c = (ExecutableStatement) subCompileExpression(expr, tk.getStart(), tk.getOffset(), pCtx);
if (pCtx.isStrictTypeEnforcement()) {
/**
* If we're using strict type enforcement, we need to see if this coercion can be done now,
* or fail epicly.
*/
if (!returnType.isAssignableFrom(c.getKnownEgressType()) && c.isLiteralOnly()) {
if (canConvert(c.getKnownEgressType(), returnType)) {
/**
* We convert the literal to the proper type.
*/
try {
a.setValueStatement(new ExecutableLiteral(convert(c.getValue(null, null), returnType)));
return tk;
} catch (Exception e) {
// fall through.
}
} else if (returnType.isPrimitive() && unboxPrimitive(c.getKnownEgressType()).equals(returnType)) {
/**
* We ignore boxed primitive cases, since MVEL does not recognize primitives.
*/
return tk;
}
throw new CompileException("cannot assign type " + c.getKnownEgressType().getName() + " to " + returnType.getName(), expr, st);
}
}
}
} else if (tk instanceof NewObjectNode) {
// this is a bit of a hack for now.
NewObjectNode n = (NewObjectNode) tk;
List<char[]> parms = ParseTools.parseMethodOrConstructor(tk.getNameAsArray());
if (parms != null) {
for (char[] p : parms) {
MVEL.analyze(p, pCtx);
}
}
}
returnType = tk.getEgressType();
}
if (!tk.isLiteral() && tk.getClass() == ASTNode.class && (tk.getFields() & ASTNode.ARRAY_TYPE_LITERAL) == 0) {
if (pCtx.isStrongTyping())
tk.strongTyping();
tk.storePctx();
tk.storeInLiteralRegister(pCtx);
}
return tk;
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class PropertyVerifier method getMethod.
/**
* Process method
*
* @param ctx - the ingress type
* @param name - the property component
* @return known egress type.
*/
private Class getMethod(Class ctx, String name) {
int st = cursor;
/**
* Check to see if this is the first element in the statement.
*/
if (first) {
first = false;
methodCall = true;
/**
* It's the first element in the statement, therefore we check to see if there is a static import of a
* native Java method or an MVEL function.
*/
if (pCtx.hasImport(name)) {
Method m = pCtx.getStaticImport(name).getMethod();
/**
* Replace the method parameters.
*/
ctx = m.getDeclaringClass();
name = m.getName();
} else {
Function f = pCtx.getFunction(name);
if (f != null && f.getEgressType() != null) {
resolvedExternally = false;
f.checkArgumentCount(parseParameterList((((cursor = balancedCapture(expr, cursor, end, '(')) - st) > 1 ? ParseTools.subset(expr, st + 1, cursor - st - 1) : new char[0]), 0, -1).size());
return f.getEgressType();
} else if (pCtx.hasVarOrInput("this")) {
if (pCtx.isStrictTypeEnforcement()) {
recordTypeParmsForProperty("this");
}
ctx = pCtx.getVarOrInputType("this");
resolvedExternally = false;
}
}
}
/**
* Get the arguments for the method.
*/
String tk;
if (cursor < end && expr[cursor] == '(' && ((cursor = balancedCapture(expr, cursor, end, '(')) - st) > 1) {
tk = new String(expr, st + 1, cursor - st - 1);
} else {
tk = "";
}
cursor++;
/**
* Parse out the arguments list.
*/
Class[] args;
List<char[]> subtokens = parseParameterList(tk.toCharArray(), 0, -1);
if (subtokens.size() == 0) {
args = new Class[0];
subtokens = Collections.emptyList();
} else {
// ParserContext subCtx = pCtx.createSubcontext();
args = new Class[subtokens.size()];
/**
* Subcompile all the arguments to determine their known types.
*/
// ExpressionCompiler compiler;
List<ErrorDetail> errors = pCtx.getErrorList().isEmpty() ? pCtx.getErrorList() : new ArrayList<ErrorDetail>(pCtx.getErrorList());
CompileException rethrow = null;
for (int i = 0; i < subtokens.size(); i++) {
try {
args[i] = MVEL.analyze(subtokens.get(i), pCtx);
if ("null".equals(String.valueOf(subtokens.get(i)))) {
args[i] = NullType.class;
}
} catch (CompileException e) {
rethrow = ErrorUtil.rewriteIfNeeded(e, expr, this.st);
}
if (errors.size() < pCtx.getErrorList().size()) {
for (ErrorDetail detail : pCtx.getErrorList()) {
if (!errors.contains(detail)) {
detail.setExpr(expr);
detail.setCursor(new String(expr).substring(this.st).indexOf(new String(subtokens.get(i))) + this.st);
detail.setColumn(0);
detail.setLineNumber(0);
detail.calcRowAndColumn();
}
}
}
if (rethrow != null) {
throw rethrow;
}
}
}
/**
* If the target object is an instance of java.lang.Class itself then do not
* adjust the Class scope target.
*/
Method m;
if ((m = getBestCandidate(args, name, ctx, ctx.getMethods(), pCtx.isStrongTyping())) == null) {
if ((m = getBestCandidate(args, name, ctx, ctx.getDeclaredMethods(), pCtx.isStrongTyping())) == null) {
StringAppender errorBuild = new StringAppender();
for (int i = 0; i < args.length; i++) {
errorBuild.append(args[i] != null ? args[i].getName() : null);
if (i < args.length - 1)
errorBuild.append(", ");
}
if (("size".equals(name) || "length".equals(name)) && args.length == 0 && ctx.isArray()) {
return Integer.class;
}
if (pCtx.isStrictTypeEnforcement()) {
throw new CompileException("unable to resolve method using strict-mode: " + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")", expr, tkStart);
}
return Object.class;
}
}
/**
* If we're in strict mode, we look for generic type information.
*/
if (pCtx.isStrictTypeEnforcement() && m.getGenericReturnType() != null) {
Map<String, Class> typeArgs = new HashMap<String, Class>();
Type[] gpt = m.getGenericParameterTypes();
Class z;
ParameterizedType pt;
for (int i = 0; i < gpt.length; i++) {
if (gpt[i] instanceof ParameterizedType) {
pt = (ParameterizedType) gpt[i];
if ((z = pCtx.getImport(new String(subtokens.get(i)))) != null) {
/**
* We record the value of the type parameter to our typeArgs Map.
*/
if (pt.getRawType().equals(Class.class)) {
/**
* If this is an instance of Class, we deal with the special parameterization case.
*/
typeArgs.put(pt.getActualTypeArguments()[0].toString(), z);
} else {
typeArgs.put(gpt[i].toString(), z);
}
}
}
}
if (pCtx.isStrictTypeEnforcement() && ctx.getTypeParameters().length != 0 && pCtx.getLastTypeParameters() != null && pCtx.getLastTypeParameters().length == ctx.getTypeParameters().length) {
TypeVariable[] typeVariables = ctx.getTypeParameters();
for (int i = 0; i < typeVariables.length; i++) {
Type typeArg = pCtx.getLastTypeParameters()[i];
typeArgs.put(typeVariables[i].getName(), typeArg instanceof Class ? type2Class(pCtx.getLastTypeParameters()[i]) : Object.class);
}
}
/**
* Get the return type argument
*/
Type parametricReturnType = m.getGenericReturnType();
String returnTypeArg = parametricReturnType.toString();
// push return type parameters onto parser context, only if this is a parametric type
if (parametricReturnType instanceof ParameterizedType) {
pCtx.setLastTypeParameters(((ParameterizedType) parametricReturnType).getActualTypeArguments());
}
if (paramTypes != null && paramTypes.containsKey(returnTypeArg)) {
/**
* If the paramTypes Map contains the known type, return that type.
*/
return type2Class(paramTypes.get(returnTypeArg));
} else if (typeArgs.containsKey(returnTypeArg)) {
/**
* If the generic type was declared as part of the method, it will be in this
* Map.
*/
return typeArgs.get(returnTypeArg);
}
}
if (!Modifier.isPublic(m.getModifiers()) && pCtx.isStrictTypeEnforcement()) {
StringAppender errorBuild = new StringAppender();
for (int i = 0; i < args.length; i++) {
errorBuild.append(args[i] != null ? args[i].getName() : null);
if (i < args.length - 1)
errorBuild.append(", ");
}
String scope = Modifier.toString(m.getModifiers());
if (scope.trim().equals(""))
scope = "<package local>";
addFatalError("the referenced method is not accessible: " + ctx.getName() + "." + name + "(" + errorBuild.toString() + ")" + " (scope: " + scope + "; required: public", this.tkStart);
}
return getReturnType(ctx, m);
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class MVEL method analysisCompile.
/**
* Performs an analysis compileShared, which will populate the ParserContext with type, input and variable information,
* but will not produce a payload.
*
* @param expression - the expression to analyze
* @param ctx - the parser context
*/
public static void analysisCompile(char[] expression, ParserContext ctx) {
ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx);
compiler.setVerifyOnly(true);
compiler.compile();
}
use of org.mvel2.ParserContext in project mvel by mvel.
the class MVEL method analyze.
public static Class analyze(char[] expression, ParserContext ctx) {
ExpressionCompiler compiler = new ExpressionCompiler(expression, ctx);
compiler.setVerifyOnly(true);
compiler.compile();
return compiler.getReturnType();
}
Aggregations