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:com.xhsoft.framework.common.utils.ReflectUtil.java

/**
 * <p>Description:setFieldValue</p>
 * @param target/*from w w  w  .j av  a2 s. c o m*/
 * @param fname
 * @param ftype
 * @param fvalue
 * @return void
 */
@SuppressWarnings("unchecked")
public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) {
    if (target == null || fname == null || "".equals(fname)
            || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass()))) {
        return;
    }

    Class clazz = target.getClass();

    try {
        Method method = clazz
                .getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype);

        if (!Modifier.isPublic(method.getModifiers())) {
            method.setAccessible(true);
        }

        method.invoke(target, fvalue);

    } catch (Exception me) {
        try {
            Field field = clazz.getDeclaredField(fname);

            if (!Modifier.isPublic(field.getModifiers())) {
                field.setAccessible(true);
            }

            field.set(target, fvalue);
        } catch (Exception fe) {

            if (logger.isDebugEnabled()) {
                logger.debug(fe);
            }
        }
    }
}

From source file:Main.java

/**
 * set constructor is accessible/*from   w  w  w .  j a v  a2  s. c  om*/
 * @param constructor {@link java.lang.reflect.Constructor}
 * @param <T> t
 */
public static <T> void makeAccessible(final Constructor<T> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }
}

From source file:ModifierUtil.java

public static boolean isPublic(Member member) {
    return Modifier.isPublic(member.getModifiers());
}

From source file:ModifierUtil.java

public static boolean isPublic(Class<?> clazz) {
    return Modifier.isPublic(clazz.getModifiers());
}

From source file:JarClassLoader.java

public void invokeClass(String name, String[] args)
        throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
    Class c = loadClass(name);//from  w ww.ja v  a  2  s  .  co  m
    Method m = c.getMethod("main", new Class[] { args.getClass() });
    m.setAccessible(true);
    int mods = m.getModifiers();
    if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
        throw new NoSuchMethodException("main");
    }
    try {
        m.invoke(null, new Object[] { args });
    } catch (IllegalAccessException e) {

    }
}

From source file:Main.java

public static List<Field> getAccessibleFields(Class<?> clazz, Class<?> limit) {
    Package topPackage = clazz.getPackage();
    List<Field> fieldList = new ArrayList<Field>();
    int topPackageHash = topPackage == null ? 0 : topPackage.hashCode();
    boolean top = true;
    do {//from ww  w. ja v a2 s.  c  o m
        if (clazz == null) {
            break;
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            if (top == true) { // add all top declared fields
                fieldList.add(field);
                continue;
            }
            int modifier = field.getModifiers();
            if (Modifier.isPrivate(modifier) == true) {
                continue; // ignore super private fields
            }
            if (Modifier.isPublic(modifier) == true) {
                addFieldIfNotExist(fieldList, field); // add super public methods
                continue;
            }
            if (Modifier.isProtected(modifier) == true) {
                addFieldIfNotExist(fieldList, field); // add super protected methods
                continue;
            }
            // add super default methods from the same package
            Package pckg = field.getDeclaringClass().getPackage();
            int pckgHash = pckg == null ? 0 : pckg.hashCode();
            if (pckgHash == topPackageHash) {
                addFieldIfNotExist(fieldList, field);
            }
        }
        top = false;
    } while ((clazz = clazz.getSuperclass()) != limit);

    return fieldList;
}

From source file:net.servicestack.client.Utils.java

public static Field[] getSerializableFields(Class type) {
    List<Field> fields = new ArrayList<Field>();
    for (Class<?> c = type; c != null; c = c.getSuperclass()) {
        if (c == Object.class)
            break;

        for (Field f : c.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()))
                continue;
            if (!Modifier.isPublic(f.getModifiers()))
                continue;

            fields.add(f);/*from w w  w .  ja v  a 2s .  c o m*/
        }
    }
    return fields.toArray(new Field[fields.size()]);
}

From source file:Main.java

/**
 * Converts a color into a string. If the color is equal to one of the defined
 * constant colors, that name is returned instead. Otherwise the color is
 * returned as hex-string.//w  w w  .jav a  2  s.  c o  m
 * 
 * @param c
 *          the color.
 * @return the string for this color.
 */
public static String colorToString(final Color c) {
    try {
        final Field[] fields = Color.class.getFields();
        for (int i = 0; i < fields.length; i++) {
            final Field f = fields[i];
            if (Modifier.isPublic(f.getModifiers()) && Modifier.isFinal(f.getModifiers())
                    && Modifier.isStatic(f.getModifiers())) {
                final String name = f.getName();
                final Object oColor = f.get(null);
                if (oColor instanceof Color) {
                    if (c.equals(oColor)) {
                        return name;
                    }
                }
            }
        }
    } catch (Exception e) {
        //
    }

    // no defined constant color, so this must be a user defined color
    final String color = Integer.toHexString(c.getRGB() & 0x00ffffff);
    final StringBuffer retval = new StringBuffer(7);
    retval.append("#");

    final int fillUp = 6 - color.length();
    for (int i = 0; i < fillUp; i++) {
        retval.append("0");
    }

    retval.append(color);
    return retval.toString();
}

From source file:IntrospectionUtil.java

public static boolean isInheritable(Package pack, Member member) {
    if (pack == null)
        return false;
    if (member == null)
        return false;

    int modifiers = member.getModifiers();
    if (Modifier.isPublic(modifiers))
        return true;
    if (Modifier.isProtected(modifiers))
        return true;
    if (!Modifier.isPrivate(modifiers) && pack.equals(member.getDeclaringClass().getPackage()))
        return true;

    return false;
}

From source file:org.eclipse.gemini.blueprint.compendium.internal.cm.UpdateMethodAdapter.java

/**
 * Determines the update method./*from   w w  w  .  j a v  a  2 s  .  c  om*/
 * 
 * @param target
 * @param methodName
 * @return
 */
static Map determineUpdateMethod(final Class<?> target, final String methodName) {
    Assert.notNull(target);
    Assert.notNull(methodName);

    final Map methods = new LinkedHashMap(2);
    final boolean trace = log.isTraceEnabled();

    org.springframework.util.ReflectionUtils.doWithMethods(target,
            new org.springframework.util.ReflectionUtils.MethodCallback() {

                public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                    // consider filtering bridge non-void and non-public methods as well
                    if (!method.isBridge() && Modifier.isPublic(method.getModifiers())
                            && (void.class.equals(method.getReturnType()))
                            && methodName.equals(method.getName())) {
                        // check the argument types
                        Class<?>[] args = method.getParameterTypes();

                        // Properties can be passed as Map or Dictionary
                        if (args != null && args.length == 1) {
                            Class<?> propertiesType = args[0];
                            if (propertiesType.isAssignableFrom(Map.class)
                                    || propertiesType.isAssignableFrom(Dictionary.class)) {

                                if (trace)
                                    log.trace("Discovered custom method [" + method.toString() + "] on "
                                            + target);
                                // see if there was a method already found
                                Method m = (Method) methods.get(propertiesType);

                                if (m != null) {
                                    if (trace)
                                        log.trace(
                                                "Type " + propertiesType + " already has an associated method ["
                                                        + m.toString() + "];ignoring " + method);
                                } else
                                    methods.put(propertiesType, method);
                            }
                        }
                    }
                }
            });

    return methods;
}