List of usage examples for java.lang Class equals
public boolean equals(Object obj)
From source file:ml.shifu.shifu.util.ClassUtils.java
@SuppressWarnings("unchecked") public static List<Field> getAllFields(Class<?> clazz) { if (clazz == null || clazz.equals(Object.class)) { return Collections.EMPTY_LIST; }/* ww w . j av a2 s.co m*/ List<Field> result = new ArrayList<Field>(); for (Field field : clazz.getDeclaredFields()) { result.add(field); } Class<?> tmpClazz = clazz.getSuperclass(); while (!Object.class.equals(tmpClazz)) { result.addAll(getAllFields(tmpClazz)); tmpClazz = tmpClazz.getSuperclass(); } return result; }
From source file:org.geoserver.wps.ppio.ProcessParameterIO.java
public static ProcessParameterIO find(Parameter<?> p, ApplicationContext context, String mime) { // enum special treatment if (p.type.isEnum()) { return new LiteralPPIO(p.type); }//from w ww .j a v a 2s. c o m // TODO: come up with some way to flag one as "default" List<ProcessParameterIO> all = findAll(p, context); if (all.isEmpty()) { return null; } if (mime != null) { for (ProcessParameterIO ppio : all) { if (ppio instanceof ComplexPPIO && ((ComplexPPIO) ppio).getMimeType().equals(mime)) { return ppio; } } } // if more than one sort by class hierarchy, pushing the most specific classes to the // beginning if (all.size() > 0) { Collections.sort(all, new Comparator<ProcessParameterIO>() { public int compare(ProcessParameterIO o1, ProcessParameterIO o2) { Class c1 = o1.getType(); Class c2 = o2.getType(); if (c1.equals(c2)) { return 0; } if (c1.isAssignableFrom(c2)) { return 1; } return -1; } }); } // fall back on the first found return all.get(0); }
From source file:Main.java
/** * Reads a <code>Shape</code> object that has been serialised by the * {@link #writeShape(Shape, ObjectOutputStream)} method. * * @param stream the input stream (<code>null</code> not permitted). * * @return The shape object (possibly <code>null</code>). * * @throws IOException if there is an I/O problem. * @throws ClassNotFoundException if there is a problem loading a class. *//*from w w w. j av a2s. c o m*/ public static Shape readShape(final ObjectInputStream stream) throws IOException, ClassNotFoundException { if (stream == null) { throw new IllegalArgumentException("Null 'stream' argument."); } Shape result = null; final boolean isNull = stream.readBoolean(); if (!isNull) { final Class c = (Class) stream.readObject(); if (c.equals(Line2D.class)) { final double x1 = stream.readDouble(); final double y1 = stream.readDouble(); final double x2 = stream.readDouble(); final double y2 = stream.readDouble(); result = new Line2D.Double(x1, y1, x2, y2); } else if (c.equals(Rectangle2D.class)) { final double x = stream.readDouble(); final double y = stream.readDouble(); final double w = stream.readDouble(); final double h = stream.readDouble(); result = new Rectangle2D.Double(x, y, w, h); } else if (c.equals(Ellipse2D.class)) { final double x = stream.readDouble(); final double y = stream.readDouble(); final double w = stream.readDouble(); final double h = stream.readDouble(); result = new Ellipse2D.Double(x, y, w, h); } else if (c.equals(Arc2D.class)) { final double x = stream.readDouble(); final double y = stream.readDouble(); final double w = stream.readDouble(); final double h = stream.readDouble(); final double as = stream.readDouble(); // Angle Start final double ae = stream.readDouble(); // Angle Extent final int at = stream.readInt(); // Arc type result = new Arc2D.Double(x, y, w, h, as, ae, at); } else if (c.equals(GeneralPath.class)) { final GeneralPath gp = new GeneralPath(); final float[] args = new float[6]; boolean hasNext = stream.readBoolean(); while (!hasNext) { final int type = stream.readInt(); for (int i = 0; i < 6; i++) { args[i] = stream.readFloat(); } switch (type) { case PathIterator.SEG_MOVETO: gp.moveTo(args[0], args[1]); break; case PathIterator.SEG_LINETO: gp.lineTo(args[0], args[1]); break; case PathIterator.SEG_CUBICTO: gp.curveTo(args[0], args[1], args[2], args[3], args[4], args[5]); break; case PathIterator.SEG_QUADTO: gp.quadTo(args[0], args[1], args[2], args[3]); break; case PathIterator.SEG_CLOSE: gp.closePath(); break; default: throw new RuntimeException("JFreeChart - No path exists"); } gp.setWindingRule(stream.readInt()); hasNext = stream.readBoolean(); } result = gp; } else { result = (Shape) stream.readObject(); } } return result; }
From source file:com.siemens.sw360.commonIO.TypeMappings.java
@SuppressWarnings("unchecked") public static <T, U> Function<T, U> getIdentifier(Class<T> clazz, @SuppressWarnings("unused") Class<U> uClass /*used to infer type*/) throws SW360Exception { if (clazz.equals(LicenseType.class)) { return (Function<T, U>) getLicenseTypeIdentifier(); } else if (clazz.equals(Obligation.class)) { return (Function<T, U>) getObligationIdentifier(); } else if (clazz.equals(RiskCategory.class)) { return (Function<T, U>) getRiskCategoryIdentifier(); }//from w w w. j a va 2 s . co m throw new SW360Exception("Unknown Type requested"); }
From source file:Utils.java
public static boolean isTypeCompatible(Class formalType, Class actualType, boolean strict) { if (formalType == null && actualType != null) return false; if (formalType != null && actualType == null) return false; if (formalType == null && actualType == null) return true; if (strict)/* w w w. ja va 2 s.co m*/ return formalType.equals(actualType); else return formalType.isAssignableFrom(actualType); }
From source file:com.junly.common.util.ReflectUtils.java
/** * <p class="detail">//from w w w.j a v a 2 s. c om * * </p> * @author wan.Dong * @date 20161112 * @param obj * @param name ?? * @param value (?)??false * @return */ public static boolean setProperty(Object obj, String name, Object value) { if (obj != null) { Class<?> clazz = obj.getClass(); while (clazz != null) { Field field = null; try { field = clazz.getDeclaredField(name); } catch (Exception e) { clazz = clazz.getSuperclass(); continue; } try { Class<?> type = field.getType(); if (type.isPrimitive() == true && value != null) { if (value instanceof String) { if (type.equals(int.class) == true) { value = Integer.parseInt((String) value); } else if (type.equals(double.class) == true) { value = Double.parseDouble((String) value); } else if (type.equals(boolean.class) == true) { value = Boolean.parseBoolean((String) value); } else if (type.equals(long.class) == true) { value = Long.parseLong((String) value); } else if (type.equals(byte.class) == true) { value = Byte.parseByte((String) value); } else if (type.equals(char.class) == true) { value = Character.valueOf(((String) value).charAt(0)); } else if (type.equals(float.class) == true) { value = Float.parseFloat((String) value); } else if (type.equals(short.class) == true) { value = Short.parseShort((String) value); } } field.setAccessible(true); field.set(obj, value); field.setAccessible(false); } if (value == null || type.equals(value.getClass()) == true) { field.setAccessible(true); field.set(obj, value); field.setAccessible(false); } return true; } catch (Exception e) { return false; } } } return false; }
From source file:com.blurengine.blur.framework.ticking.TickMethodsCache.java
/** * Loads and caches a {@link Class}/*from w w w.j a v a 2 s . c om*/ * * @param clazz class to load and cache for future usage * * @return list of {@link TickMethod} represented the cached methods * * @throws IllegalArgumentException thrown if {@code clazz} is {@code Tickable.class} */ public static Collection<TickMethod> loadClass(@Nonnull Class<?> clazz) throws IllegalArgumentException { Preconditions.checkNotNull(clazz, "clazz cannot be null."); if (!LOADED_CLASSES.contains(clazz)) { Collection<TickMethod> tickMethods = CLASS_TICK_METHODS.get(clazz); // empty cache list, automatically updates { // Load superclasses first Class<?> superclass = clazz.getSuperclass(); // No need for while loop as loadClass will end up reaching here if necessary. if (!superclass.equals(Object.class)) { tickMethods.addAll(loadClass(superclass)); } } for (Method method : clazz.getDeclaredMethods()) { TickMethod tickMethod = getTickMethod(clazz, method); if (tickMethod != null) { tickMethods.add(tickMethod); } else { Tick tick = method.getDeclaredAnnotation(Tick.class); if (tick != null) { try { Preconditions.checkArgument(method.getParameterCount() <= 1, "too many parameters in tick method " + method.getName() + "."); if (method.getParameterCount() > 0) { Preconditions.checkArgument( method.getParameterTypes()[0].isAssignableFrom(TickerTask.class), "Invalid parameter in tick method " + method.getName() + "."); } boolean passParams = method.getParameterCount() > 0; // Tickables may be marked private for organisation. method.setAccessible(true); tickMethods.add(new TickMethod(method, passParams, tick)); } catch (Exception e) { e.printStackTrace(); } } } } LOADED_CLASSES.add(clazz); } return Collections.unmodifiableCollection(CLASS_TICK_METHODS.get(clazz)); }
From source file:gumga.framework.presentation.api.CSVGeneratorAPI.java
public static List<Field> getAllAtributes(Class clazz) { List<Field> fields = new ArrayList<>(); Class superClass = clazz.getSuperclass(); if (superClass != null && !superClass.equals(Object.class)) { fields.addAll(getAllAtributes(clazz.getSuperclass())); }/*ww w.j a v a2s . co m*/ for (Field f : clazz.getDeclaredFields()) { if (!Modifier.isStatic(f.getModifiers())) { fields.add(f); } } return fields; }
From source file:NumberUtils.java
/** * Convert the given number into an instance of the given target class. * * @param number the number to convert * @param targetClass the target class to convert to * @return the converted number/*from w w w . j ava 2 s. c om*/ * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see java.lang.Byte * @see java.lang.Short * @see java.lang.Integer * @see java.lang.Long * @see java.math.BigInteger * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal */ public static Number convertNumberToTargetClass(Number number, Class targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.byteValue(); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.shortValue(); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.intValue(); } else if (targetClass.equals(Long.class)) { return number.longValue(); } else if (targetClass.equals(Float.class)) { return number.floatValue(); } else if (targetClass.equals(Double.class)) { return number.doubleValue(); } else if (targetClass.equals(BigInteger.class)) { return BigInteger.valueOf(number.longValue()); } else if (targetClass.equals(BigDecimal.class)) { // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return new BigDecimal(number.toString()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); } }
From source file:com.siemens.sw360.commonIO.TypeMappings.java
@SuppressWarnings("unchecked") public static <T> List<T> addAlltoDB(LicenseService.Iface licenseClient, Class<T> clazz, List<T> candidates) throws TException { if (candidates != null && !candidates.isEmpty()) { if (clazz.equals(LicenseType.class)) { return (List<T>) licenseClient.addLicenseTypes((List<LicenseType>) candidates); } else if (clazz.equals(Obligation.class)) { return (List<T>) licenseClient.addObligations((List<Obligation>) candidates); } else if (clazz.equals(RiskCategory.class)) { return (List<T>) licenseClient.addRiskCategories((List<RiskCategory>) candidates); }/*from w ww . ja va2 s. c o m*/ } throw new SW360Exception("Unknown Type requested"); }