Example usage for java.lang.reflect Method getDeclaringClass

List of usage examples for java.lang.reflect Method getDeclaringClass

Introduction

In this page you can find the example usage for java.lang.reflect Method getDeclaringClass.

Prototype

@Override
public Class<?> getDeclaringClass() 

Source Link

Document

Returns the Class object representing the class or interface that declares the method represented by this object.

Usage

From source file:io.fabric8.cxf.endpoint.IgnorePropertiesBackedByTransientFields.java

@Override
public boolean isGetterVisible(Method method) {
    boolean answer = defaultChecker.isGetterVisible(method);
    if (answer) {
        answer = isGetterMethodWithFieldVisible(method, getGetterFieldName(method.getName()),
                method.getDeclaringClass())
                && isGetterMethodRetItselfVisible(method, method.getDeclaringClass());
    }/*w  ww  . j ava 2  s  .  c om*/
    return answer;
}

From source file:com.springframework.core.annotation.AnnotationUtils.java

/**
 * Find a single {@link Annotation} of {@code annotationType} on the supplied
 * {@link Method}, traversing its super methods (i.e., from superclasses and
 * interfaces) if no annotation can be found on the given method itself.
 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
 * <p>Meta-annotations will be searched if the annotation is not
 * <em>directly present</em> on the method.
 * <p>Annotations on methods are not inherited by default, so we need to handle
 * this explicitly./*from   w w w  . jav  a  2  s. com*/
 * @param method the method to look for annotations on
 * @param annotationType the annotation type to look for
 * @return the matching annotation, or {@code null} if not found
 * @see #getAnnotation(Method, Class)
 */
@SuppressWarnings("unchecked")
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
    AnnotationCacheKey cacheKey = new AnnotationCacheKey(method, annotationType);
    A result = (A) findAnnotationCache.get(cacheKey);

    if (result == null) {
        Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
        result = findAnnotation((AnnotatedElement) resolvedMethod, annotationType);

        if (result == null) {
            result = searchOnInterfaces(method, annotationType, method.getDeclaringClass().getInterfaces());
        }

        Class<?> clazz = method.getDeclaringClass();
        while (result == null) {
            clazz = clazz.getSuperclass();
            if (clazz == null || clazz.equals(Object.class)) {
                break;
            }
            try {
                Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
                Method resolvedEquivalentMethod = BridgeMethodResolver.findBridgedMethod(equivalentMethod);
                result = findAnnotation((AnnotatedElement) resolvedEquivalentMethod, annotationType);
            } catch (NoSuchMethodException ex) {
                // No equivalent method found
            }
            if (result == null) {
                result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
            }
        }

        if (result != null) {
            findAnnotationCache.put(cacheKey, result);
        }
    }

    return result;
}

From source file:io.fabric8.cxf.endpoint.IgnorePropertiesBackedByTransientFields.java

@Override
public boolean isIsGetterVisible(Method method) {
    boolean answer = defaultChecker.isIsGetterVisible(method);
    if (answer) {
        answer = isGetterMethodWithFieldVisible(method, getIsGetterFieldName(method.getName()),
                method.getDeclaringClass())
                && isGetterMethodRetItselfVisible(method, method.getDeclaringClass());
    }/* w w  w  .  j  av  a 2s  .  c om*/
    return answer;
}

From source file:fr.putnami.pwt.core.service.server.service.CommandExecutorImpl.java

public CommandExecutorImpl(Object service, Method method) {
    this.service = service;
    this.method = method;

    String[] paramTypes = new String[method.getParameterTypes().length];

    for (int i = 0; i < method.getParameterTypes().length; i++) {
        paramTypes[i] = method.getParameterTypes()[i].getCanonicalName();
    }// www .  j av  a  2  s .co  m
    CommandDefinition definition = new CommandDefinition(method.getDeclaringClass().getName(), method.getName(),
            method.getReturnType().getCanonicalName(), paramTypes);
    this.setCommandDefinition(definition);

    this.executorLogger = LogFactory.getLog(method.getDeclaringClass().getCanonicalName());
}

From source file:at.ac.tuwien.infosys.jcloudscale.test.unit.TestReflectionUtil.java

@Test
public void testInvariance() throws Exception {
    SubType obj = new SubType();

    Method method = resolveMethod(obj.getClass(), "invariant", Object.class, String.class);
    assertNotNull(method);/*from w  w  w  .j  a v a  2s  . c  om*/
    assertSame(SuperType.class, method.getDeclaringClass());

    method = resolveMethod(obj.getClass(), "invariant", String.class, Object.class);
    assertNotNull(method);
    assertSame(SubType.class, method.getDeclaringClass());

    assertEquals(null, resolveMethod(obj.getClass(), "invariant", Object.class, Object.class));
}

From source file:com.google.code.ssm.aop.CacheBase.java

protected SerializationType getSerializationType(final Method method) {
    Serialization serialization = method.getAnnotation(Serialization.class);
    if (serialization != null) {
        return serialization.value();
    }/*from  www . j  a  v a2s . c o  m*/

    serialization = method.getDeclaringClass().getAnnotation(Serialization.class);
    if (serialization != null) {
        return serialization.value();
    }

    return null;
}

From source file:io.dyn.core.handler.HandlerMethod.java

public HandlerMethod(Method method, HandlerMethodArgument[] arguments, Guard guard) {
    this.method = method;
    ReflectionUtils.makeAccessible(method);
    this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
    ReflectionUtils.makeAccessible(bridgedMethod);
    this.arguments = arguments;
    this.guard = guard;
    this.invocationHandler = (Proxy.isProxyClass(method.getDeclaringClass())
            ? Proxy.getInvocationHandler(method.getDeclaringClass())
            : null);/*from  w  w  w. j  a  v  a  2s .  c o  m*/
}

From source file:org.bytesoft.bytetcc.supports.spring.CompensableAnnotationValidator.java

private void validateTransactionalPropagation(Method method, Class<?> clazz) throws IllegalStateException {
    Transactional transactional = method.getAnnotation(Transactional.class);
    if (transactional == null) {
        Class<?> declaringClass = method.getDeclaringClass();
        transactional = declaringClass.getAnnotation(Transactional.class);
    }//from  ww  w.java 2s . c  o m

    if (transactional == null) {
        throw new IllegalStateException(
                String.format("Method(%s) must be specificed a Transactional annotation!", method));
    }
    Propagation propagation = transactional.propagation();
    if (Propagation.REQUIRED.equals(propagation) == false //
            && Propagation.MANDATORY.equals(propagation) == false //
            && Propagation.REQUIRES_NEW.equals(propagation) == false) {
        throw new IllegalStateException(
                String.format("Method(%s) not support propagation level: %s!", method, propagation.name()));
    }
}

From source file:io.fabric8.spring.boot.AbstractServiceRegistar.java

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
        BeanDefinitionRegistry registry) {

    for (Method method : REFLECTIONS.getMethodsAnnotatedWith(Factory.class)) {
        String methodName = method.getName();
        Class sourceType = getSourceType(method);
        Class targetType = method.getReturnType();
        Class beanType = method.getDeclaringClass();
        BeanDefinitionHolder holder = createConverterBean(beanType, methodName, sourceType, targetType);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }//from   w  ww . j  a  va 2 s.co  m

    for (Field field : REFLECTIONS.getFieldsAnnotatedWith(ServiceName.class)) {
        Class targetClass = field.getType();
        Alias alias = field.getAnnotation(Alias.class);
        ServiceName name = field.getAnnotation(ServiceName.class);
        PortName port = field.getAnnotation(PortName.class);
        Protocol protocol = field.getAnnotation(Protocol.class);
        External external = field.getAnnotation(External.class);

        String serviceName = name != null ? name.value() : null;

        //We copy the service since we are going to add properties to it.
        Service serviceInstance = new ServiceBuilder(getService(serviceName)).build();
        String servicePort = port != null ? port.value() : null;
        String serviceProtocol = protocol != null ? protocol.value() : DEFAULT_PROTOCOL;
        Boolean serviceExternal = external != null && external.value();
        String serviceAlias = alias != null ? alias.value()
                : createAlias(serviceName, targetClass, serviceProtocol, servicePort, serviceExternal);

        //Add annotation info as additional properties
        serviceInstance.getAdditionalProperties().put(ALIAS, serviceAlias);
        serviceInstance.getAdditionalProperties().put(PROTOCOL, serviceProtocol);
        serviceInstance.getAdditionalProperties().put(EXTERNAL, serviceExternal);

        //We don't want to add a fallback value to the attributes.
        if (port != null) {
            serviceInstance.getAdditionalProperties().put(PORT, servicePort);
        }

        BeanDefinitionHolder holder = createServiceDefinition(serviceInstance, serviceAlias, serviceProtocol,
                servicePort, targetClass);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
    }
}

From source file:hu.bme.mit.sette.common.model.snippet.SnippetInputFactory.java

/**
 * Instantiates a new snippet input factory.
 *
 * @param pContainer//from w ww.ja  v a  2  s .  c o  m
 *            the container
 * @param pMethod
 *            the method
 * @param classLoader
 *            the class loader for loading snippet project classes
 * @throws ValidatorException
 *             if validation has failed
 */
SnippetInputFactory(final SnippetInputFactoryContainer pContainer, final Method pMethod,
        final ClassLoader classLoader) throws ValidatorException {
    Validate.notNull(pContainer, "The container must not be null");
    Validate.notNull(pMethod, "The method must not be null");
    Validate.isTrue(pContainer.getJavaClass().equals(pMethod.getDeclaringClass()),
            "The method must be declared in the Java class " + "stored by the container\n"
                    + "(container.javaClass: [%s])\n" + "(method.declaringClass: [%s])",
            pContainer.getJavaClass(), pMethod.getDeclaringClass());
    container = pContainer;
    method = pMethod;

    // start validation
    MethodValidator v = new MethodValidator(pMethod);
    // modifiers are checked by the container when parsing the Java class

    // check SETTE annotations
    if (!SetteAnnotationUtils.getSetteAnnotations(pMethod).isEmpty()) {
        v.addException("The method must not have any SETTE annotations");
    }

    // check parameter types
    if (pMethod.getParameterTypes().length != 0) {
        v.addException("The method must not have any parameter types");
    }

    // check return type
    if (!pMethod.getReturnType().equals(SnippetInputContainer.class)) {
        v.addException(
                "The method must have the return type of " + SnippetInputContainer.class.getSimpleName());
    }

    // get snippet method
    snippetMethod = pContainer.getSnippetContainer().getSnippets().get(pMethod.getName());

    if (snippetMethod == null) {
        v.addException("The corresponding snippet method was not found");
    }

    v.validate();

    snippetMethod.setInputFactory(this);
}