Example usage for java.lang.reflect Modifier isPublic

List of usage examples for java.lang.reflect Modifier isPublic

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isPublic.

Prototype

public static boolean isPublic(int mod) 

Source Link

Document

Return true if the integer argument includes the public modifier, false otherwise.

Usage

From source file:org.jdto.util.MemberUtils.java

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

From source file:com.activecq.api.ActiveProperties.java

/**
 * Uses reflection to create a List of the keys defined in the ActiveField subclass (and its inheritance tree).
 * Only fields that meet the following criteria are returned:
 * - public/*from   ww  w.j  a  v a  2 s  .  c o  m*/
 * - static
 * - final
 * - String
 * - Upper case 
 * @return a List of all the Fields
 */
@SuppressWarnings("unchecked")
public List<String> getKeys() {
    ArrayList<String> keys = new ArrayList<String>();
    ArrayList<String> names = new ArrayList<String>();
    Class cls = this.getClass();

    if (cls == null) {
        return Collections.unmodifiableList(keys);
    }

    Field fieldList[] = cls.getFields();

    for (Field fld : fieldList) {
        int mod = fld.getModifiers();

        // Only look at public static final fields
        if (!Modifier.isPublic(mod) || !Modifier.isStatic(mod) || !Modifier.isFinal(mod)) {
            continue;
        }

        // Only look at String fields
        if (!String.class.equals(fld.getType())) {
            continue;
        }

        // Only look at uppercase fields
        if (!StringUtils.equals(fld.getName().toUpperCase(), fld.getName())) {
            continue;
        }

        // Get the value of the field
        String value;
        try {
            value = StringUtils.stripToNull((String) fld.get(this));
        } catch (IllegalArgumentException e) {
            continue;
        } catch (IllegalAccessException e) {
            continue;
        }

        // Do not add duplicate or null keys, or previously added named fields
        if (value == null || names.contains(fld.getName()) || keys.contains(value)) {
            continue;
        }

        // Success! Add key to key list
        keys.add(value);

        // Add field named to process field names list
        names.add(fld.getName());

    }

    return Collections.unmodifiableList(keys);
}

From source file:com.hihframework.core.utils.BeanUtils.java

/**
 * java bean??Map/*w  w w .  j  ava 2s.  co  m*/
 * @param bean
 * @param map
 * @author 
 * @since 
 */
public static void beanToMap(Object bean, Map<String, Object> map) {
    if (map == null || bean == null)
        return;
    Method[] methods = bean.getClass().getDeclaredMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        if (!name.startsWith("get") && !name.startsWith("is"))
            continue;
        int position = name.startsWith("get") ? 3 : 2;
        String n = StringHelpers.lowerFirst(name.substring(position));
        try {
            Object val = m.invoke(bean);
            map.put(n, val);
        } catch (Exception e) {
        }
    }
}

From source file:com.taobao.tdhs.client.protocol.TDHSProtocolBinary.java

protected static void encodeRequest(Request o, ByteArrayOutputStream out, String charsetName)
        throws IllegalAccessException, IOException, TDHSEncodeException {
    for (Field f : o.getClass().getDeclaredFields()) {
        if (!Modifier.isPublic(f.getModifiers())) {
            f.setAccessible(true);// w  w w  .  ja  v a  2s  . com
        }
        Object v = f.get(o);
        if (v instanceof Request) {
            encodeRequest((Request) v, out, charsetName);
        } else if (f.getName().startsWith("_")) {
            writeObjectToStream(v, out, f.getName(), charsetName);
        }
    }
}

From source file:com.diversityarrays.kdxplore.stats.StatsUtil.java

static public void initCheck() {
    if (checkDone) {
        return;/*  w  w w  .j a v a 2s  .c o  m*/
    }
    Set<String> okIfExhibitMissing = new HashSet<>();
    Collections.addAll(okIfExhibitMissing, "getFormat", "getValueClass", "getStatsName", "getLowOutliers",
            "getHighOutliers");
    List<String> errors = new ArrayList<String>();

    Set<String> unMatchedStatNameValues = new HashSet<String>();
    for (SimpleStatistics.StatName sname : StatName.values()) {
        unMatchedStatNameValues.add(sname.displayName);
    }

    for (Method m : SimpleStatistics.class.getDeclaredMethods()) {

        if (!Modifier.isPublic(m.getModifiers())) {
            continue; // shouldn't happen!
        }

        ExhibitColumn ec = m.getAnnotation(ExhibitColumn.class);

        if (ec == null) {
            if (okIfExhibitMissing.contains(m.getName())) {
                //               if ("getQuartiles".equals(m.getName())) {
                //                  System.err.println("%TODO: @ExhibitColumn for 'SimpleStatistics.getQuartiles()'");
                //               }
            } else {
                errors.add("Missing @ExhibitColumn: " + m.getName());
            }
        } else {
            String ecValue = ec.value();
            boolean found = false;
            for (SimpleStatistics.StatName sname : StatName.values()) {
                if (sname.displayName.equals(ecValue)) {
                    unMatchedStatNameValues.remove(sname.displayName);
                    METHOD_BY_STATNAME.put(sname, m);
                    found = true;
                    break;
                }
            }
            if (!found) {
                errors.add("Doesn't match any StatName: '" + ecValue + "', method=" + m.getName());
            }
        }
    }

    if (!unMatchedStatNameValues.isEmpty()) {
        errors.add(StringUtil.join("Unmatched StatName values: ", " ", unMatchedStatNameValues));
    }

    if (!errors.isEmpty()) {
        throw new RuntimeException(StringUtil.join("Problems in SimpleStatistics config: ", "\n", errors));
    }

    checkDone = true;
}

From source file:fi.luontola.cqrshotel.JsonSerializationTest.java

@Test
public void all_messages_are_serializable() throws Exception {
    new Reflections("fi.luontola.cqrshotel").getSubTypesOf(Message.class).stream()
            .filter(type -> !type.isInterface()).filter(type -> Modifier.isPublic(type.getModifiers()))
            .forEach(type -> assertSerializable(type, objectMapper));
}

From source file:edu.northwestern.bioinformatics.studycalendar.xml.writers.ChangeXmlSerializerFactoryTest.java

@SuppressWarnings({ "RawUseOfParameterizedType" })
public void testASerializerCanBeBuiltForEveryChange() throws Exception {
    List<String> errors = new ArrayList<String>();
    for (Class<? extends Change> aClass : domainReflections.getSubTypesOf(Change.class)) {
        if (Modifier.isPublic(aClass.getModifiers()) && !Modifier.isAbstract(aClass.getModifiers())
                && !aClass.isAnonymousClass()) {
            Change c = aClass.newInstance();
            try {
                factory.createXmlSerializer(c);
            } catch (StudyCalendarError sce) {
                errors.add(String.format("Failure with %s: %s", c, sce.getMessage()));
            }//from www  .j av  a2 s  .co  m
        }
    }
    if (!errors.isEmpty()) {
        fail(StringUtils.join(errors.iterator(), '\n'));
    }
}

From source file:com.sjsu.crawler.util.RandomStringUtilsTest.java

@Test
public void testConstructor() {
    assertNotNull(new RandomStringUtils());
    final Constructor<?>[] cons = RandomStringUtils.class.getDeclaredConstructors();
    assertEquals(1, cons.length);/*  w ww .ja  va2s .  c o m*/
    assertTrue(Modifier.isPublic(cons[0].getModifiers()));
    assertTrue(Modifier.isPublic(RandomStringUtils.class.getModifiers()));
    assertFalse(Modifier.isFinal(RandomStringUtils.class.getModifiers()));
}

From source file:org.grails.orm.hibernate.cfg.AbstractIdentityEnumType.java

@SuppressWarnings("unchecked")
public static boolean supports(@SuppressWarnings("rawtypes") Class enumClass) {
    if (enumClass.isEnum()) {
        try {/*from w  ww.  j av  a  2 s  .  c  om*/
            Method idAccessor = enumClass.getMethod(ENUM_ID_ACCESSOR);
            int mods = idAccessor.getModifiers();
            if (Modifier.isPublic(mods) && !Modifier.isStatic(mods)) {
                Class<?> returnType = idAccessor.getReturnType();
                return returnType != null
                        && typeResolver.basic(returnType.getName()) instanceof AbstractStandardBasicType;
            }
        } catch (NoSuchMethodException e) {
            // ignore
        }
    }
    return false;
}

From source file:org.jdto.util.MethodUtils.java

/**
 * <p>Return an accessible method (that is, one that can be invoked via
 * reflection) that implements the specified Method. If no such method can
 * be found, return/*from w  w  w  . ja  va  2  s . c  o  m*/
 * <code>null</code>.</p>
 *
 * @param method The method that we wish to call
 * @return The accessible method
 */
public static Method getAccessibleMethod(Method method) {

    if (!MemberUtils.isAccessible(method)) {
        return null;
    }
    // If the declaring class is public, we are done
    Class cls = method.getDeclaringClass();
    if (Modifier.isPublic(cls.getModifiers())) {
        return method;
    }
    String methodName = method.getName();
    Class[] parameterTypes = method.getParameterTypes();

    // Check the implemented interfaces and subinterfaces
    method = getAccessibleMethodFromInterfaceNest(cls, methodName, parameterTypes);

    // Check the superclass chain
    if (method == null) {
        method = getAccessibleMethodFromSuperclass(cls, methodName, parameterTypes);
    }
    return method;
}