List of usage examples for java.lang Object getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
public static void setVar(@NonNull Object obj, @NonNull String name, @Nullable Object val) throws Exception { for (Class cls = obj.getClass(); // cls != null && cls != Object.class; // cls = cls.getSuperclass()) { for (Field f : cls.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true);/*from w w w .j av a 2 s . c o m*/ f.set(obj, val); return; } } } throw new RuntimeException("no var matching " + name); }
From source file:Main.java
static Map<String, Object> objectToMap(Object object) { Map<String, Object> map = new HashMap<>(); Class cls = object.getClass(); while (cls != null) { for (Field field : cls.getDeclaredFields()) { field.setAccessible(true);/* www .j a v a2 s .co m*/ Object value = null; try { value = field.get(object); } catch (IllegalAccessException e) { e.printStackTrace(); } if (value != null) map.put(field.getName(), value); } cls = cls.getSuperclass(); } return map; }
From source file:Main.java
/** * Returns of linked list of the objects given in the vararg. * * @param contents the objects to return in the list. * @param <T> the class the given objects are instantiations of. * @return a linked list containing the given contents. * @throws AssertionError if any of the contents are not the same type. */// www . java 2s.com public static <T> List<T> listOf(T... contents) { // Check all items in set are of same type... Class first_class = null; for (Object item : contents) { if (first_class == null) { first_class = item.getClass(); } else { assert first_class == item.getClass(); } } return new LinkedList<T>(Arrays.asList(contents)); }
From source file:Main.java
static public Object invokeMethod(Object owner, String methodName, Object... args) throws Exception { Class<?> ownerClass = owner.getClass(); Class<?>[] argsClass = null; if (args != null && args.length > 0) { argsClass = new Class<?>[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); if (argsClass[i] == Integer.class) { argsClass[i] = int.class; } else if (argsClass[i] == Boolean.class) { argsClass[i] = boolean.class; }// w w w .j a va 2 s . c om } } Method method = ownerClass.getDeclaredMethod(methodName, argsClass); method.setAccessible(true); return method.invoke(owner, args); }
From source file:Main.java
public static <T> T parse(final Class<T> clazz, final InputStream inputStream) { try {//w w w.j a v a 2 s . com final JAXBContext jaxbContext = JAXBContext.newInstance(clazz); final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); final Object deserialized = jaxbUnmarshaller.unmarshal(inputStream); if (clazz.isAssignableFrom(deserialized.getClass())) { return clazz.cast(deserialized); } else { final JAXBElement<T> jaxbElement = (JAXBElement<T>) deserialized; return jaxbElement.getValue(); } } catch (final JAXBException e) { throw new RuntimeException(e); } }
From source file:io.fns.calculator.filter.CORSFilter.java
private static Object getInstanceField(Object instance, String fieldName) throws Throwable { Field field = instance.getClass().getSuperclass().getDeclaredField(fieldName); field.setAccessible(true);// w ww . ja va 2s . c o m return field.get(instance); }
From source file:ch.citux.td.util.Log.java
public static void e(Object caller, Exception exception) { e(caller.getClass(), exception); }
From source file:Main.java
/** * Null safe getClass().toString() call on an object * @param obj the object to get the string representation from. * @return the class string./*from w ww .j a v a 2 s. c o m*/ */ public static String getClassString(Object obj) { if (obj == null) { return "null"; } else { return obj.getClass().toString(); } }
From source file:Main.java
/** * @since 2.4/*from w w w . j a va 2 s .c o m*/ */ public static Marshaller createMarshaller(final Object object) throws JAXBException { JAXBContext context = createContext(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); return marshaller; }
From source file:org.cleverbus.core.common.contextcall.ReflectionCallUtils.java
/** * Invokes target method.// w w w . ja va 2 s. co m * * @param params the parameters of the call * @param beanFactory the Spring bean factory * @return response */ public static Object invokeMethod(ContextCallParams params, BeanFactory beanFactory) { // find target service Object targetService = beanFactory.getBean(params.getTargetType()); // determine method's argument types List<Class> argTypes = new ArrayList<Class>(); for (Object arg : params.getMethodArgs()) { argTypes.add(arg.getClass()); } // exist method? Method method = ReflectionUtils.findMethod(params.getTargetType(), params.getMethodName(), argTypes.toArray(new Class[] {})); if (method == null) { throw new IllegalStateException("there is no method '" + params.getMethodName() + "' on target type '" + params.getTargetType().getSimpleName() + "'"); } // invoke method return ReflectionUtils.invokeMethod(method, targetService, params.getMethodArgs().toArray()); }