List of usage examples for java.lang.reflect Method getDeclaringClass
@Override
public Class<?> getDeclaringClass()
From source file:org.apache.brooklyn.util.javalang.MethodAccessibleReflections.java
private static Maybe<Method> tryFindAccessibleEquivalent(Method method) { Class<?> clazz = method.getDeclaringClass(); for (Class<?> interf : Reflections.getAllInterfaces(clazz)) { Maybe<Method> altMethod = tryFindAccessibleMethod(interf, method.getName(), method.getParameterTypes()); if (altMethod.isPresent()) { return altMethod; }/* w ww . ja v a 2 s .co m*/ } Class<?> superClazz = clazz.getSuperclass(); while (superClazz != null) { Maybe<Method> altMethod = tryFindAccessibleMethod(superClazz, method.getName(), method.getParameterTypes()); if (altMethod.isPresent()) { return altMethod; } superClazz = superClazz.getSuperclass(); } return Maybe.absent(); }
From source file:com.github.yongchristophertang.engine.java.LoggerProxyHelper.java
static Object addLogger(Logger logger, Method method, Object[] args, Object client) throws Throwable { if (method.getDeclaringClass() == Object.class) { return method.invoke(client, args); }//www . j a va 2s . co m String formatter = "\n\tAPI: " + method.getName() + "\n\n"; formatter += "\tInput:\n"; for (int i = 0; i < method.getParameterCount(); i++) { formatter += "\t\t" + method.getParameters()[i].getName() + " (" + method.getParameters()[i].getType().getSimpleName() + "): "; if (args[i] == null) { formatter += "NULL"; } else if (args[i] instanceof Iterable) { int cnt = 0; Iterator iter = ((Iterable) args[i]).iterator(); while (iter.hasNext()) { formatter += "\n\t\t\t[" + (++cnt) + "]: " + toPrinterString(iter.next(), false); } } else { formatter += toPrinterString(args[i], false); } formatter += "\n"; } long bf = System.nanoTime(); Object result; try { result = method.invoke(client, args); } catch (InvocationTargetException e) { formatter += "\n\tException: \n\t\t" + e.getTargetException(); formatter += "\n=======================================================================\n"; logger.info(formatter); throw e.getTargetException(); } long af = System.nanoTime(); formatter += "\n\tResponse Time(ms): " + (af - bf) / 1000000 + "\n\n\tOutput:\n"; if (result == null) { formatter += "\t\tNULL\n"; } else if (result instanceof Iterable) { Iterator iter = ((Iterable) result).iterator(); int cnt = 0; while (iter.hasNext()) { formatter += "\t\t[" + (++cnt) + "]: " + toPrinterString(iter.next(), true) + "\n"; } if (cnt == 0) { formatter += "\t\tEmpty Collection []\n"; } } else { formatter += "\t\t" + toPrinterString(result, true) + "\n"; } formatter += "=======================================================================\n"; logger.info(formatter); return result; }
From source file:org.dimitrovchi.conf.service.ServiceParameterUtils.java
public static String reflectToString(String name, Object proxy) { final Map<String, Object> map = new LinkedHashMap<>(); for (final Method method : proxy.getClass().getMethods()) { if (method.getDeclaringClass() == Object.class || method.getDeclaringClass() == Proxy.class) { continue; }/*from www . j a va 2 s . c om*/ switch (method.getName()) { case "toString": case "hashCode": case "annotationType": continue; } if (method.getParameterCount() == 0) { try { map.put(method.getName(), method.invoke(proxy)); } catch (ReflectiveOperationException x) { throw new IllegalStateException(x); } } } return name + map; }
From source file:ch.ifocusit.plantuml.utils.ClassUtils.java
public static boolean isGetter(Method method) { try {/* www .j a va 2s . com*/ return Stream.of(Introspector.getBeanInfo(method.getDeclaringClass()).getPropertyDescriptors()) .map(desc -> desc.getReadMethod()).filter(Objects::nonNull) .anyMatch(getter -> getter.equals(method)); } catch (IntrospectionException e) { throw new IllegalStateException(e); } }
From source file:ch.ifocusit.plantuml.utils.ClassUtils.java
public static boolean isSetter(Method method) { try {//from w w w. java2s . c om return Stream.of(Introspector.getBeanInfo(method.getDeclaringClass()).getPropertyDescriptors()) .map(desc -> desc.getWriteMethod()).filter(Objects::nonNull) .anyMatch(setter -> setter.equals(method)); } catch (IntrospectionException e) { throw new IllegalStateException(e); } }
From source file:MethodHashing.java
public static long calculateHash(Method method) { Map methodHashes = (Map) hashMap.get(method.getDeclaringClass()); if (methodHashes == null) { methodHashes = getInterfaceHashes(method.getDeclaringClass()); // Copy and add WeakHashMap newHashMap = new WeakHashMap(); newHashMap.putAll(hashMap);/*from w w w .j a v a 2 s .c o m*/ newHashMap.put(method.getDeclaringClass(), methodHashes); hashMap = newHashMap; } return ((Long) methodHashes.get(method.toString())).longValue(); }
From source file:fi.jumi.core.util.EqualityMatchers.java
private static String findDifference(String path, Object obj1, Object obj2) { if (obj1 == null || obj2 == null) { return obj1 == obj2 ? null : path; }//from w w w . j a v a 2 s .c o m if (obj1.getClass() != obj2.getClass()) { return path; } // collections have a custom equals, but we want deep equality on every collection element // TODO: support other collection types? comparing Sets should be order-independent if (obj1 instanceof List) { List<?> col1 = (List<?>) obj1; List<?> col2 = (List<?>) obj2; int size1 = col1.size(); int size2 = col2.size(); if (size1 != size2) { return path + ".size()"; } for (int i = 0; i < Math.min(size1, size2); i++) { String diff = findDifference(path + ".get(" + i + ")", col1.get(i), col2.get(i)); if (diff != null) { return diff; } } return null; } // use custom equals method if exists try { Method equals = obj1.getClass().getMethod("equals", Object.class); if (equals.getDeclaringClass() != Object.class) { return obj1.equals(obj2) ? null : path; } } catch (NoSuchMethodException e) { throw new RuntimeException(e); } // arrays if (obj2.getClass().isArray()) { int length1 = Array.getLength(obj1); int length2 = Array.getLength(obj2); if (length1 != length2) { return path + ".length"; } for (int i = 0; i < Math.min(length1, length2); i++) { String diff = findDifference(path + "[" + i + "]", Array.get(obj1, i), Array.get(obj2, i)); if (diff != null) { return diff; } } return null; } // structural equality for (Class<?> cl = obj2.getClass(); cl != null; cl = cl.getSuperclass()) { for (Field field : cl.getDeclaredFields()) { try { String diff = findDifference(path + "." + field.getName(), FieldUtils.readField(field, obj1, true), FieldUtils.readField(field, obj2, true)); if (diff != null) { return diff; } } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } return null; }
From source file:com.galenframework.reports.GalenTestInfo.java
public static GalenTestInfo fromMethod(Method method, Object[] arguments) { StringBuilder builder = new StringBuilder( method.getDeclaringClass().getSimpleName() + "#" + method.getName()); if (arguments != null && arguments.length > 0) { builder.append(" ("); boolean shouldUseComma = false; for (Object argument : arguments) { if (shouldUseComma) { builder.append(", "); }//from w w w. j av a 2s . co m builder.append(convertArgumentToString(argument)); shouldUseComma = true; } builder.append(")"); } return GalenTestInfo.fromString(builder.toString()); }
From source file:com.baomidou.framework.common.util.BeanUtil.java
/** * ?//from ww w . j a v a 2 s . c om * * @param source * ? * @param target * ? */ public static void copy(Object source, Object target) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } /* * no copy null properties */ Object value = readMethod.invoke(source); if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
From source file:jp.go.nict.langrid.client.soap.io.SoapRequestWriter.java
public static void writeSoapRequest(OutputStream os, String namespace, Iterable<RpcHeader> headers, Method method, Object... args) throws IOException, IllegalAccessException, InvocationTargetException { Service sa = method.getDeclaringClass().getAnnotation(Service.class); Map<String, Object> bindings = new HashMap<String, Object>(); Class<?>[] paramTypes = method.getParameterTypes(); Annotation[][] paramAnnots = method.getParameterAnnotations(); List<Encoder> params = new ArrayList<Encoder>(); int n = paramTypes.length; for (int i = 0; i < n; i++) { String name = EncoderUtil.getParameterName(paramAnnots[i]); if (name == null) name = "in" + i; params.add(Encoders.create(3, name, paramTypes[i], args[i])); }//from w w w .j a va2s . c o m String ns = namespace; if (ns == null) { if (sa != null && sa.namespace().length() > 0) { ns = sa.namespace(); } else { String sn = method.getDeclaringClass().getSimpleName(); if (sn.endsWith("Service")) { sn = sn.substring(0, sn.length() - 7); } ns = "servicegrid:servicetype:nict.nlp:" + sn; } } bindings.put("namespace", ns); bindings.put("headers", escapeValueXml(headers)); bindings.put("methodName", method.getName()); bindings.put("parameters", params); StreamUtil.writeString(os, jmte.transform(template, bindings), "UTF-8"); }