List of usage examples for org.apache.commons.lang3 ClassUtils getPublicMethod
public static Method getPublicMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) throws SecurityException, NoSuchMethodException
Returns the desired Method much like Class.getMethod , however it ensures that the returned Method is from a public class or interface and not from an anonymous inner class.
From source file:com.jsen.javascript.java.HostedJavaCollection.java
/** * Locates the value for a given key./*from w w w. j av a 2 s .c om*/ * * @param key Key that should be located. * @return If this object wraps the collection then returns the value of the collection using given key, otherwise null. */ protected Object collectionGet(Object key) { Method method = null; Object result = Scriptable.NOT_FOUND; Object collectionObject = (object instanceof ArrayWrapper) ? ((ArrayWrapper) object).unwrap() : object; Class<?> javaObjectType = collectionObject.getClass(); if (collectionObject instanceof List<?> && key instanceof Integer) { try { method = ClassUtils.getPublicMethod(javaObjectType, "get", int.class); } catch (Exception e) { throw new InternalException(e); } List<?> list = (List<?>) collectionObject; int index = (Integer) key; result = (list.size() > index) ? list.get((Integer) key) : Scriptable.NOT_FOUND; } else if (collectionObject instanceof Map<?, ?>) { try { method = ClassUtils.getPublicMethod(javaObjectType, "get", Object.class); } catch (Exception e) { throw new InternalException(e); } result = ((Map<?, ?>) collectionObject).get(key); } return result; }
From source file:com.jsen.core.reflect.ClassFunction.java
/** * Returns method of the class that equals to the object getter method. * /*from w w w . ja v a 2 s . c o m*/ * @param clazz Class which should contain the object getter method. * @return Object getter method. */ private static Method getObjectGetterMetod(Class<?> clazz) { try { return ClassUtils.getPublicMethod(clazz, ObjectGetter.METHOD_NAME, ObjectGetter.METHOD_ARG_TYPES); } catch (Exception e) { throw new InternalException(e); } }
From source file:com.jsen.javascript.java.HostedJavaObject.java
/** * Returns the value of the object getter it there is any for the wrapped java object. * // w ww . j av a2s . c o m * @param key Key that should be searched using the object getter. * @return Value of the property of the wrapped java object */ protected Object objectGetterGet(Object key) { Method method = null; Object result = Scriptable.NOT_FOUND; if (object instanceof ObjectGetter) { Object value = ((ObjectGetter) object).get(key); try { method = ClassUtils.getPublicMethod(objectClass, ObjectGetter.METHOD_NAME, ObjectGetter.METHOD_ARG_TYPES); } catch (Exception e) { throw new InternalException(e); } if (value != ObjectGetter.UNDEFINED_VALUE) { result = value; } } if (result != Scriptable.NOT_FOUND && method != null) { //result = new JavaMethodRedirectedWrapper(result, object, method); } return result; }
From source file:org.apache.jorphan.reflect.ClassTools.java
/** * Invoke a public method on a class instance * * @param instance//from w ww.j a va2 s . c o m * object on which the method should be called * @param methodName * name of the method to be called * @throws SecurityException * if a security violation occurred while looking for the method * @throws IllegalArgumentException * if the method parameters (none given) do not match the * signature of the method * @throws JMeterException * if something went wrong in the invoked method */ public static void invoke(Object instance, String methodName) throws SecurityException, IllegalArgumentException, JMeterException { Method m; try { m = ClassUtils.getPublicMethod(instance.getClass(), methodName, new Class[] {}); m.invoke(instance, (Object[]) null); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { throw new JMeterException(e); } }
From source file:org.apache.syncope.core.persistence.jpa.dao.AbstractAnySearchDAO.java
protected Triple<PlainSchema, PlainAttrValue, AnyCond> check(final AnyCond cond, final AnyTypeKind kind) { AnyCond condClone = SerializationUtils.clone(cond); AnyUtils attrUtils = anyUtilsFactory.getInstance(kind); // Keeps track of difference between entity's getKey() and JPA @Id fields if ("key".equals(condClone.getSchema())) { condClone.setSchema("id"); }/*from ww w. j a v a 2 s .c o m*/ Field anyField = ReflectionUtils.findField(attrUtils.anyClass(), condClone.getSchema()); if (anyField == null) { LOG.warn("Ignoring invalid schema '{}'", condClone.getSchema()); throw new IllegalArgumentException(); } PlainSchema schema = new JPAPlainSchema(); schema.setKey(anyField.getName()); for (AttrSchemaType attrSchemaType : AttrSchemaType.values()) { if (anyField.getType().isAssignableFrom(attrSchemaType.getType())) { schema.setType(attrSchemaType); } } // Deal with any Integer fields logically mapping to boolean values boolean foundBooleanMin = false; boolean foundBooleanMax = false; if (Integer.class.equals(anyField.getType())) { for (Annotation annotation : anyField.getAnnotations()) { if (Min.class.equals(annotation.annotationType())) { foundBooleanMin = ((Min) annotation).value() == 0; } else if (Max.class.equals(annotation.annotationType())) { foundBooleanMax = ((Max) annotation).value() == 1; } } } if (foundBooleanMin && foundBooleanMax) { schema.setType(AttrSchemaType.Boolean); } // Deal with any fields representing relationships to other entities if (anyField.getType().getAnnotation(Entity.class) != null) { Method relMethod = null; try { relMethod = ClassUtils.getPublicMethod(anyField.getType(), "getKey", new Class<?>[0]); } catch (Exception e) { LOG.error("Could not find {}#getKey", anyField.getType(), e); } if (relMethod != null && String.class.isAssignableFrom(relMethod.getReturnType())) { condClone.setSchema(condClone.getSchema() + "_id"); schema.setType(AttrSchemaType.String); } } PlainAttrValue attrValue = attrUtils.newPlainAttrValue(); if (condClone.getType() != AttributeCond.Type.LIKE && condClone.getType() != AttributeCond.Type.ILIKE && condClone.getType() != AttributeCond.Type.ISNULL && condClone.getType() != AttributeCond.Type.ISNOTNULL) { try { ((JPAPlainSchema) schema).validator().validate(condClone.getExpression(), attrValue); } catch (ValidationException e) { LOG.error("Could not validate expression '" + condClone.getExpression() + "'", e); throw new IllegalArgumentException(); } } return Triple.of(schema, attrValue, condClone); }
From source file:org.apache.syncope.core.persistence.jpa.dao.JPASubjectSearchDAO.java
@SuppressWarnings("rawtypes") private String getQuery(final SubjectCond cond, final boolean not, final List<Object> parameters, final SubjectType type, final SearchSupport svs) { final AttributableUtil attrUtil = attrUtilFactory.getInstance(type.asAttributableType()); // Keeps track of difference between entity's getKey() and JPA @Id fields if ("key".equals(cond.getSchema())) { cond.setSchema("id"); }/*from w w w .jav a 2 s . c om*/ Field subjectField = ReflectionUtils.findField(attrUtil.attributableClass(), cond.getSchema()); if (subjectField == null) { LOG.warn("Ignoring invalid schema '{}'", cond.getSchema()); return EMPTY_ATTR_QUERY; } PlainSchema schema = attrUtil.newPlainSchema(); schema.setKey(subjectField.getName()); for (AttrSchemaType attrSchemaType : AttrSchemaType.values()) { if (subjectField.getType().isAssignableFrom(attrSchemaType.getType())) { schema.setType(attrSchemaType); } } // Deal with subject Integer fields logically mapping to boolean values // (JPARole.inheritPlainAttrs, for example) boolean foundBooleanMin = false; boolean foundBooleanMax = false; if (Integer.class.equals(subjectField.getType())) { for (Annotation annotation : subjectField.getAnnotations()) { if (Min.class.equals(annotation.annotationType())) { foundBooleanMin = ((Min) annotation).value() == 0; } else if (Max.class.equals(annotation.annotationType())) { foundBooleanMax = ((Max) annotation).value() == 1; } } } if (foundBooleanMin && foundBooleanMax) { schema.setType(AttrSchemaType.Boolean); } // Deal with subject fields representing relationships to other entities if (subjectField.getType().getAnnotation(Entity.class) != null) { Method relMethod = null; try { relMethod = ClassUtils.getPublicMethod(subjectField.getType(), "getKey", new Class[0]); } catch (Exception e) { LOG.error("Could not find {}#getKey", subjectField.getType(), e); } if (relMethod != null) { if (Long.class.isAssignableFrom(relMethod.getReturnType())) { cond.setSchema(cond.getSchema() + "_id"); schema.setType(AttrSchemaType.Long); } if (String.class.isAssignableFrom(relMethod.getReturnType())) { cond.setSchema(cond.getSchema() + "_name"); schema.setType(AttrSchemaType.String); } } } PlainAttrValue attrValue = attrUtil.newPlainAttrValue(); if (cond.getType() != AttributeCond.Type.LIKE && cond.getType() != AttributeCond.Type.ISNULL && cond.getType() != AttributeCond.Type.ISNOTNULL) { try { schema.getValidator().validate(cond.getExpression(), attrValue); } catch (ValidationException e) { LOG.error("Could not validate expression '" + cond.getExpression() + "'", e); return EMPTY_ATTR_QUERY; } } final StringBuilder query = new StringBuilder("SELECT DISTINCT subject_id FROM ").append(svs.field().name) .append(" WHERE "); fillAttributeQuery(query, attrValue, schema, cond, not, parameters, svs); return query.toString(); }
From source file:org.fit.cssbox.scriptbox.script.javascript.java.HostedJavaCollection.java
/** * Locates the value for a given key.// w ww . j a va2 s . com * * @param key Key that should be located. * @return If this object wraps the collection then returns the value of the collection using given key, otherwise null. */ protected Object collectionGet(Object key) { Method method = null; Object result = Scriptable.NOT_FOUND; if (object instanceof List<?> && key instanceof Integer) { try { method = ClassUtils.getPublicMethod(javaObjectType, "get", int.class); } catch (Exception e) { throw new InternalException(e); } List<?> list = (List<?>) object; int index = (Integer) key; result = (list.size() > index) ? list.get((Integer) key) : Scriptable.NOT_FOUND; } else if (object instanceof Map<?, ?>) { try { method = ClassUtils.getPublicMethod(javaObjectType, "get", Object.class); } catch (Exception e) { throw new InternalException(e); } result = ((Map<?, ?>) object).get(key); } if (result != Scriptable.NOT_FOUND && method != null) { //result = new JavaMethodRedirectedWrapper(result, javaObject, method); } return result; }
From source file:yoyo.framework.standard.shared.commons.lang.ClassUtilsTest.java
@Test public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException { List<Class<?>> classes = new ArrayList<Class<?>>() { {/* ww w .jav a 2 s. c o m*/ add(Integer.class); add(Long.class); } }; assertThat(ClassUtils.convertClassesToClassNames(classes).toArray(), is(new Object[] { "java.lang.Integer", "java.lang.Long" })); List<String> classNames = new ArrayList<String>() { { add("java.lang.Integer"); add("java.lang.Long"); } }; assertThat(ClassUtils.convertClassNamesToClasses(classNames).toArray(), is(classes.toArray())); assertThat(ClassUtils.getAllInterfaces(String.class).toArray(), is(new Object[] { java.io.Serializable.class, java.lang.Comparable.class, java.lang.CharSequence.class })); assertThat(ClassUtils.getAllSuperclasses(HashMap.class).toArray(), is(new Object[] { java.util.AbstractMap.class, java.lang.Object.class })); assertThat(ClassUtils.getClass("java.lang.String").toString(), is("class java.lang.String")); assertThat(ClassUtils.getPackageCanonicalName(String.class), is("java.lang")); assertThat(ClassUtils.getPackageName(String.class), is("java.lang")); assertThat(ClassUtils.getPublicMethod(String.class, "length", new Class[] {}), is(not(nullValue()))); assertThat(ClassUtils.getShortCanonicalName(String.class), is("String")); assertThat(ClassUtils.getShortClassName(String.class), is("String")); assertThat(ClassUtils.getSimpleName(String.class), is("String")); assertThat(ClassUtils.isAssignable(String.class, Object.class), is(true)); assertThat(ClassUtils.isInnerClass(Foo.class), is(true)); assertThat(ClassUtils.isInnerClass(String.class), is(false)); assertThat(ClassUtils.isPrimitiveOrWrapper(Integer.class), is(true)); assertThat(ClassUtils.isPrimitiveOrWrapper(String.class), is(false)); assertThat(ClassUtils.isPrimitiveWrapper(Integer.class), is(true)); assertThat(ClassUtils.isPrimitiveWrapper(String.class), is(false)); assertThat(ClassUtils.primitivesToWrappers(new Class[] { Integer.class, Long.class }), is(not(nullValue()))); assertThat(ClassUtils.primitiveToWrapper(Integer.class), is(not(nullValue()))); assertThat(ClassUtils.toClass(1L, 2L), is(not(nullValue()))); assertThat(ClassUtils.wrappersToPrimitives(new Class[] { Integer.class, Long.class }), is(not(nullValue()))); assertThat(ClassUtils.wrapperToPrimitive(Integer.class), is(not(nullValue()))); }