List of usage examples for org.apache.commons.lang3 ClassUtils isAssignable
public static boolean isAssignable(final Class<?> cls, final Class<?> toClass)
Checks if one Class can be assigned to a variable of another Class .
Unlike the Class#isAssignableFrom(java.lang.Class) method, this method takes into account widenings of primitive classes and null s.
Primitive widenings allow an int to be assigned to a long, float or double.
From source file:com.jsen.javascript.java.HostedJavaMethod.java
/** * Casts the given arguments into given expected types. * /* w w w. jav a 2 s. c o m*/ * @param expectedTypes Types into which should be casted the given arguments. * @param args Arguments to be casted. * @return Array of the casted arguments if casting was successful, otherwise null. */ public static Object[] castArgs(Class<?>[] expectedTypes, Object... args) { if (expectedTypes != null && args != null) { if (expectedTypes.length <= args.length + 1 && expectedTypes.length > 0) { Class<?> lastType = expectedTypes[expectedTypes.length - 1]; if (lastType.isArray()) { Class<?> arrayType = lastType.getComponentType(); boolean maybeVarargs = true; if (expectedTypes.length == args.length) { Object lastArg = args[args.length - 1]; Class<?> lastArgClass = (lastArg != null) ? lastArg.getClass() : null; maybeVarargs = lastArgClass != null && !ClassUtils.isAssignable(lastArgClass, lastType); } if (maybeVarargs) { for (int i = expectedTypes.length - 1; i < args.length; i++) { if (args[i] == null) { continue; } Class<?> argType = args[i].getClass(); if (!ClassUtils.isAssignable(argType, arrayType)) { maybeVarargs = false; break; } } if (maybeVarargs) { Object[] oldArgs = args; args = new Object[expectedTypes.length]; for (int i = 0; i < expectedTypes.length - 1; i++) { args[i] = oldArgs[i]; } Object[] varargs = new Object[oldArgs.length - expectedTypes.length + 1]; for (int i = expectedTypes.length - 1; i < oldArgs.length; i++) { varargs[i - expectedTypes.length + 1] = oldArgs[i]; } args[expectedTypes.length - 1] = varargs; } } } } if (expectedTypes.length == args.length) { Object[] castedArgs = new Object[args.length]; for (int i = 0; i < args.length; i++) { Object arg = args[i]; Class<?> expectedType = expectedTypes[i]; if (arg == null) { castedArgs[i] = null; } else if (arg == Undefined.instance) { castedArgs[i] = null; } else if (arg instanceof ConsString) { castedArgs[i] = ((ConsString) arg).toString(); } else if (arg instanceof Double && (expectedType.equals(Integer.class) || expectedType.equals(int.class) || expectedType.equals(Long.class) || expectedType.equals(long.class))) { castedArgs[i] = ((Double) arg).intValue(); } else { castedArgs[i] = JavaScriptEngine.jsToJava(arg); //castedArgs[i] = Context.jsToJava(castedArgs[i], expectedType); } castedArgs[i] = HostedJavaObject.wrap(expectedTypes[i], castedArgs[i]); } return castedArgs; } } return null; }
From source file:lineage2.gameserver.scripts.Scripts.java
/** * Method shutdown.// w w w. j av a2 s . co m */ public void shutdown() { for (Class<?> clazz : _classes.values()) { if (ClassUtils.isAssignable(clazz, Quest.class)) { continue; } if (ClassUtils.isAssignable(clazz, ScriptFile.class)) { try { ((ScriptFile) clazz.newInstance()).onShutdown(); } catch (Exception e) { _log.error("Scripts: Failed running " + clazz.getName() + ".onShutdown()", e); } } } }
From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java
public static boolean isCollectionOrSequenceOrArrayOrEnum(Class<?> type) { if (!((ClassUtils.isAssignable(type, java.util.Collection.class))) || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Collection.class)) || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Sequence.class))) { return type.isArray() || type.isEnum(); }/* w w w . ja v a 2 s. c o m*/ return true; }
From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java
@SuppressWarnings("unchecked") public static <T> T cast(Object obj, Class<?> type) { if ((obj != null) && (type != null)) { if (type.isPrimitive()) { Class<?> wrapperType = ClassUtils.primitiveToWrapper(type); if (ClassUtils.isAssignable(obj.getClass(), wrapperType)) { return (T) wrapperType.cast(obj); }/*from w ww .jav a2s .c om*/ } else { if (ClassUtils.isAssignable(obj.getClass(), type)) { return (T) type.cast(obj); } } } return null; }
From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java
public static boolean isInstanceOf(Object obj, Class<?> type) { if (obj != null) { return ClassUtils.isAssignable(obj.getClass(), type); }/*from w w w . j a v a2s. c o m*/ return false; }
From source file:com.tugo.dt.PojoUtils.java
/** * Return the getter expression for the given field. * <p>/*from w w w .j ava 2 s .co m*/ * If the field is a public member, the field name is used else the getter function. If no matching field or getter * method is found, the expression is returned unmodified. * * @param pojoClass class to check for the field * @param fieldExpression field name expression * @param exprClass expected field type * @return java code fragment */ private static String getSingleFieldGetterExpression(final Class<?> pojoClass, final String fieldExpression, final Class<?> exprClass) { JavaStatement code = new JavaReturnStatement( pojoClass.getName().length() + fieldExpression.length() + exprClass.getName().length() + 32, exprClass); code.appendCastToTypeExpr(pojoClass, OBJECT).append("."); try { final Field field = pojoClass.getField(fieldExpression); if (ClassUtils.isAssignable(field.getType(), exprClass)) { return code.append(field.getName()).getStatement(); } logger.debug("Field {} can not be assigned to an {}. Proceeding to locate a getter method.", field, exprClass); } catch (NoSuchFieldException ex) { logger.debug("{} does not have field {}. Proceeding to locate a getter method.", pojoClass, fieldExpression); } catch (SecurityException ex) { logger.debug("{} does not have field {}. Proceeding to locate a getter method.", pojoClass, fieldExpression); } String methodName = GET + upperCaseWord(fieldExpression); try { Method method = pojoClass.getMethod(methodName); if (ClassUtils.isAssignable(method.getReturnType(), exprClass)) { return code.append(methodName).append("()").getStatement(); } logger.debug( "method {} of the {} returns {} that can not be assigned to an {}. Proceeding to locate another getter method.", pojoClass, methodName, method.getReturnType(), exprClass); } catch (NoSuchMethodException ex) { logger.debug("{} does not have method {}. Proceeding to locate another getter method.", pojoClass, methodName); } catch (SecurityException ex) { logger.debug("{} does not have method {}. Proceeding to locate another getter method.", pojoClass, methodName); } methodName = IS + upperCaseWord(fieldExpression); try { Method method = pojoClass.getMethod(methodName); if (ClassUtils.isAssignable(method.getReturnType(), exprClass)) { return code.append(methodName).append("()").getStatement(); } logger.debug( "method {} of the {} returns {} that can not be assigned to an {}. Proceeding with the original expression {}.", pojoClass, methodName, method.getReturnType(), exprClass, fieldExpression); } catch (NoSuchMethodException ex) { logger.debug("{} does not have method {}. Proceeding with the original expression {}.", pojoClass, methodName, fieldExpression); } catch (SecurityException ex) { logger.debug("{} does not have method {}. Proceeding with the original expression {}.", pojoClass, methodName, fieldExpression); } return code.append(fieldExpression).getStatement(); }
From source file:com.datatorrent.lib.util.PojoUtils.java
/** * Return the getter expression for the given field. * <p>/* w ww . ja v a2 s . c o m*/ * If the field is a public member, the field name is used else the getter function. If no matching field or getter * method is found, the expression is returned unmodified. * * @param pojoClass class to check for the field * @param fieldExpression field name expression * @param exprClass expected field type * @return java code fragment */ private static String getSingleFieldGetterExpression(final Class<?> pojoClass, final String fieldExpression, final Class<?> exprClass) { JavaStatement code = new JavaReturnStatement( pojoClass.getName().length() + fieldExpression.length() + exprClass.getName().length() + 32, exprClass); code.appendCastToTypeExpr(pojoClass, OBJECT).append("."); try { final Field field = pojoClass.getField(fieldExpression); if (ClassUtils.isAssignable(field.getType(), exprClass)) { return code.append(field.getName()).getStatement(); } logger.debug("Field {} can not be assigned to {}. Proceeding to locate a getter method.", field, exprClass); } catch (NoSuchFieldException ex) { logger.debug("{} does not have field {}. Proceeding to locate a getter method.", pojoClass, fieldExpression); } catch (SecurityException ex) { logger.debug("{} does not have field {}. Proceeding to locate a getter method.", pojoClass, fieldExpression); } String methodName = GET + upperCaseWord(fieldExpression); try { Method method = pojoClass.getMethod(methodName); if (ClassUtils.isAssignable(method.getReturnType(), exprClass)) { return code.append(methodName).append("()").getStatement(); } logger.debug( "method {} of the {} returns {} that can not be assigned to {}. Proceeding to locate another getter method.", pojoClass, methodName, method.getReturnType(), exprClass); } catch (NoSuchMethodException | SecurityException ex) { logger.debug("{} does not have method {}. Proceeding to locate another getter method.", pojoClass, methodName); } methodName = IS + upperCaseWord(fieldExpression); try { Method method = pojoClass.getMethod(methodName); if (ClassUtils.isAssignable(method.getReturnType(), exprClass)) { return code.append(methodName).append("()").getStatement(); } logger.debug( "method {} of the {} returns {} that can not be assigned to {}. Proceeding with the original expression {}.", pojoClass, methodName, method.getReturnType(), exprClass, fieldExpression); } catch (NoSuchMethodException | SecurityException ex) { logger.debug("{} does not have method {}. Proceeding with the original expression {}.", pojoClass, methodName, fieldExpression); } return code.append(fieldExpression).getStatement(); }
From source file:com.tugo.dt.PojoUtils.java
private static String getSingleFieldSetterExpression(final Class<?> pojoClass, final String fieldExpression, final Class<?> exprClass) { JavaStatement code = new JavaStatement( pojoClass.getName().length() + fieldExpression.length() + exprClass.getName().length() + 32); /* Construct ((<pojo class name>)pojo). */ code.appendCastToTypeExpr(pojoClass, OBJECT).append("."); try {/*from ww w . j a va 2s. c o m*/ final Field field = pojoClass.getField(fieldExpression); if (ClassUtils.isAssignable(exprClass, field.getType())) { /* there is public field on the class, use direct assignment. */ /* append <field name> = (<field type>)val; */ return code.append(field.getName()).append(" = ").appendCastToTypeExpr(exprClass, VAL) .getStatement(); } logger.debug("{} can not be assigned to {}. Proceeding to locate a setter method.", exprClass, field); } catch (NoSuchFieldException ex) { logger.debug("{} does not have field {}. Proceeding to locate a setter method.", pojoClass, fieldExpression); } catch (SecurityException ex) { logger.debug("{} does not have field {}. Proceeding to locate a setter method.", pojoClass, fieldExpression); } final String setMethodName = SET + upperCaseWord(fieldExpression); Method bestMatchMethod = null; List<Method> candidates = new ArrayList<Method>(); for (Method method : pojoClass.getMethods()) { if (setMethodName.equals(method.getName())) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1) { if (exprClass == parameterTypes[0]) { bestMatchMethod = method; break; } else if (org.apache.commons.lang.ClassUtils.isAssignable(exprClass, parameterTypes[0])) { candidates.add(method); } } } } if (bestMatchMethod == null) { // We did not find the exact match, use candidates to find the match if (candidates.size() == 0) { logger.debug("{} does not have suitable setter method {}. Returning original expression {}.", pojoClass, setMethodName, fieldExpression); /* We did not find any match at all, use original expression */ /* append = (<expr type>)val;*/ return code.append(fieldExpression).append(" = ").appendCastToTypeExpr(exprClass, VAL) .getStatement(); } else { // TODO: see if we can find a better match bestMatchMethod = candidates.get(0); } } /* We found a method that we may use for setter */ /* append <method name>((<expr class)val); */ return code.append(bestMatchMethod.getName()).append("(").appendCastToTypeExpr(exprClass, VAL).append(")") .getStatement(); }
From source file:com.datatorrent.lib.util.PojoUtils.java
private static String getSingleFieldSetterExpression(final Class<?> pojoClass, final String fieldExpression, final Class<?> exprClass) { JavaStatement code = new JavaStatement( pojoClass.getName().length() + fieldExpression.length() + exprClass.getName().length() + 32); /* Construct ((<pojo class name>)pojo). */ code.appendCastToTypeExpr(pojoClass, OBJECT).append("."); try {//from w w w. j a v a 2 s . c o m final Field field = pojoClass.getField(fieldExpression); if (ClassUtils.isAssignable(exprClass, field.getType())) { /* there is public field on the class, use direct assignment. */ /* append <field name> = (<field type>)val; */ return code.append(field.getName()).append(" = ").appendCastToTypeExpr(exprClass, VAL) .getStatement(); } logger.debug("{} can not be assigned to {}. Proceeding to locate a setter method.", exprClass, field); } catch (NoSuchFieldException ex) { logger.debug("{} does not have field {}. Proceeding to locate a setter method.", pojoClass, fieldExpression); } catch (SecurityException ex) { logger.debug("{} does not have field {}. Proceeding to locate a setter method.", pojoClass, fieldExpression); } final String setMethodName = SET + upperCaseWord(fieldExpression); Method bestMatchMethod = null; List<Method> candidates = new ArrayList<Method>(); for (Method method : pojoClass.getMethods()) { if (setMethodName.equals(method.getName())) { Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 1) { if (exprClass == parameterTypes[0]) { bestMatchMethod = method; break; } else if (ClassUtils.isAssignable(exprClass, parameterTypes[0])) { candidates.add(method); } } } } if (bestMatchMethod == null) { // We did not find the exact match, use candidates to find the match if (candidates.size() == 0) { logger.debug("{} does not have suitable setter method {}. Returning original expression {}.", pojoClass, setMethodName, fieldExpression); /* We did not find any match at all, use original expression */ /* append = (<expr type>)val;*/ return code.append(fieldExpression).append(" = ").appendCastToTypeExpr(exprClass, VAL) .getStatement(); } else { // TODO: see if we can find a better match bestMatchMethod = candidates.get(0); } } /* We found a method that we may use for setter */ /* append <method name>((<expr class)val); */ return code.append(bestMatchMethod.getName()).append("(").appendCastToTypeExpr(exprClass, VAL).append(")") .getStatement(); }
From source file:objenome.solver.evolve.init.Full.java
public static boolean containsSub(Iterable<Class<?>> collection, Class<?> cls) { for (Class<?> c : collection) if (ClassUtils.isAssignable(c, cls)) return true; return false; }