Example usage for java.lang.reflect Member getDeclaringClass

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

Introduction

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

Prototype

public Class<?> getDeclaringClass();

Source Link

Document

Returns the Class object representing the class or interface that declares the member or constructor represented by this Member.

Usage

From source file:Main.java

/**
 * Method called to check if we can use the passed method or constructor
 * (wrt access restriction -- public methods can be called, others
 * usually not); and if not, if there is a work-around for
 * the problem.//from www  . j  ava2  s .co m
 */
public static void checkAndFixAccess(Member member) {
    // We know all members are also accessible objects...
    AccessibleObject ao = (AccessibleObject) member;

    /* 14-Jan-2009, tatu: It seems safe and potentially beneficial to
     *   always to make it accessible (latter because it will force
     *   skipping checks we have no use for...), so let's always call it.
     */
    //if (!ao.isAccessible()) {
    try {
        ao.setAccessible(true);
    } catch (SecurityException se) {
        /* 17-Apr-2009, tatu: Related to [JACKSON-101]: this can fail on
         *    platforms like EJB and Google App Engine); so let's
         *    only fail if we really needed it...
         */
        if (!ao.isAccessible()) {
            Class<?> declClass = member.getDeclaringClass();
            throw new IllegalArgumentException("Can not access " + member + " (from class "
                    + declClass.getName() + "; failed to set access: " + se.getMessage());
        }
    }
    //}
}

From source file:org.batoo.jpa.common.reflect.ReflectHelper.java

/**
 * Returns the accessor for the member//from   w w  w  .j ava2s  .c  o m
 * 
 * @param javaMember
 *            the java member
 * @return the accessor
 * 
 * @since $version
 * @author hceylan
 */
public static AbstractAccessor getAccessor(Member javaMember) {
    if (javaMember instanceof Field) {
        return ReflectHelper.unsafe != null ? new UnsafeFieldAccessor((Field) javaMember)
                : new FieldAccessor((Field) javaMember);
    } else {
        final String name = javaMember.getName().startsWith(ReflectHelper.IS_PREFIX)
                ? javaMember.getName().substring(2)
                : javaMember.getName().substring(3);
        Field field;
        try {
            field = javaMember.getDeclaringClass().getDeclaredField(StringUtils.uncapitalize(name));
            return new FieldAccessor(field);
        } catch (final Exception e) {
            throw new BatooException("Cannot find instance variable field "
                    + javaMember.getDeclaringClass().getName() + "." + name);
        }
    }
}

From source file:org.batoo.common.reflect.ReflectHelper.java

/**
 * Returns the accessor for the member//from w  w w.j  a v a 2s. c  om
 * 
 * @param javaMember
 *            the java member
 * @return the accessor
 * 
 * @since 2.0.1
 */
public static AbstractAccessor getAccessor(Member javaMember) {
    if (javaMember instanceof Field) {
        return ReflectHelper.unsafe != null ? new UnsafeFieldAccessor((Field) javaMember)
                : new FieldAccessor((Field) javaMember);
    } else {
        String name = javaMember.getName().startsWith(ReflectHelper.IS_PREFIX)
                ? javaMember.getName().substring(2)
                : javaMember.getName().substring(3);

        name = StringUtils.uncapitalize(name);

        final Class<?> declaringClass = javaMember.getDeclaringClass();
        final PropertyDescriptor[] properties = ReflectHelper.getProperties(declaringClass);
        for (final PropertyDescriptor descriptor : properties) {
            if (descriptor.getName().equals(name)) {
                return new PropertyAccessor(descriptor);
            }
        }

        Field field;
        try {
            field = javaMember.getDeclaringClass().getDeclaredField(StringUtils.uncapitalize(name));
            return new FieldAccessor(field);
        } catch (final Exception e) {
            throw new BatooException("Cannot find instance variable field "
                    + javaMember.getDeclaringClass().getName() + "." + name);
        }
    }
}

From source file:com.agimatec.validation.jsr303.xml.AnnotationIgnores.java

public void setIgnoreAnnotationsOnMember(Member member) {
    Class<?> beanClass = member.getDeclaringClass();
    if (ignoreAnnotationOnMember.get(beanClass) == null) {
        List<Member> tmpList = new ArrayList<Member>();
        tmpList.add(member);//  ww w.j a v a  2 s  . co m
        ignoreAnnotationOnMember.put(beanClass, tmpList);
    } else {
        ignoreAnnotationOnMember.get(beanClass).add(member);
    }
}

From source file:com.agimatec.validation.jsr303.xml.AnnotationIgnores.java

public boolean isIgnoreAnnotations(Member member) {
    boolean ignoreAnnotation;
    Class<?> clazz = member.getDeclaringClass();
    List<Member> ignoreAnnotationForMembers = ignoreAnnotationOnMember.get(clazz);
    if (ignoreAnnotationForMembers == null || !ignoreAnnotationForMembers.contains(member)) {
        ignoreAnnotation = getDefaultIgnoreAnnotation(clazz);
    } else {/*from w w w  . java2s .  c o  m*/
        ignoreAnnotation = ignoreAnnotationForMembers.contains(member);
    }
    if (ignoreAnnotation) {
        logMessage(member, clazz);
    }
    return ignoreAnnotation;
}

From source file:org.picocontainer.gems.monitors.CommonsLoggingComponentMonitor.java

/**
 * Retrieves the logger appropriate for the calling member's class.
 * @param member constructor/method/field who's callback is required.
 * @return the Commons logging instance.
 *///w  ww.j av a2  s . co m
protected synchronized Log getLog(final Member member) {
    if (log != null) {
        return log;
    }
    return LogFactory.getLog(member.getDeclaringClass());
}

From source file:org.eclipse.swordfish.plugins.cxf.support.NMROutputStream.java

private org.apache.servicemix.nmr.api.Exchange createNMRMessageExchange() throws IOException {
    Member member = (Member) message.get(Method.class.getName());
    Class<?> clz = member.getDeclaringClass();
    Exchange exchange = message.getExchange();
    BindingOperationInfo bop = exchange.get(BindingOperationInfo.class);

    LOG.info("Invoking service" + clz);

    WebService ws = clz.getAnnotation(WebService.class);
    InterfaceInfo info = message.getExchange().get(InterfaceInfo.class);
    QName interfaceName = null;// w w w  . j  a  v a  2  s.c  o  m
    if (ws != null && ws.name() != null && !"".equals(ws.name())) {
        interfaceName = new QName(ws.targetNamespace(), ws.name());
    } else {
        if (info != null) {
            interfaceName = info.getName();
        }
    }
    QName serviceName;
    if (target != null) {
        serviceName = EndpointReferenceUtils.getServiceName(target, conduit.getBus());
    } else {
        serviceName = message.getExchange().get(org.apache.cxf.service.Service.class).getName();
    }

    LOG.info("Create messageExchange" + serviceName);
    org.apache.servicemix.nmr.api.Exchange xchng;
    if (isOneWay) {
        xchng = channel.createExchange(Pattern.InOnly);
    } else if (bop.getOutput() == null) {
        xchng = channel.createExchange(Pattern.RobustInOnly);
    } else {
        xchng = channel.createExchange(Pattern.InOut);
    }
    org.apache.servicemix.nmr.api.Message inMsg = xchng.getIn();
    LOG.info("Exchange endpoint " + serviceName);
    LOG.info("setup message contents on " + inMsg);
    inMsg.setBody(getMessageContent(message));
    /*
          for (Iterator it = message.keySet().iterator(); it.hasNext();) {
             String key = (String) it.next();
             inMsg.setHeader(key, message.get(key));
          }
    */
    LOG.info("service for exchange " + serviceName);

    Map<String, Object> refProps = new HashMap<String, Object>();
    if (interfaceName != null) {
        refProps.put(Endpoint.INTERFACE_NAME, interfaceName.toString());
    }
    refProps.put(Endpoint.SERVICE_NAME, serviceName.toString());
    Reference ref = channel.getNMR().getEndpointRegistry().lookup(refProps);
    xchng.setTarget(ref);
    xchng.setOperation(bop.getName());
    return xchng;
}

From source file:com.picocontainer.gems.monitors.CommonsLoggingComponentMonitor.java

/**
 * Retrieves the logger appropriate for the calling member's class.
 * @param member constructor/method/field who's callback is required.
 * @return the Commons logging instance.
 */// w  ww . j  a  v a  2  s  . c  o  m
protected synchronized Log getLog(final Member member) {
    if (log != null) {
        return log;
    }
    if (member == null) {
        return LogFactory.getLog("");
    }

    return LogFactory.getLog(member.getDeclaringClass());
}

From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java

@SuppressWarnings("unchecked")
@Test/*w w  w .j  a  v  a  2 s.  c om*/
public void publicAbstractMethodStartingWithAdd() {
    memberCriteria.membersOfType(Method.class);
    memberCriteria.withModifiers(Modifier.ABSTRACT);
    memberCriteria.named(Pattern.compile("add.*"));
    memberCriteria.setMemberIterateOrder(ReflectFacade.getMemberNameComparator());
    Collection<Class<?>> iteratedTypes = new ArrayList<Class<?>>(Arrays.asList(List.class, Collection.class));
    ClassCriteria classCriteria = new ClassCriteria();
    Iterable<Class<?>> classIterable = classCriteria.getIterable(ArrayList.class);
    Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable);
    assertTrue(memberIterable.iterator().hasNext());
    for (Member member : memberIterable) {
        assertTrue("member must be a method", member instanceof Method);
        iteratedTypes.remove(member.getDeclaringClass());
        String name = member.getName();
        assertTrue(name.startsWith("add"));
        int modifiers = member.getModifiers();
        assertTrue(Modifier.isAbstract(modifiers));
    }
    assertTrue("Some types have not been iterated. " + iteratedTypes, iteratedTypes.isEmpty());
}

From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java

@Test
public void hierarchyTraveral() {
    memberCriteria.membersOfType(Method.class);
    memberCriteria.withModifiers(Modifier.ABSTRACT);
    memberCriteria.named(Pattern.compile("add.*"));
    Class<?> currentHierarchyClass = ArrayList.class;
    ClassCriteria classCriteria = new ClassCriteria();
    Iterable<Class<?>> classIterable = classCriteria.getIterable(ArrayList.class);
    Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable);
    assertTrue(memberIterable.iterator().hasNext());
    for (Member member : memberIterable) {
        assertTrue("member must be a method", member instanceof Method);
        if (!currentHierarchyClass.equals(member.getDeclaringClass())) {
            if (!member.getDeclaringClass().isInterface()) {
                assertTrue("members must be iterated " + "from subclass to superclass",
                        ((Class<?>) member.getDeclaringClass()).isAssignableFrom(currentHierarchyClass));
                // so the current declaring class is not the members
                // declaring
                // class which means we went up the hierarchy
                currentHierarchyClass = member.getDeclaringClass();
            }/*from  w ww.  j ava 2  s  .  c o  m*/
        }
    }
}