@Override public Value evaluate(final EvaluationContext evaluationContext) throws EvaluateException { DebugProcess process = evaluationContext.getDebugProcess(); ClassLoaderReference classLoader; try { classLoader = getClassLoader(evaluationContext, process); } catch (Exception e) { throw new EvaluateException("Error creating evaluation class loader: " + e, e); } String version = ((VirtualMachineProxyImpl) process.getVirtualMachineProxy()).version(); JavaSdkVersion sdkVersion = JdkVersionUtil.getVersion(version); Collection<OutputFileObject> classes = compile(sdkVersion != null ? sdkVersion.getDescription() : null); try { defineClasses(classes, evaluationContext, process, classLoader); } catch (Exception e) { throw new EvaluateException("Error during classes definition " + e, e); } try { // invoke base evaluator on call code final Project project = ApplicationManager.getApplication() .runReadAction( new Computable<Project>() { @Override public Project compute() { return myPsiContext.getProject(); } }); ExpressionEvaluator evaluator = DebuggerInvocationUtil.commitAndRunReadAction( project, new EvaluatingComputable<ExpressionEvaluator>() { @Override public ExpressionEvaluator compute() throws EvaluateException { final TextWithImports callCode = getCallCode(); PsiElement copyContext = myData.getAnchor(); final CodeFragmentFactory factory = DebuggerUtilsEx.findAppropriateCodeFragmentFactory(callCode, copyContext); return factory .getEvaluatorBuilder() .build( factory.createCodeFragment(callCode, copyContext, project), ContextUtil.getSourcePosition(evaluationContext)); } }); ((EvaluationContextImpl) evaluationContext).setClassLoader(classLoader); return evaluator.evaluate(evaluationContext); } catch (Exception e) { throw new EvaluateException("Error during generated code invocation " + e, e); } }
private static ClassLoaderReference getClassLoader( EvaluationContext context, DebugProcess process) throws EvaluateException, InvocationException, InvalidTypeException, ClassNotLoadedException, IncompatibleThreadStateException { // TODO: cache ClassType loaderClass = (ClassType) process.findClass(context, "java.net.URLClassLoader", context.getClassLoader()); Method ctorMethod = loaderClass.concreteMethodByName("<init>", "([Ljava/net/URL;Ljava/lang/ClassLoader;)V"); ClassLoaderReference reference = (ClassLoaderReference) process.newInstance( context, loaderClass, ctorMethod, Arrays.asList(createURLArray(context), context.getClassLoader())); keep(reference, context); return reference; }
private static ArrayReference mirrorOf( byte[] bytes, EvaluationContext context, DebugProcess process) throws EvaluateException, InvalidTypeException, ClassNotLoadedException { ArrayType arrayClass = (ArrayType) process.findClass(context, "byte[]", context.getClassLoader()); ArrayReference reference = process.newInstance(arrayClass, bytes.length); keep(reference, context); for (int i = 0; i < bytes.length; i++) { reference.setValue( i, ((VirtualMachineProxyImpl) process.getVirtualMachineProxy()).mirrorOf(bytes[i])); } return reference; }
private static ArrayReference createURLArray(EvaluationContext context) throws EvaluateException, InvocationException, InvalidTypeException, ClassNotLoadedException, IncompatibleThreadStateException { DebugProcess process = context.getDebugProcess(); ArrayType arrayType = (ArrayType) process.findClass(context, "java.net.URL[]", context.getClassLoader()); ArrayReference arrayRef = arrayType.newInstance(1); keep(arrayRef, context); ClassType classType = (ClassType) process.findClass(context, "java.net.URL", context.getClassLoader()); VirtualMachineProxyImpl proxy = (VirtualMachineProxyImpl) process.getVirtualMachineProxy(); StringReference url = proxy.mirrorOf("file:a"); keep(url, context); ObjectReference reference = process.newInstance( context, classType, classType.concreteMethodByName("<init>", "(Ljava/lang/String;)V"), Collections.singletonList(url)); keep(reference, context); arrayRef.setValues(Collections.singletonList(reference)); return arrayRef; }
public static void main(String[] args) { // 创建一个ExpressionParser对象,用于解析表达式 ExpressionParser parser = new SpelExpressionParser(); List<String> list = new ArrayList<String>(); list.add("疯狂Java讲义"); list.add("疯狂Ajax讲义"); list.add("疯狂XML讲义"); list.add("经典Java EE企业应用实战"); EvaluationContext ctx = new StandardEvaluationContext(); ctx.setVariable("mylist", list); // 得到的新集合的元素是原集合的每个元素length()方法返回值 Expression expr = parser.parseExpression("#mylist.![length()]"); System.out.println(expr.getValue(ctx)); List<Person> list2 = new ArrayList<Person>(); list2.add(new Person(1, "孙悟空", 162)); list2.add(new Person(1, "猪八戒", 182)); list2.add(new Person(1, "牛魔王", 195)); ctx.setVariable("mylist2", list2); // 得到的新集合的元素是原集合的每个元素name属性值 expr = parser.parseExpression("#mylist2.![name]"); System.out.println(expr.getValue(ctx)); }
public static FieldValue evaluate( Apply apply, List<FieldValue> values, EvaluationContext context) { String name = apply.getFunction(); Function function = getFunction(name); if (function == null) { DefineFunction defineFunction = context.resolveFunction(name); if (defineFunction == null) { throw new UnsupportedFeatureException(apply); } return evaluate(defineFunction, values, context); } return function.evaluate(values); }
public static void keep(Value value, EvaluationContext context) { if (value instanceof ObjectReference) { ((SuspendContextImpl) context.getSuspendContext()).keep((ObjectReference) value); } }
private static void keep(ObjectReference reference, EvaluationContext context) { ((SuspendContextImpl) context.getSuspendContext()).keep(reference); }