List of usage examples for java.lang Object getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:core.Helpers.java
/** * will return true if x is of teh same class as y * * @param x/*www . j a v a 2 s .c om*/ * @param y * @return */ public static boolean compare(Object x, Object y) { if (x.getClass().equals(y.getClass())) { return true; } else { return false; } }
From source file:Main.java
/** * Locates a given method anywhere in the class inheritance hierarchy. * * @param instance an object to search the method into. * @param name method name/* w ww.jav a2 s .c om*/ * @param parameterTypes method parameter types * @return a method object * @throws NoSuchMethodException if the method cannot be located */ static Method findMethod(Object instance, String name, Class<?>... parameterTypes) throws NoSuchMethodException { for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) { try { Method method = clazz.getDeclaredMethod(name, parameterTypes); if (!method.isAccessible()) { method.setAccessible(true); } return method; } catch (NoSuchMethodException e) { // ignore and search next } } throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList(parameterTypes) + " not found in " + instance.getClass()); }
From source file:Main.java
public static void marshal(Object o, OutputStream os, Map<String, Object> properties) { try {// www. j av a2s . c om JAXBContext ctx = JAXBContext.newInstance(o.getClass()); Marshaller marshaller = ctx.createMarshaller(); for (Entry<String, Object> p : properties.entrySet()) { marshaller.setProperty(p.getKey(), p.getValue()); } marshaller.marshal(o, os); } catch (JAXBException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void buildShortClassTag(Object cls, StringBuilder out) { if (cls == null) { out.append("null"); return;/*from w w w .j av a 2 s. c om*/ } String simpleName = cls.getClass().getSimpleName(); if (simpleName == null || simpleName.length() <= 0) { simpleName = cls.getClass().getName(); int end = simpleName.lastIndexOf(46); if (end > 0) { simpleName = simpleName.substring(end + 1); } } out.append(simpleName); out.append('{'); out.append(Integer.toHexString(System.identityHashCode(cls))); }
From source file:Main.java
public static String toXxml(Object bean) { StringWriter stringWriter = null; try {/*from w ww. j ava 2s. co m*/ JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); stringWriter = new StringWriter(); marshaller.marshal(bean, stringWriter); String result = stringWriter.toString(); // remove xml declaration result = result.replaceFirst(".*\n", ""); return result; } catch (JAXBException e) { throw new RuntimeException(e); } finally { if (stringWriter != null) try { stringWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
private static boolean equalFields(Object paramObject1, Object paramObject2) { boolean bool1 = false; Field[] arrayOfField1 = paramObject1.getClass().getDeclaredFields(); Field[] arrayOfField2 = paramObject2.getClass().getDeclaredFields(); if (arrayOfField1.length != arrayOfField2.length) { return bool1; }/*w w w . j a v a 2 s . co m*/ int i = 0; try { while (true) { if (i >= arrayOfField1.length) break; Field localField1 = arrayOfField1[i]; localField1.setAccessible(true); Field localField2 = arrayOfField2[i]; localField2.setAccessible(true); Object localObject1 = localField1.get(paramObject1); Object localObject2 = localField2.get(paramObject2); if ((localObject1 == null) && (localObject2 != null)) break; if (localObject1 != null) { boolean bool2 = localObject1.equals(localObject2); if (!bool2) break; } i++; } } catch (IllegalArgumentException localIllegalArgumentException) { localIllegalArgumentException.printStackTrace(); bool1 = true; } catch (IllegalAccessException localIllegalAccessException) { label122: localIllegalAccessException.printStackTrace(); } return bool1; }
From source file:net.fabricmc.loom.task.ProcessModsTask.java
public static void addFile(File file, Object object) { try {/*from ww w. j ava 2s .co m*/ URLClassLoader classLoader = (URLClassLoader) object.getClass().getClassLoader(); Class urlClassLoaderClass = URLClassLoader.class; Method method = urlClassLoaderClass.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(classLoader, file.toURI().toURL()); } catch (NoSuchMethodException | IllegalAccessException | MalformedURLException | InvocationTargetException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Called internally by installGtkPopupBugWorkaround to fix the thickness * of a GTK style field by setting it to a minimum value of 1. * /*from www .ja va 2 s . co m*/ * @param style * The GTK style object. * @param fieldName * The field name. * @throws Exception * When reflection fails. */ private static void fixGtkThickness(Object style, String fieldName) throws Exception { Field field = style.getClass().getDeclaredField(fieldName); boolean accessible = field.isAccessible(); field.setAccessible(true); field.setInt(style, Math.max(1, field.getInt(style))); field.setAccessible(accessible); }
From source file:Main.java
/** * Convert bundle to readable string/*from w w w .j av a2 s.c o m*/ * * @param bundle The bundle to convert * @return String representation of bundle */ public static String toString(Bundle bundle) { if (bundle == null) { return null; } StringBuilder stringBuilder = new StringBuilder(); for (String key : bundle.keySet()) { Object value = bundle.get(key); stringBuilder.append( String.format("%s %s (%s)\n", key, value, value == null ? "null" : value.getClass().getName())); } return stringBuilder.substring(0, stringBuilder.length() - 1); }
From source file:Main.java
/** * Copy fields from parent object to child object. * * @param parent parent object/* www . j ava2s. c om*/ * @param child child object * @param <T> child class * @return filled child object */ public static <T> T shallowCopy(Object parent, T child) { try { List<Field> fields = new ArrayList<>(); Class clazz = parent.getClass(); do { fields.addAll(Arrays.asList(clazz.getDeclaredFields())); } while (!(clazz = clazz.getSuperclass()).equals(Object.class)); for (Field field : fields) { field.setAccessible(true); field.set(child, field.get(parent)); } return child; } catch (Exception e) { throw new RuntimeException(e); } }