List of usage examples for java.lang Class equals
public boolean equals(Object obj)
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
private static boolean compareClass(Class<?> clazz1, Class<?> clazz2) { if (ClassUtils.isPrimitiveWrapper(clazz1)) clazz1 = ClassUtils.wrapperToPrimitive(clazz1); if (ClassUtils.isPrimitiveWrapper(clazz2)) clazz2 = ClassUtils.wrapperToPrimitive(clazz2); if (clazz1 != null) { return clazz1.equals(clazz2); } else if (clazz2 != null) { return clazz2.equals(clazz1); } else {//from w ww .j ava 2s .co m return true; } }
From source file:com.jsen.javascript.java.HostedJavaMethod.java
/** * Casts the given arguments into given expected types. * //from w w w .ja v a2s.com * @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:GenericUtils.java
/** * Get the Generic definitions from a class for given class without looking * super classes.//from ww w .j a v a 2s .co m * @param classFrom Implementing class * @param interfaceClz class with generic definition * @return null if not found */ @SuppressWarnings("unchecked") public static Type[] getGenericDefinitonsThis(Class classFrom, Class interfaceClz) { Type[] genericInterfaces = classFrom.getGenericInterfaces(); for (Type type : genericInterfaces) { if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (interfaceClz.isAssignableFrom((Class) pt.getRawType())) { return pt.getActualTypeArguments(); } } } // check if it if available on generic super class Type genericSuperclass = classFrom.getGenericSuperclass(); if (genericSuperclass instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericSuperclass; if (interfaceClz.equals(pt.getRawType())) { return pt.getActualTypeArguments(); } } return null; }
From source file:com.yx.baseframe.util.ReflectionUtils.java
/** * Get the actual type arguments a child class has used to extend a generic * base class. (Taken from http://www.artima.com/weblogs/viewpost.jsp?thread=208860. Thanks * mathieu.grenonville for finding this solution!) * /*from w w w. ja v a2 s. com*/ * @param baseClass * the base class * @param childClass * the child class * @return a list of the raw classes for the actual type arguments. */ public static <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass) { Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(); Type type = childClass; // start walking up the inheritance hierarchy until we hit baseClass while (!getClass(type).equals(baseClass)) { if (type instanceof Class) { // there is no useful information for us in raw types, so just // keep going. type = ((Class) type).getGenericSuperclass(); } else { ParameterizedType parameterizedType = (ParameterizedType) type; Class<?> rawType = (Class) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); for (int i = 0; i < actualTypeArguments.length; i++) { resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); } if (!rawType.equals(baseClass)) { type = rawType.getGenericSuperclass(); } } } // finally, for each actual type argument provided to baseClass, // determine (if possible) // the raw class for that type argument. Type[] actualTypeArguments; if (type instanceof Class) { actualTypeArguments = ((Class) type).getTypeParameters(); } else { actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); } List<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>(); // resolve types by chasing down type variables. for (Type baseType : actualTypeArguments) { while (resolvedTypes.containsKey(baseType)) { baseType = resolvedTypes.get(baseType); } typeArgumentsAsClasses.add(getClass(baseType)); } return typeArgumentsAsClasses; }
From source file:com.germinus.easyconf.ComponentProperties.java
protected static Object getTypedPropertyWithDefault(String key, Class theClass, Configuration properties, Object defaultValue) {/* www.j a v a 2 s . c o m*/ if (theClass.equals(Float.class)) { return properties.getFloat(key, (Float) defaultValue); } else if (theClass.equals(Integer.class)) { return properties.getInteger(key, (Integer) defaultValue); } else if (theClass.equals(String.class)) { return properties.getString(key, (String) defaultValue); } else if (theClass.equals(Double.class)) { return properties.getDouble(key, (Double) defaultValue); } else if (theClass.equals(Long.class)) { return properties.getLong(key, (Long) defaultValue); } else if (theClass.equals(Boolean.class)) { return properties.getBoolean(key, (Boolean) defaultValue); } else if (theClass.equals(List.class)) { return properties.getList(key, (List) defaultValue); } else if (theClass.equals(BigInteger.class)) { return properties.getBigInteger(key, (BigInteger) defaultValue); } else if (theClass.equals(BigDecimal.class)) { return properties.getBigDecimal(key, (BigDecimal) defaultValue); } else if (theClass.equals(Byte.class)) { return properties.getByte(key, (Byte) defaultValue); } else if (theClass.equals(Short.class)) { return properties.getShort(key, (Short) defaultValue); } throw new IllegalArgumentException("Class " + theClass + " is not" + "supported for properties"); }
From source file:com.plusub.lib.annotate.JsonParserUtils.java
/** * ?set??/*from ww w. java 2 s . co m*/ * <p>Title: getSetMethodName * <p>Description: * @param field * @return */ private static String getSetMethodName(Field field) { Class clazz = field.getType(); String name = field.getName(); StringBuilder sb = new StringBuilder(); //boolean ?set? //private boolean ishead; setIshead //private boolean isHead; setHead //private boolean head; setHead if (clazz.equals(Boolean.class) || clazz.getName().equals("boolean")) { sb.append("set"); if (name.startsWith("is")) { if (name.length() > 2) { //private boolean isHead; setHead if (Character.isUpperCase(name.charAt(2))) { sb.append(name.substring(2)); } else {//private boolean ishead; setIshead sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } } else { sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } } else {//private boolean head; setHead sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } } else { sb.append("set"); sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } return sb.toString(); }
From source file:edu.umass.cs.gigapaxos.paxospackets.PaxosPacket.java
static void checkFields(Class<?> clazz, GetType[] expFields) { if (!STRICT_ADDRESS_CHECKS) return;//w ww . j a v a2 s . co m Field[] fields = clazz.getDeclaredFields(); boolean print = edu.umass.cs.gigapaxos.PaxosManager.getLogger().isLoggable(Level.FINE); if (print) System.out.println("Checking fields for " + clazz); for (int i = 0, j = 0; i < fields.length; j += (isStatic(fields[i]) ? 0 : 1), i++) { if (isStatic(fields[i])) continue; if (j >= expFields.length) break; if (print) System.out.println(" " + fields[j]); boolean match = true; String field = fields[i].getName(); Class<?> type = fields[i].getType(); match = match && field.equals(expFields[j].toString()) && type.equals(expFields[j].getType()); if (!match) throw new RuntimeException("Field " + i + " = " + type + " " + field + " does not match expected " + expFields[j].getType() + " " + expFields[j].toString()); } }
From source file:io.fabric8.jolokia.support.JolokiaHelpers.java
public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException { if (clazz.isArray()) { if (value instanceof JSONArray) { JSONArray jsonArray = (JSONArray) value; Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size()); int idx = 0; for (Object element : jsonArray) { Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element)); }/*from ww w. j a v a2 s . c o m*/ return javaArray; } else { return null; } } else if (String.class.equals(clazz)) { return (value != null) ? value.toString() : null; } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) { Number number = asNumber(value); return number != null ? number.byteValue() : null; } else if (clazz.equals(Short.class) || clazz.equals(short.class)) { Number number = asNumber(value); return number != null ? number.shortValue() : null; } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) { Number number = asNumber(value); return number != null ? number.intValue() : null; } else if (clazz.equals(Long.class) || clazz.equals(long.class)) { Number number = asNumber(value); return number != null ? number.longValue() : null; } else if (clazz.equals(Float.class) || clazz.equals(float.class)) { Number number = asNumber(value); return number != null ? number.floatValue() : null; } else if (clazz.equals(Double.class) || clazz.equals(double.class)) { Number number = asNumber(value); return number != null ? number.doubleValue() : null; } else if (value instanceof JSONObject) { JSONObject jsonObject = (JSONObject) value; if (!JSONObject.class.isAssignableFrom(clazz)) { String json = jsonObject.toJSONString(); return getObjectMapper().readerFor(clazz).readValue(json); } } return value; }
From source file:org.paxml.util.ReflectUtils.java
/** * Check if a class implements an interface class. * //from w w w. ja v a 2s.c o m * @param implementingClass * the implementing class * @param interfaceClass * the interface class * @param matchNameOnly * true to only do name string comparison, false also compares * the class loader. * @return true if yes, false not */ public static boolean isImplementingClass(Class<?> implementingClass, final Class<?> interfaceClass, final boolean matchNameOnly) { if (!interfaceClass.isInterface() || implementingClass.isInterface() || implementingClass.equals(interfaceClass)) { return false; } Object result = traverseInheritance(implementingClass, interfaceClass, true, new IClassVisitor<Object>() { public Object onVisit(Class<?> clazz) { if (matchNameOnly) { return clazz.getName().equals(interfaceClass.getName()) ? new Object() : null; } else { return clazz.equals(interfaceClass) ? new Object() : null; } } }); return result != null; }
From source file:ca.uhn.fhir.model.api.ResourceMetadataKeyEnum.java
@SuppressWarnings("unchecked") private static <T extends Enum<?>> T getEnumFromMetadataOrNullIfNone( Map<ResourceMetadataKeyEnum<?>, Object> theResourceMetadata, ResourceMetadataKeyEnum<T> theKey, Class<T> theEnumType, IValueSetEnumBinder<T> theBinder) { Object retValObj = theResourceMetadata.get(theKey); if (retValObj == null) { return null; } else if (theEnumType.equals(retValObj.getClass())) { return (T) retValObj; } else if (retValObj instanceof String) { return theBinder.fromCodeString((String) retValObj); }/* ww w. j a v a 2 s .c o m*/ throw new InternalErrorException("Found an object of type '" + retValObj.getClass().getCanonicalName() + "' in resource metadata for key " + theKey.name() + " - Expected " + InstantDt.class.getCanonicalName()); }