List of usage examples for java.lang.reflect Method getDeclaringClass
@Override
public Class<?> getDeclaringClass()
From source file:springfox.documentation.swagger.readers.parameter.ParameterAnnotationReader.java
@SuppressWarnings("unchecked") private static <A extends Annotation> A searchOnInterfaces(Method method, int parameterIndex, Class<A> annotationType, Class<?>[] interfaces) { A annotation = null;//from w ww .j av a 2s . c o m for (Class<?> interfaze : interfaces) { Optional<Method> interfaceMethod = interfaceMethod(interfaze, method); if (interfaceMethod.isPresent()) { Method superMethod = interfaceMethod.get(); Optional<Annotation> found = tryFind( newArrayList(superMethod.getParameterAnnotations()[parameterIndex]), annotationOfType(annotationType)); if (found.isPresent()) { annotation = (A) found.get(); break; } Class<?>[] superInterfaces = superMethod.getDeclaringClass().getInterfaces(); annotation = searchOnInterfaces(superMethod, parameterIndex, annotationType, superInterfaces); } } return annotation; }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static String prettyPrint(Method method) { StringBuilder sb = new StringBuilder(); Class<?> declaredOn = method.getDeclaringClass(); sb.append(prettyPrint(declaredOn)).append(".").append(method.getName()).append("("); if (method.getParameterCount() > 0) { for (Type paramType : method.getGenericParameterTypes()) { sb.append(prettyPrint(paramType)).append(", "); }/* w ww . jav a 2 s .c om*/ sb.delete(sb.length() - 2, sb.length()); } sb.append(")"); return sb.toString(); }
From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java
/** * Get a single {@link Annotation} of <code>annotationType</code> from the supplied {@link Method}, traversing its * super methods if no annotation can be found on the given method itself. * <p>/*from w w w.ja va 2 s . c o m*/ * Annotations on methods are not inherited by default, so we need to handle this explicitly. * * @param method * the method to look for annotations on * @param annotationType * the annotation class to look for * @return the annotation found, or <code>null</code> if none found */ public static <A extends Annotation> A findAnnotation(final Method method, final Class<A> annotationType) { A annotation = getAnnotation(method, annotationType); Class<?> cl = method.getDeclaringClass(); if (annotation == null) { annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces()); } while (annotation == null) { cl = cl.getSuperclass(); if ((cl == null) || (cl == Object.class)) { break; } try { final Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes()); annotation = getAnnotation(equivalentMethod, annotationType); if (annotation == null) { annotation = searchOnInterfaces(method, annotationType, cl.getInterfaces()); } } catch (final NoSuchMethodException ex) { // We're done... } } return annotation; }
From source file:de.escalon.hypermedia.spring.AffordanceBuilder.java
/** * @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(Method, Object...) *///from w w w . j a va2 s . c om public static AffordanceBuilder linkTo(Method method, Object... parameters) { return linkTo(method.getDeclaringClass(), method, parameters); }
From source file:com.taobao.rpc.doclet.RPCAPIInfoHelper.java
public static RPCAPIInfo getAPIInfo(String realPath, Method method) { RPCAPIInfo apiInfo = new RPCAPIInfo(); ClassInfo classInfo = RPCAPIDocletUtil.getClassInfo(method.getDeclaringClass().getName()); MethodInfo methodInfo = null;/* w w w .j ava 2 s .c o m*/ if (classInfo != null) { methodInfo = classInfo.getMethodInfo(method.getName()); } if (methodInfo != null) { apiInfo.setComment(methodInfo.getComment()); } Annotation[][] parameterAnnotations = method.getParameterAnnotations(); Security securityAnnotation = method.getAnnotation(Security.class); ResourceMapping resourceMapping = method.getAnnotation(ResourceMapping.class); Object returnType = null; returnType = buildTypeStructure(method.getReturnType(), method.getGenericReturnType(), null); boolean checkCSRF = false; if (securityAnnotation != null) { checkCSRF = securityAnnotation.checkCSRF(); } apiInfo.setPattern(realPath.replaceAll("///", "/")); apiInfo.setCheckCSRF(checkCSRF); apiInfo.setResponseMimes(StringUtils.arrayToDelimitedString(resourceMapping.produces(), ",")); apiInfo.setHttpMethod(StringUtils.arrayToDelimitedString(resourceMapping.method(), ",")); List<RPCAPIInfo.Parameter> parameters = new ArrayList<RPCAPIInfo.Parameter>(); RPCAPIInfo.Parameter parameter = null; LocalVariableTableParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); String[] paramNames = nameDiscoverer.getParameterNames(method); int i = 0; Class<?>[] parameterTypes = method.getParameterTypes(); Type[] genericParameterTypes = method.getGenericParameterTypes(); Class<?> paramType = null; Type genericType; for (int k = 0; k < parameterTypes.length; k++) { paramType = parameterTypes[k]; genericType = genericParameterTypes[k]; Annotation[] pAnnotations = parameterAnnotations[i]; if (HttpServletRequest.class.isAssignableFrom(paramType) || HttpServletResponse.class.isAssignableFrom(paramType) || ErrorContext.class.isAssignableFrom(paramType)) { continue; } // end if String realParamName = paramNames[k]; if (pAnnotations.length == 0) { parameter = apiInfo.new Parameter(); parameter.setName(realParamName); parameter.setType(buildTypeStructure(paramType, genericType, null)); parameters.add(parameter); setParameterComment(parameter, realParamName, methodInfo); continue; } // end if for (Annotation annotation : pAnnotations) { parameter = apiInfo.new Parameter(); setParameterComment(parameter, realParamName, methodInfo); if (annotation instanceof RequestParam) { RequestParam requestParam = (RequestParam) annotation; parameter.setName(requestParam.name()); parameter.setType(buildTypeStructure(paramType, genericType, null)); if (!StringUtil.isBlank(requestParam.defaultValue())) { parameter.setDefaultValue(requestParam.defaultValue().trim()); } parameters.add(parameter); } else if (annotation instanceof PathParam) { PathParam pathParam = (PathParam) annotation; parameter.setName(pathParam.name()); parameter.setType(buildTypeStructure(paramType, genericType, null)); if (!StringUtil.isBlank(pathParam.defaultValue())) { parameter.setDefaultValue(pathParam.defaultValue().trim()); } parameters.add(parameter); } else if (annotation instanceof JsonParam) { JsonParam pathParam = (JsonParam) annotation; parameter.setName(pathParam.value()); parameter.setType(buildTypeStructure(paramType, genericType, null)); parameters.add(parameter); } else if (annotation instanceof RequestParams) { parameter.setName(realParamName); parameter.setType(buildTypeStructure(paramType, genericType, null)); parameters.add(parameter); } else if (annotation instanceof File) { File file = (File) annotation; parameter.setName(file.value()); parameter.setType(""); parameters.add(parameter); } // end if } // end for i++; } // end for apiInfo.setParmeters(parameters); apiInfo.setReturnType(returnType); return apiInfo; }
From source file:mondrian.xmla.impl.Olap4jXmlaServlet.java
/** * Returns something that implements {@link OlapConnection} but still * behaves as the wrapper returned by the connection pool. * * <p>In other words we want the "close" method to play nice and do all the * pooling actions while we want all the olap methods to execute directly on * the un-wrapped OlapConnection object. *///from w w w. j a v a 2s . co m private static OlapConnection createDelegatingOlapConnection(final Connection connection, final OlapConnection olapConnection) { return (OlapConnection) Proxy.newProxyInstance(olapConnection.getClass().getClassLoader(), new Class[] { OlapConnection.class }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("unwrap".equals(method.getName()) || OlapConnection.class.isAssignableFrom(method.getDeclaringClass())) { return method.invoke(olapConnection, args); } else { return method.invoke(connection, args); } } }); }
From source file:kelly.util.BeanUtils.java
/** * Copy the property values of the given source bean into the given target bean. * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean// ww w .j av a2 s.c om * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) { Validate.notNull(source, "Source must not be null"); Validate.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new BeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:org.apache.ode.jacob.vpu.JacobVPU.java
static String stringifyMethods(Class kind) { StringBuffer buf = new StringBuffer(); Method[] methods = kind.getMethods(); boolean found = false; for (Method method : methods) { if (method.getDeclaringClass() == Object.class) { continue; }// www .j a v a 2s . co m if (found) { buf.append(" & "); } buf.append(method.getName()).append('('); Class[] argTypes = method.getParameterTypes(); for (int j = 0; j < argTypes.length; ++j) { if (j > 0) { buf.append(", "); } buf.append(argTypes[j].getName()); } buf.append(") {...}"); found = true; } return buf.toString(); }
From source file:com.simiacryptus.mindseye.lang.Layer.java
/** * From json nn key.//from w ww.j a v a 2 s. c o m * * @param json the json * @param rs the rs * @return the nn key */ @Nonnull static Layer fromJson(@Nonnull final JsonObject json, Map<CharSequence, byte[]> rs) { JsonElement classElement = json.get("class"); assert null != classElement : json.toString(); final String className = classElement.getAsString(); try { final Class<?> clazz = Class.forName(className); if (null == clazz) throw new ClassNotFoundException(className); final Method method = clazz.getMethod("fromJson", JsonObject.class, Map.class); if (method.getDeclaringClass() == Layer.class) { throw new IllegalArgumentException("Cannot find deserialization method for " + className); } @Nonnull Layer invoke = (Layer) method.invoke(null, json, rs); if (null == invoke) throw new IllegalStateException(); return invoke; } catch (@Nonnull IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) { throw new RuntimeException(e); } }
From source file:ObjectInspector.java
/** * Attempts to build a nicer looking string than the basic * {@link Object}.toString()/* w ww .j ava 2 s .c o m*/ * * @param o * The object to build from * @return A descriptive string */ private static String buildString(Object o) { if (o == null) { return "null"; } // first see if there is a version of toString more specific // than that supplied by Object... try { Method m = o.getClass().getMethod("toString"); if (!m.getDeclaringClass().equals(Object.class)) { return o.toString(); } } catch (SecurityException e) { } catch (NoSuchMethodException e) { } // then see if it is an array... if (o.getClass().isArray()) { StringBuilder buff = new StringBuilder(" [ "); for (int i = 0; i < Array.getLength(o); i++) { /* * this could recurse infinitely, but only if the user is * trying to be malicious, like so - Object[] array = new * Object[ 1 ]; array[ 0 ] = array; - which, I'm sure * we'll agree, is and odd thing to do. I say let the * StackOverflowException catch it. */ buff.append(buildString(Array.get(o, i))); buff.append(", "); } if (Array.getLength(o) > 0) { buff.delete(buff.length() - 2, buff.length()); } buff.append(" ]"); return buff.toString(); } return getObjectPosition(o); }