Example usage for java.lang.reflect Member getName

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

Introduction

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

Prototype

public String getName();

Source Link

Document

Returns the simple name of the underlying member or constructor represented by this Member.

Usage

From source file:com.helpinput.core.Utils.java

@SuppressWarnings("rawtypes")
public static Member findMember(Member[] methods, Class<?> targetClass, String methodName,
        Class<?>[] argsClasses) {
    for (Member member : methods) {
        if (!hasLength(methodName) || member.getName().equals(methodName)) {

            if (!hasLength(argsClasses))
                return setAccess(member);

            Class<?>[] paramTypes;

            if (member instanceof Method)
                paramTypes = ((Method) member).getParameterTypes();
            else/*from   w w  w.ja v  a  2s.  c  o m*/
                paramTypes = ((Constructor) member).getParameterTypes();

            if (!hasLength(paramTypes) && !hasLength(argsClasses))
                return setAccess(member);

            if (argsClasses.length <= paramTypes.length) {
                for (int i = 0; i < argsClasses.length; i++) {
                    if (argsClasses[i] == null)
                        continue;

                    if (paramTypes[i].isPrimitive()) {
                        if (!primitiveWrap(paramTypes[i]).isAssignableFrom(argsClasses[i]))
                            break;
                    } else if (!paramTypes[i].isAssignableFrom(argsClasses[i]))
                        break;

                    if (i == argsClasses.length - 1) {
                        return setAccess(member);
                    }
                }
            }
        }
    }
    Class<?> superClass;
    if ((superClass = targetClass.getSuperclass()) != null) {
        return findMember(superClass.getDeclaredMethods(), superClass, methodName, argsClasses);
    }
    return null;
}

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

private void logMessage(Member member, Class<?> clazz) {
    String type;//from  ww w. j  a v a2s . c  o  m
    if (member instanceof Field) {
        type = "Field";
    } else {
        type = "Property";
    }
    log.debug(type + " level annotations are getting ignored for " + clazz.getName() + "." + member.getName());
}

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

@SuppressWarnings("unchecked")
@Test// w w  w  .j a  va2s  .c  o m
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.ReflectFacade.java

/**
 * {@inheritDoc}// www  . j a v  a2  s .c  om
 *
 * @see MemberNamePatternPredicate class javadoc
 * @since 1.0.0.0
 */
public boolean evaluate(Object object) {
    if (!Member.class.isInstance(object)) {
        throw new IllegalArgumentException(
                MemberNamePatternPredicate.class.getSimpleName() + " can only handle " + Member.class);
    }
    Member member = (Member) object;
    String memberName = member.getName();
    Matcher matcher = getMatcher(memberName);
    return matcher.matches();
}

From source file:com.link_intersystems.lang.reflect.ReflectFacade.java

/**
 * {@inheritDoc}//  w  ww. ja  v a2 s. c o  m
 *
 * @see MemberNameComparator class javadoc
 * @since 1.0.0.0
 */
public int compare(Member o1, Member o2) {
    int compareValue = 0;
    if (o1 == null) {
        if (o2 == null) {
            compareValue = 0;
        } else {
            compareValue = -1;
        }
    } else {
        if (o2 == null) {
            compareValue = 1;
        } else {
            String name1 = o1.getName();
            String name2 = o2.getName();
            compareValue = name1.compareTo(name2);
        }
    }
    return compareValue;
}

From source file:mondrian.olap.Util.java

/**
 * Finds a root member of a hierarchy with a given name.
 *
 * @param hierarchy Hierarchy/*from  w  w w. j  a va  2  s .  co  m*/
 * @param memberName Name of root member
 * @return Member, or null if not found
 */
public static Member lookupHierarchyRootMember(SchemaReader reader, Hierarchy hierarchy,
        Id.NameSegment memberName, MatchType matchType) {
    // Lookup member at first level.
    //
    // Don't use access control. Suppose we cannot see the 'nation' level,
    // we still want to be able to resolve '[Customer].[USA].[CA]'.
    List<Member> rootMembers = reader.getHierarchyRootMembers(hierarchy);

    // if doing an inexact search on a non-all hierarchy, create
    // a member corresponding to the name we're searching for so
    // we can use it in a hierarchical search
    Member searchMember = null;
    if (!matchType.isExact() && !hierarchy.hasAll() && !rootMembers.isEmpty()) {
        searchMember = hierarchy.createMember(null, rootMembers.get(0).getLevel(), memberName.name, null);
    }

    int bestMatch = -1;
    int k = -1;
    for (Member rootMember : rootMembers) {
        ++k;
        int rc;
        // when searching on the ALL hierarchy, match must be exact
        if (matchType.isExact() || hierarchy.hasAll()) {
            rc = rootMember.getName().compareToIgnoreCase(memberName.name);
        } else {
            rc = FunUtil.compareSiblingMembers(rootMember, searchMember);
        }
        if (rc == 0) {
            return rootMember;
        }
        if (!hierarchy.hasAll()) {
            if (matchType == MatchType.BEFORE) {
                if (rc < 0 && (bestMatch == -1
                        || FunUtil.compareSiblingMembers(rootMember, rootMembers.get(bestMatch)) > 0)) {
                    bestMatch = k;
                }
            } else if (matchType == MatchType.AFTER) {
                if (rc > 0 && (bestMatch == -1
                        || FunUtil.compareSiblingMembers(rootMember, rootMembers.get(bestMatch)) < 0)) {
                    bestMatch = k;
                }
            }
        }
    }

    if (matchType == MatchType.EXACT_SCHEMA) {
        return null;
    }

    if (matchType != MatchType.EXACT && bestMatch != -1) {
        return rootMembers.get(bestMatch);
    }
    // If the first level is 'all', lookup member at second level. For
    // example, they could say '[USA]' instead of '[(All
    // Customers)].[USA]'.
    return (rootMembers.size() > 0 && rootMembers.get(0).isAll())
            ? reader.lookupMemberChildByName(rootMembers.get(0), memberName, matchType)
            : null;
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceXMLMetaDataParser.java

private void populateFromReflection(Class cls, XMLMetaData meta) {
    Member[] members;//  w ww  . j a v a 2  s.  co  m

    Class superclass = cls.getSuperclass();

    // handle inheritance at sub-element level
    if ((AccessController.doPrivileged(J2DoPrivHelper.isAnnotationPresentAction(superclass, xmlTypeClass)))
            .booleanValue())
        populateFromReflection(superclass, meta);

    try {
        if (StringUtils.equals(
                xmlAccessorValue.invoke(cls.getAnnotation(xmlAccessorTypeClass), new Object[] {}).toString(),
                "FIELD"))
            members = cls.getDeclaredFields();
        else
            members = cls.getDeclaredMethods();

        for (int i = 0; i < members.length; i++) {
            Member member = members[i];
            AnnotatedElement el = (AnnotatedElement) member;
            XMLMetaData field = null;
            if (el.getAnnotation(xmlElementClass) != null) {
                String xmlname = (String) xmlElementName.invoke(el.getAnnotation(xmlElementClass),
                        new Object[] {});
                // avoid JAXB XML bind default name
                if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
                    xmlname = member.getName();
                if ((AccessController.doPrivileged(
                        J2DoPrivHelper.isAnnotationPresentAction(((Field) member).getType(), xmlTypeClass)))
                                .booleanValue()) {
                    field = _repos.addXMLClassMetaData(((Field) member).getType());
                    parseXmlRootElement(((Field) member).getType(), field);
                    populateFromReflection(((Field) member).getType(), field);
                    field.setXmltype(XMLMetaData.XMLTYPE);
                    field.setXmlname(xmlname);
                } else {
                    field = _repos.newXMLFieldMetaData(((Field) member).getType(), member.getName());
                    field.setXmltype(XMLMetaData.ELEMENT);
                    field.setXmlname(xmlname);
                    field.setXmlnamespace((String) xmlElementNamespace.invoke(el.getAnnotation(xmlElementClass),
                            new Object[] {}));
                }
            } else if (el.getAnnotation(xmlAttributeClass) != null) {
                field = _repos.newXMLFieldMetaData(((Field) member).getType(), member.getName());
                field.setXmltype(XMLFieldMetaData.ATTRIBUTE);
                String xmlname = (String) xmlAttributeName.invoke(el.getAnnotation(xmlAttributeClass),
                        new Object[] {});
                // avoid JAXB XML bind default name
                if (StringUtils.equals(XMLMetaData.defaultName, xmlname))
                    xmlname = member.getName();
                field.setXmlname("@" + xmlname);
                field.setXmlnamespace((String) xmlAttributeNamespace.invoke(el.getAnnotation(xmlAttributeClass),
                        new Object[] {}));
            }
            if (field != null)
                meta.addField(member.getName(), field);
        }
    } catch (Exception e) {
    }
}

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

/**
 * Returns the qualified name for the member.
 * // ww w.ja  va  2  s .c om
 * @param member
 *            the member to qualify
 * @return the qualified name for the member
 * 
 * @since 2.0.1
 */
public static String createMemberName(Member member) {
    return member.getDeclaringClass().getName() + "." + member.getName();
}

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

/**
 * Returns the accessor for the member/*from w  w  w. ja v  a  2s .  c  o m*/
 * 
 * @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:org.batoo.jpa.common.reflect.ReflectHelper.java

/**
 * Returns the accessor for the member//from  www  .  j  ava 2s.  c  om
 * 
 * @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);
        }
    }
}