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:Util.java

/**
 * Returns true if a Member is accessible via reflection under normal Java access
 * controls./*from  w ww . j  ava2  s .co m*/
 * 
 * @since 1.2
 */
public static boolean isAccessible(Member member) {
    return Modifier.isPublic(member.getModifiers())
            && Modifier.isPublic(member.getDeclaringClass().getModifiers());
}

From source file:edu.brown.utils.ClassUtil.java

/**
 * @param clazz// w  w w .j  av a  2  s .  c  om
 * @return
 */
public static <T> Field[] getFieldsByType(Class<?> clazz, Class<? extends T> fieldType) {
    List<Field> fields = new ArrayList<Field>();
    for (Field f : clazz.getDeclaredFields()) {
        int modifiers = f.getModifiers();
        if (Modifier.isTransient(modifiers) == false && Modifier.isPublic(modifiers) == true
                && Modifier.isStatic(modifiers) == false
                && ClassUtil.getSuperClasses(f.getType()).contains(fieldType)) {

            fields.add(f);
        }
    } // FOR
    return (fields.toArray(new Field[fields.size()]));
}

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

public static void bean2Bean(Object src, Object dest, String... excludes) {
    if (src == null || dest == null || src == dest)
        return;//ww  w  . ja  v  a 2  s .c o  m
    Method[] methods = src.getClass().getDeclaredMethods();
    for (Method m : methods) {
        String name = m.getName();
        if (!Modifier.isPublic(m.getModifiers()))
            continue;
        if (!name.startsWith("get") && !name.startsWith("is"))
            continue;
        if (Collection.class.isAssignableFrom(m.getReturnType()))
            continue;
        boolean exc = false;
        for (String exclude : excludes) {
            if (name.equalsIgnoreCase(exclude)) {
                exc = true;
                break;
            }
        }
        if (exc)
            continue;
        int position = name.startsWith("get") ? 3 : 2;
        String method = name.substring(position);
        try {
            Object val = m.invoke(src);
            if (val == null)
                continue;
            Method targetFun = dest.getClass().getMethod("set" + method, m.getReturnType());
            targetFun.invoke(dest, val);
        } catch (Exception e) {
            log.error(e);
        }

    }
}

From source file:org.evosuite.setup.TestClusterUtils.java

public static void makeAccessible(Method method) {
    if (!Modifier.isPublic(method.getModifiers())
            || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
        method.setAccessible(true);/* w  w  w  . j  a  va2  s.  com*/
    }
}

From source file:org.eclipse.gemini.blueprint.config.internal.adapter.CustomListenerAdapterUtils.java

private static Map<Class<?>, List<Method>> doDetermineCustomMethods(final Class<?> target,
        final String methodName, final Class<?>[] possibleArgumentTypes, final boolean onlyPublic) {
    final Map<Class<?>, List<Method>> methods = new LinkedHashMap<Class<?>, List<Method>>(3);

    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 {
                    if (!method.isBridge() && methodName.equals(method.getName())) {
                        if (onlyPublic && !Modifier.isPublic(method.getModifiers())) {
                            if (trace)
                                log.trace("Only public methods are considered; ignoring " + method);
                            return;
                        }//  w w  w .  ja  v  a 2  s .  c o  m
                        // take a look at the variables
                        Class<?>[] args = method.getParameterTypes();

                        if (args != null) {
                            // Properties can be ignored
                            if (args.length == 1) {
                                addMethod(args[0], method, methods);
                            }
                            // or passed as Map, Dictionary
                            else if (args.length == 2) {
                                Class<?> propType = args[1];

                                for (int i = 0; i < possibleArgumentTypes.length; i++) {
                                    Class<?> clazz = possibleArgumentTypes[i];
                                    if (clazz.isAssignableFrom(propType)) {
                                        addMethod(args[0], method, methods);
                                    }
                                }
                            }
                        }
                    }
                }

                private void addMethod(Class<?> key, Method mt, Map<Class<?>, List<Method>> methods) {

                    if (trace)
                        log.trace("discovered custom method [" + mt.toString() + "] on " + target);

                    List<Method> mts = methods.get(key);
                    if (mts == null) {
                        mts = new ArrayList<Method>(2);
                        methods.put(key, mts);
                        org.springframework.util.ReflectionUtils.makeAccessible(mt);
                        mts.add(mt);
                        return;
                    }
                    // add a method only if there is still space
                    if (mts.size() == 1) {
                        Method m = mts.get(0);
                        if (m.getParameterTypes().length == mt.getParameterTypes().length) {
                            if (trace)
                                log.trace("Method w/ signature " + methodSignature(m)
                                        + " has been already discovered; ignoring it");
                        } else {
                            org.springframework.util.ReflectionUtils.makeAccessible(mt);
                            mts.add(mt);
                        }
                    }
                }

                private String methodSignature(Method m) {
                    StringBuilder sb = new StringBuilder();
                    int mod = m.getModifiers();
                    if (mod != 0) {
                        sb.append(Modifier.toString(mod) + " ");
                    }
                    sb.append(m.getReturnType() + " ");
                    sb.append(m.getName() + "(");
                    Class<?>[] params = m.getParameterTypes();
                    for (int j = 0; j < params.length; j++) {
                        sb.append(params[j]);
                        if (j < (params.length - 1))
                            sb.append(",");
                    }
                    sb.append(")");
                    return sb.toString();
                }
            });

    return methods;
}

From source file:ReflectionUtils.java

/**
 * ,DeclaredField./*from ww w  .j  av a2  s.c  om*/
 */
protected static void makeAccessible(final Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
        field.setAccessible(true);
    }
}

From source file:com.judoscript.jamaica.parser.JamaicaDumpVisitor.java

public Object visit(ASTDefaultCtor node, Object data) throws Exception {
    out.print("\n  %default_constructor");
    int flags = node.getAccessFlags();
    if (Modifier.isPublic(flags))
        out.print(" <public>");
    else if (Modifier.isProtected(flags))
        out.print(" <protected>");
    else if (Modifier.isPrivate(flags))
        out.print(" <private>");
    out.println();/*from  w  w w.j a v  a  2 s  . c om*/
    return null;
}

From source file:cn.lambdalib.s11n.SerializationHelper.java

private List<Field> buildExposedFields(Class<?> type) {
    return FieldUtils.getAllFieldsList(type).stream().filter(f -> {
        Class<?> declaringClass = f.getDeclaringClass();
        SerializeStrategy anno = declaringClass.getAnnotation(SerializeStrategy.class);
        ExposeStrategy strategy = anno == null ? ExposeStrategy.PUBLIC : anno.strategy();
        boolean serializeAll = anno == null ? false : anno.all();

        if (f.isAnnotationPresent(SerializeIncluded.class)) {
            return true;
        } else if (f.isAnnotationPresent(SerializeExcluded.class)) {
            return false;
        } else {//from   w ww  .  j  a v a 2  s .  com
            if (!serializeAll && !isS11nType(f.getType())) {
                return false;
            } else {
                int mod = f.getModifiers();
                switch (strategy) {
                case PUBLIC:
                    return Modifier.isPublic(mod) && !Modifier.isStatic(mod) && !Modifier.isFinal(mod);
                case ALL:
                    return !Modifier.isStatic(mod) && !Modifier.isFinal(mod);
                default:
                    return false;
                }
            }
        }
    }).map(f -> {
        f.setAccessible(true);
        return f;
    }).collect(Collectors.toList());
}

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

/**
 * <p>Return an accessible method (that is, one that can be invoked via
 * reflection) by scanning through the superclasses. If no such method can
 * be found, return//  w  w w .  j  a  va 2  s . c om
 * <code>null</code>.</p>
 *
 * @param cls Class to be checked
 * @param methodName Method name of the method we wish to call
 * @param parameterTypes The parameter type signatures
 * @return the accessible method or
 * <code>null</code> if not found
 */
private static Method getAccessibleMethodFromSuperclass(Class cls, String methodName, Class[] parameterTypes) {
    Class parentClass = cls.getSuperclass();
    while (parentClass != null) {
        if (Modifier.isPublic(parentClass.getModifiers())) {
            try {
                return parentClass.getMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e) {
                return null;
            }
        }
        parentClass = parentClass.getSuperclass();
    }
    return null;
}

From source file:com.zigbee.framework.common.util.BeanCopyUtil.java

/**
 * Override copyProperties Method/*from   ww  w .  j  a v  a 2s . c o m*/
 * @Date        :      2011-8-5
 * @param source source bean instance
 * @param target destination bean instance
 */
public static void copyProperties(Object source, Object target) {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");
    Class<?> actualEditable = target.getClass();
    PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null) {

            try {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
                if (sourcePd != null && sourcePd.getReadMethod() != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    Object value = readMethod.invoke(source);
                    //Check whether the value is empty, only copy the properties which are not empty
                    if (value != null) {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, value);
                    }

                }
            } catch (BeansException e) {
                e.printStackTrace();
                String errMsg = "BEAN COPY Exception!" + e.getMessage();
                logger.error(errMsg);

            } catch (SecurityException e) {
                e.printStackTrace();
                String errMsg = "BEAN COPY Exception!" + e.getMessage();
                logger.error(errMsg);

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                String errMsg = "BEAN COPY Exception!" + e.getMessage();
                logger.error(errMsg);

            } catch (IllegalAccessException e) {
                e.printStackTrace();
                String errMsg = "BEAN COPY Exception!" + e.getMessage();
                logger.error(errMsg);

            } catch (InvocationTargetException e) {
                e.printStackTrace();
                String errMsg = "BEAN COPY Exception!" + e.getMessage();
                logger.error(errMsg);

            }

        }
    }
}