Example usage for java.lang.reflect Member getModifiers

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

Introduction

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

Prototype

public int getModifiers();

Source Link

Document

Returns the Java language modifiers for the member or constructor represented by this Member, as an integer.

Usage

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

@Test
public void protecedConstructors() {
    memberCriteria.membersOfType(Constructor.class);
    memberCriteria.withAccess(AccessType.PROTECTED);
    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 constructor", member instanceof Constructor<?>);
        int modifiers = member.getModifiers();
        assertFalse(Modifier.isPublic(modifiers));
        assertTrue(Modifier.isProtected(modifiers));
        assertFalse(Modifier.isPrivate(modifiers));
    }//  ww w  .ja v a  2s . c o  m
}

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

@SuppressWarnings("unchecked")
@Test/* w  w  w.  ja  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:org.apache.openjpa.persistence.PersistenceMetaDataDefaults.java

protected boolean isDefaultPersistent(ClassMetaData meta, Member member, String name, boolean ignoreTransient) {
    int mods = member.getModifiers();
    if (Modifier.isTransient(mods))
        return false;
    int access = meta.getAccessType();

    if (member instanceof Field) {
        // If mixed or unknown, default property access, keep explicit 
        // field members
        if (AccessCode.isProperty(access)) {
            if (!isAnnotatedAccess(member, AccessType.FIELD))
                return false;
        }// w  ww  .  j  av a2s.  c  om
    } else if (member instanceof Method) {
        // If mixed or unknown, field default access, keep explicit property
        // members
        if (AccessCode.isField(access)) {
            if (!isAnnotatedAccess(member, AccessType.PROPERTY))
                return false;
        }
        try {
            // check for setters for methods
            Method setter = (Method) AccessController.doPrivileged(J2DoPrivHelper.getDeclaredMethodAction(
                    meta.getDescribedType(), "set" + StringUtils.capitalize(name),
                    new Class[] { ((Method) member).getReturnType() }));
            if (setter == null && !isAnnotatedTransient(member)) {
                logNoSetter(meta, name, null);
                return false;
            }
        } catch (Exception e) {
            // e.g., NoSuchMethodException
            if (!isAnnotatedTransient(member))
                logNoSetter(meta, name, e);
            return false;
        }
    }

    PersistenceStrategy strat = getPersistenceStrategy(null, member, ignoreTransient);
    if (strat == null) {
        warn(meta, _loc.get("no-pers-strat", name));
        return false;
    } else if (strat == PersistenceStrategy.TRANSIENT) {
        return false;
    } else {
        return true;
    }
}

From source file:jp.co.acroquest.jsonic.JSON.java

/**
 * Ignore this property. A default behavior is to ignore transient or declaring method in java.lang.Object.
 * You can override this method if you have to change default behavior.
 * /*  w  ww.  j  a  v  a  2  s.c o  m*/
 * @param context current context
 * @param target target class
 * @param member target member
 * @return true if this member must be ignored.
 */
protected boolean ignore(Context context, Class<?> target, Member member) {
    if (Modifier.isTransient(member.getModifiers()))
        return true;
    if (member.getDeclaringClass().equals(Object.class))
        return true;
    return false;
}

From source file:org.apache.openjpa.meta.FieldMetaData.java

/**
 * Supply the backing member object; this allows us to utilize
 * parameterized type information if available.
 * Sets the access style of this receiver based on whether the given 
 * member represents a field or getter method.
 *///from  ww  w.  j  a  v a  2  s .  com
public void backingMember(Member member) {
    if (member == null)
        return;
    if (Modifier.isTransient(member.getModifiers()))
        _transient = true;

    _backingMember = new MemberProvider(member);

    Class<?> type;
    Class<?>[] types;
    if (member instanceof Field) {
        Field f = (Field) member;
        type = f.getType();
        types = JavaVersions.getParameterizedTypes(f);
        setAccessType(AccessCode.FIELD);
    } else {
        Method meth = (Method) member;
        type = meth.getReturnType();
        types = JavaVersions.getParameterizedTypes(meth);
        setAccessType(AccessCode.PROPERTY);
    }

    setDeclaredType(type);
    if (Collection.class.isAssignableFrom(type) && _elem.getDeclaredType() == Object.class
            && types.length == 1) {
        _elem.setDeclaredType(types[0]);
    } else if (Map.class.isAssignableFrom(type) && types.length == 2) {
        if (_key.getDeclaredType() == Object.class)
            _key.setDeclaredType(types[0]);
        if (_elem.getDeclaredType() == Object.class)
            _elem.setDeclaredType(types[1]);
    }
}

From source file:com.parse.ParseObject.java

private static boolean isAccessible(Member m) {
    return Modifier.isPublic(m.getModifiers())
            || (m.getDeclaringClass().getPackage().getName().equals("com.parse")
                    && !Modifier.isPrivate(m.getModifiers()) && !Modifier.isProtected(m.getModifiers()));
}

From source file:org.regenstrief.util.Util.java

/**
 * Writes information about a .jar/*from   w ww  . j a  v  a2 s .com*/
 * 
 * @param path the path of the .jar
 * @param w the Writer
 * @throws Exception if an I/O problem occurs
 **/
public final static void jarInfo(final String path, final Writer w) throws Exception {
    final JarFile jar = new JarFile(new File(path));
    final Enumeration<JarEntry> en = jar.entries();
    final PrintWriter pw = getPrintWriter(w);

    while (en.hasMoreElements()) {
        final String name = en.nextElement().getName();
        if (!name.endsWith(".class")) {
            continue;
        }
        try {
            Class<?> c = Class.forName(name.substring(0, name.length() - 6).replace('/', '.'));
            final Member[][] allMembers = new Member[][] { c.getFields(), c.getConstructors(), c.getMethods() };
            pw.print(c);
            for (c = c.getSuperclass(); c != null; c = c.getSuperclass()) {
                pw.print(" extends ");
                pw.print(c.getName());
            }
            pw.println();
            pw.println('{');
            for (int j = 0; j < allMembers.length; j++) {
                final Member[] members = allMembers[j];
                for (final Member member : members) {
                    if (!Modifier.isPublic(member.getModifiers())) {
                        continue;
                    }
                    pw.print('\t');
                    pw.print(member);
                    pw.println(';');
                }
                if ((members.length > 0) && (j < allMembers.length - 1)) {
                    pw.println();
                }
            }
            pw.println('}');
        } catch (final Throwable e) {
            pw.println(e);
        }
        pw.println();
    }

    pw.flush();
    jar.close();
}

From source file:com.clark.func.Functions.java

/**
 * Check a Member for basic accessibility.
 * /*from ww w. j  ava 2s  . c o m*/
 * @param m
 *            Member to check
 * @return true if <code>m</code> is accessible
 */
static boolean isAccessible(Member m) {
    return m != null && Modifier.isPublic(m.getModifiers()) && !m.isSynthetic();
}

From source file:com.clark.func.Functions.java

/**
 * XXX Default access superclass workaround
 * /*from   w  w w .  ja va  2  s .  c  om*/
 * When a public class has a default access superclass with public members,
 * these members are accessible. Calling them from compiled code works fine.
 * Unfortunately, on some JVMs, using reflection to invoke these members
 * seems to (wrongly) to prevent access even when the modifer is public.
 * Calling setAccessible(true) solves the problem but will only work from
 * sufficiently privileged code. Better workarounds would be gratefully
 * accepted.
 * 
 * @param o
 *            the AccessibleObject to set as accessible
 */
static void setAccessibleWorkaround(AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) {
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}