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.evosuite.instrumentation.mutation.ReplaceVariable.java

/**
 * This replicates TestUsageChecker.canUse but we need to avoid that
 * we try to access Properties.getTargetClassAndDontInitialise
 *
 * @param f/*from  w  w w  .  jav a 2s . co m*/
 * @return
 */
public static boolean canUse(Field f) {

    if (f.getDeclaringClass().equals(java.lang.Object.class))
        return false;// handled here to avoid printing reasons

    if (f.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (f.isSynthetic()) {
        logger.debug("Skipping synthetic field " + f.getName());
        return false;
    }

    if (f.getName().startsWith("ajc$")) {
        logger.debug("Skipping AspectJ field " + f.getName());
        return false;
    }

    // in, out, err
    if (f.getDeclaringClass().equals(FileDescriptor.class)) {
        return false;
    }

    if (Modifier.isPublic(f.getModifiers())) {
        // It may still be the case that the field is defined in a non-visible superclass of the class
        // we already know we can use. In that case, the compiler would be fine with accessing the
        // field, but reflection would start complaining about IllegalAccess!
        // Therefore, we set the field accessible to be on the safe side
        TestClusterUtils.makeAccessible(f);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(f.getModifiers())) {
        String packageName = ClassUtils.getPackageName(f.getDeclaringClass());

        if (packageName.equals(Properties.CLASS_PREFIX)) {
            TestClusterUtils.makeAccessible(f);
            return true;
        }
    }

    return false;
}

From source file:com.gdevelop.gwt.syncrpc.SyncClientSerializationStreamReader.java

private void deserializeClass(Class<?> instanceClass, Object instance) throws SerializationException,
        IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
    Field[] serializableFields = SerializabilityUtil.applyFieldSerializationPolicy(instanceClass);

    for (Field declField : serializableFields) {
        assert (declField != null);

        Object value = deserializeValue(declField.getType());

        boolean isAccessible = declField.isAccessible();
        boolean needsAccessOverride = !isAccessible && !Modifier.isPublic(declField.getModifiers());
        if (needsAccessOverride) {
            // Override access restrictions
            declField.setAccessible(true);
        }//from  w  w w.ja  va  2 s .c  om

        declField.set(instance, value);
    }

    Class<?> superClass = instanceClass.getSuperclass();
    if (serializationPolicy.shouldDeserializeFields(superClass)) {
        deserializeImpl(SerializabilityUtil.hasCustomFieldSerializer(superClass), superClass, instance);
    }
}

From source file:adalid.core.Project.java

/**
 *
 * @param clazz/*from w  w  w  . j a v  a 2s.c o m*/
 */
public void attachAddAttributesMethods(Class<?> clazz) {
    logger.debug(signature("attachAddAttributesMethods", clazz));
    String name;
    boolean addAttributesMethod;
    int modifiers;
    Class<?> returnType;
    Class<?>[] parameterTypes;
    Method[] methods = clazz.getDeclaredMethods();
    List<Method> list = Arrays.asList(methods);
    Comparator<Method> comparator = new ByMethodSequence();
    ColUtils.sort(list, comparator);
    for (Method method : list) {
        name = method.getName();
        addAttributesMethod = method.isAnnotationPresent(AddAttributesMethod.class);
        modifiers = method.getModifiers();
        returnType = method.getReturnType();
        parameterTypes = method.getParameterTypes();
        if (addAttributesMethod && Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
                && void.class.equals(returnType) && parameterTypes.length == 1
                && Artifact.class.isAssignableFrom(parameterTypes[0])) {
            logger.debug(signature(clazz.getSimpleName() + "." + name, parameterTypes[0]));
            _addAttributesMethods.add(method);
        }
    }
}

From source file:org.evosuite.regression.RegressionAssertionCounter.java

private static Class<?> getExceptionClassToUse(Throwable exception) {
    /*/*from   ww  w .  j a va2 s  .c o m*/
    we can only catch a public class.
    for "readability" of tests, it shouldn't be a mock one either
      */
    Class<?> ex = exception.getClass();
    while (!Modifier.isPublic(ex.getModifiers()) || EvoSuiteMock.class.isAssignableFrom(ex)
            || ex.getCanonicalName().startsWith("com.sun.")) {
        ex = ex.getSuperclass();
    }
    return ex;
}

From source file:adalid.core.XS1.java

private static boolean isRestrictedFieldType(Class<?> fieldType) {
    int modifiers = fieldType.getModifiers();
    boolean b = fieldType.isPrimitive();
    b |= Modifier.isAbstract(modifiers) || !Modifier.isPublic(modifiers);
    b |= fieldType.isAnnotation();/*from  ww  w .j  a v  a  2s . c o  m*/
    b |= fieldType.isAnonymousClass();
    b |= fieldType.isArray();
    b |= fieldType.isEnum();
    b |= fieldType.isLocalClass();
    b |= fieldType.isInterface();
    return b;
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Work out if the specified object has a public field with the name supplied.
 *
 * @param obj/*  w  w w . j  av a2s.  c o  m*/
 * @param name
 * @return true if a public field with the name exists
 */
public static boolean isPublicField(Object obj, String name) {
    Class<?> clazz = obj.getClass();
    try {
        Field f = clazz.getDeclaredField(name);
        return Modifier.isPublic(f.getModifiers());
    } catch (NoSuchFieldException e) {
        return false;
    }
}

From source file:org.argouml.moduleloader.ModuleLoader2.java

/**
 * Try to load a module from the given ClassLoader.
 * <p>//from w  w  w  .  j a v a2 s  .  c om
 *
 * Only add it as a module if it is a module (i.e. it implements the
 * {@link ModuleInterface} interface.
 *
 * @param classLoader
 *            The ClassLoader to load from.
 * @param classname
 *            The name.
 * @throws ClassNotFoundException
 *             if the class classname is not found.
 */
private boolean addClass(ClassLoader classLoader, String classname) throws ClassNotFoundException {

    LOG.log(Level.INFO, "Loading module " + classname);
    Class moduleClass;
    try {
        moduleClass = classLoader.loadClass(classname);
    } catch (UnsupportedClassVersionError e) {
        LOG.log(Level.SEVERE, "Unsupported Java class version for " + classname);
        return false;
    } catch (NoClassDefFoundError e) {
        LOG.log(Level.SEVERE, "Unable to find required class while loading " + classname
                + " - may indicate an obsolete" + " extension module or an unresolved dependency", e);
        return false;
    } catch (Throwable e) {
        if (e instanceof ClassNotFoundException) {
            throw (ClassNotFoundException) e;
        }
        LOG.log(Level.SEVERE, "Unexpected error while loading " + classname, e);
        return false;
    }

    if (!ModuleInterface.class.isAssignableFrom(moduleClass)) {
        LOG.log(Level.FINE, "The class {0} is not a module.", classname);
        return false;
    }

    Constructor defaultConstructor;
    try {
        defaultConstructor = moduleClass.getDeclaredConstructor(new Class[] {});
    } catch (SecurityException e) {
        LOG.log(Level.SEVERE, "The default constructor for class " + classname + " is not accessable.", e);
        return false;
    } catch (NoSuchMethodException e) {
        LOG.log(Level.SEVERE, "The default constructor for class " + classname + " is not found.", e);
        return false;
    } catch (NoClassDefFoundError e) {
        LOG.log(Level.SEVERE, "Unable to find required class while loading " + classname
                + " - may indicate an obsolete" + " extension module or an unresolved dependency", e);
        return false;
    } catch (Throwable e) {
        LOG.log(Level.SEVERE, "Unexpected error while loading " + classname, e);
        return false;
    }

    if (!Modifier.isPublic(defaultConstructor.getModifiers())) {
        LOG.log(Level.SEVERE,
                "The default constructor for class " + classname + " is not public.  Not loaded.");
        return false;
    }
    Object moduleInstance;
    try {
        moduleInstance = defaultConstructor.newInstance(new Object[] {});
    } catch (IllegalArgumentException e) {
        LOG.log(Level.SEVERE, "The constructor for class " + classname + " is called with incorrect argument.",
                e);
        return false;
    } catch (InstantiationException e) {
        LOG.log(Level.SEVERE, "The constructor for class " + classname + " threw an exception.", e);
        return false;
    } catch (IllegalAccessException e) {
        LOG.log(Level.SEVERE, "The constructor for class " + classname + " is not accessible.", e);
        return false;
    } catch (InvocationTargetException e) {
        LOG.log(Level.SEVERE, "The constructor for class " + classname + " cannot be called.", e);
        return false;
    } catch (NoClassDefFoundError e) {
        LOG.log(Level.SEVERE, "Unable to find required class while instantiating " + classname
                + " - may indicate an obsolete" + " extension module or an unresolved dependency", e);
        return false;
    } catch (Throwable e) {
        LOG.log(Level.SEVERE, "Unexpected error while instantiating " + classname, e);
        return false;
    }

    // The following check should have been satisfied before we
    // instantiated the module, but double check again
    if (!(moduleInstance instanceof ModuleInterface)) {
        LOG.log(Level.SEVERE, "The class " + classname + " is not a module.");
        return false;
    }
    ModuleInterface mf = (ModuleInterface) moduleInstance;

    addModule(mf);
    LOG.log(Level.INFO, "Succesfully loaded module {0}", classname);
    return true;
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Check whether the specified method is a property getter
 *
 * @param method The method/*from   www. j  a v a2s .  c  o  m*/
 * @return true if the method is a property getter
 */
public static boolean isPropertyGetter(Method method) {
    return !Modifier.isStatic(method.getModifiers()) && Modifier.isPublic(method.getModifiers())
            && GrailsNameUtils.isGetter(method.getName(), method.getReturnType(), method.getParameterTypes());
}

From source file:javadz.beanutils.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 <code>null</code>.</p>
 *
 * @param clazz The class of the object//from w ww . ja  v  a 2s . c  om
 * @param method The method that we wish to call
 * @return The accessible method
 * @since 1.8.0
 */
public static Method getAccessibleMethod(Class clazz, Method method) {

    // Make sure we have a method to check
    if (method == null) {
        return (null);
    }

    // If the requested method is not public we cannot call it
    if (!Modifier.isPublic(method.getModifiers())) {
        return (null);
    }

    boolean sameClass = true;
    if (clazz == null) {
        clazz = method.getDeclaringClass();
    } else {
        sameClass = clazz.equals(method.getDeclaringClass());
        if (!method.getDeclaringClass().isAssignableFrom(clazz)) {
            throw new IllegalArgumentException(
                    clazz.getName() + " is not assignable from " + method.getDeclaringClass().getName());
        }
    }

    // If the class is public, we are done
    if (Modifier.isPublic(clazz.getModifiers())) {
        if (!sameClass && !Modifier.isPublic(method.getDeclaringClass().getModifiers())) {
            setMethodAccessible(method); // Default access superclass workaround
        }
        return (method);
    }

    String methodName = method.getName();
    Class[] parameterTypes = method.getParameterTypes();

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

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

    return (method);

}

From source file:com.glaf.core.util.ReflectUtils.java

public static Object invoke(Object object, String methodName) {
    try {/*from  ww w  .  jav  a  2  s  .  com*/
        Method method = ReflectionUtils.findMethod(object.getClass(), methodName);
        if (!Modifier.isPublic(method.getModifiers())) {
            method.setAccessible(true);
        }
        return ReflectionUtils.invokeMethod(method, object);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}