public static Method getGetter(Class clazz, String property) { String isGet = ReflectionUtil.getIsGetter(property); property = ReflectionUtil.getGetter(property); for (Method meth : clazz.getMethods()) { if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 0 && (property.equals(meth.getName()) || isGet.equals(meth.getName()))) { return meth; } } return null; }
public Object setValue(Object ctx, Object elCtx, VariableResolverFactory vars, Object value) { try { if (nextNode != null) { return nextNode.setValue(method.invoke(ctx, EMPTY), elCtx, vars, value); } else { throw new RuntimeException("bad payload"); } } catch (IllegalArgumentException e) { /** * HACK: Try to access this another way. */ if (nextNode != null) { return nextNode.setValue(getProperty(getPropertyFromAccessor(method.getName()), ctx), elCtx, vars, value); } else { return getProperty(getPropertyFromAccessor(method.getName()), ctx); } } catch (CompileException e) { throw e; } catch (Exception e) { throw new RuntimeException("error " + method.getName() + ": " + e.getClass().getName() + ":" + e.getMessage(), e); } }
public static Method getSetter(Class clazz, String property) { property = ReflectionUtil.getSetter(property); for (Method meth : clazz.getMethods()) { if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 1 && property.equals(meth.getName())) { return meth; } } return null; }
public static boolean canConvert(Class toType, Class convertFrom) { if (isAssignableFrom(toType, convertFrom)) return true; if (CONVERTERS.containsKey(toType)) { return CONVERTERS.get(toType).canConvertFrom(toNonPrimitiveType(convertFrom)); } else if (toType.isArray() && canConvert(toType.getComponentType(), convertFrom)) { return true; } return false; }
private void visitConstantClass(Class<?> clazz) { if (clazz == null) clazz = Object.class; if (clazz.isPrimitive()) { mv.visitFieldInsn(GETSTATIC, toNonPrimitiveType(clazz).getName().replace(".", "/"), "TYPE", "Ljava/lang/Class;"); } else { mv.visitLdcInsn(org.mvel2.asm.Type.getType(clazz)); } }
public void testJIRA139() { ParserContext ctx = new ParserContext(); ctx.addImport("ReflectionUtil", ReflectionUtil.class); Serializable s = compileExpression("ReflectionUtil.getGetter('foo')", ctx); assertEquals(ReflectionUtil.getGetter("foo"), executeExpression(s)); }
public static Class getClassReference(ParserContext ctx, Class cls, TypeDescriptor tDescr) throws ClassNotFoundException { if (tDescr.isArray()) { cls = cls.isPrimitive() ? toPrimitiveArrayType(cls) : findClass(null, repeatChar('[', tDescr.arraySize.length) + "L" + cls.getName() + ";", ctx); } return cls; }
private boolean canCast(Class from, Class to) { return isAssignableFrom(from, to) || (from.isInterface() && interfaceAssignable(from, to)); }
public static boolean isAssignableFrom(Class<?> from, Class<?> to) { return from.isAssignableFrom(to) || areBoxingCompatible(from, to); }
private static boolean areBoxingCompatible(Class<?> c1, Class<?> c2) { return c1.isPrimitive() ? isPrimitiveOf(c2, c1) : (c2.isPrimitive() && isPrimitiveOf(c1, c2)); }
public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) { this.expr = property; this.cursor = this.start = start; this.end = start + offset; this.length = offset; type = toNonPrimitiveArray(type); this.returnType = type; this.compiledInputs = new ArrayList<ExecutableStatement>(); this.ctx = ctx; this.thisRef = thisRef; this.variableFactory = factory; this.pCtx = pCtx; _initJIT(); literal = true; _getAccessor(o, type); _finishJIT(); try { Accessor compiledAccessor = _initializeAccessor(); if (property != null && length > start) { return new Union(pCtx, compiledAccessor, property, start, length); } else { return compiledAccessor; } } catch (Exception e) { throw new OptimizationFailure("could not optimize collection", e); } }
private void visitConstantClass(Class<?> clazz) { if (clazz == null) clazz = Object.class; if (clazz.isPrimitive()) { mv.visitFieldInsn(GETSTATIC, toNonPrimitiveType(clazz).getName().replace(".", "/"), "TYPE", "Ljava/lang/Class;"); } else { mv.visitLdcInsn(org.mvel2.asm.Type.getType(clazz)); } }
public static boolean canConvert(Class toType, Class convertFrom) { if (isAssignableFrom(toType, convertFrom)) return true; if (CONVERTERS.containsKey(toType)) { return CONVERTERS.get(toType).canConvertFrom(toNonPrimitiveType(convertFrom)); } else if (toType.isArray() && canConvert(toType.getComponentType(), convertFrom)) { return true; } return false; }
public static Class getClassReference(ParserContext ctx, Class cls, TypeDescriptor tDescr) throws ClassNotFoundException { if (tDescr.isArray()) { cls = cls.isPrimitive() ? toPrimitiveArrayType(cls) : findClass(null, repeatChar('[', tDescr.arraySize.length) + "L" + cls.getName() + ";", ctx); } return cls; }
private boolean canCast(Class from, Class to) { return isAssignableFrom(from, to) || (from.isInterface() && interfaceAssignable(from, to)); }
public static boolean isAssignableFrom(Class<?> from, Class<?> to) { return from.isAssignableFrom(to) || areBoxingCompatible(from, to); }
private static boolean areBoxingCompatible(Class<?> c1, Class<?> c2) { return c1.isPrimitive() ? isPrimitiveOf(c2, c1) : (c2.isPrimitive() && isPrimitiveOf(c1, c2)); }
public Accessor optimizeCollection(ParserContext pCtx, Object o, Class type, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory) { this.expr = property; this.cursor = this.start = start; this.end = start + offset; this.length = offset; type = toNonPrimitiveArray(type); this.returnType = type; this.compiledInputs = new ArrayList<ExecutableStatement>(); this.ctx = ctx; this.thisRef = thisRef; this.variableFactory = factory; this.pCtx = pCtx; _initJIT(); literal = true; _getAccessor(o, type); _finishJIT(); try { Accessor compiledAccessor = _initializeAccessor(); if (property != null && length > start) { return new Union(pCtx, compiledAccessor, property, start, length); } else { return compiledAccessor; } } catch (Exception e) { throw new OptimizationFailure("could not optimize collection", e); } }
public Object setValue(Object ctx, Object elCtx, VariableResolverFactory vars, Object value) { try { if (nextNode != null) { return nextNode.setValue(method.invoke(ctx, EMPTY), elCtx, vars, value); } else { throw new RuntimeException("bad payload"); } } catch (IllegalArgumentException e) { /** * HACK: Try to access this another way. */ if (nextNode != null) { return nextNode.setValue(getProperty(getPropertyFromAccessor(method.getName()), ctx), elCtx, vars, value); } else { return getProperty(getPropertyFromAccessor(method.getName()), ctx); } } catch (CompileException e) { throw e; } catch (Exception e) { throw new RuntimeException("error " + method.getName() + ": " + e.getClass().getName() + ":" + e.getMessage(), e); } }
public static Method getGetter(Class clazz, String property) { String simple = "get" + property; String simpleIsGet = "is" + property; String isGet = ReflectionUtil.getIsGetter(property); String getter = ReflectionUtil.getGetter(property); Method candidate = null; for (Method meth : clazz.getMethods()) { if ((meth.getModifiers() & PUBLIC) != 0 && (meth.getModifiers() & STATIC) == 0 && meth.getParameterTypes().length == 0 && (getter.equals(meth.getName()) || property.equals(meth.getName()) || ((isGet.equals(meth.getName()) || simpleIsGet.equals(meth.getName())) && meth.getReturnType() == boolean.class) || simple.equals(meth.getName()))) { if (candidate == null || candidate.getReturnType().isAssignableFrom(meth.getReturnType())) { candidate = meth; } } } return candidate; }