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

public ClassFigure(Class cls) {
    setLayoutManager(new ToolbarLayout());
    setBorder(new LineBorder(ColorConstants.black));
    setBackgroundColor(ColorConstants.yellow);
    setOpaque(true);//w  ww  .  j av a  2 s.  c  o m

    for (int i = 0; i < keys.length; i++)
        registry.put(keys[i], ImageDescriptor.createFromFile(null, "icons/java/" + keys[i] + ".gif"));

    Label title = new Label(cls.getName(), registry.get(KEY_CLASS));
    add(title);
    add(fieldBox);
    add(methodBox);

    // fields.
    Field[] fields = cls.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        Image image = null;
        if (Modifier.isPublic(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PUBLIC);
        } else if (Modifier.isProtected(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PROTECTED);
        } else if (Modifier.isPrivate(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PRIVATE);
        } else {
            image = registry.get(KEY_FIELD_DEFAULT);
        }
        fieldBox.add(new Label(fields[i].getName(), image));
    }

    // fields.
    Method[] methods = cls.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        Image image = null;
        if (Modifier.isPublic(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PUBLIC);
        } else if (Modifier.isProtected(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PROTECTED);
        } else if (Modifier.isPrivate(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PRIVATE);
        } else {
            image = registry.get(KEY_METHOD_DEFAULT);
        }
        methodBox.add(new Label(methods[i].getName(), image));
    }

}

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

public static void makeAccessible(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())
            || !Modifier.isPublic(constructor.getDeclaringClass().getModifiers())) {
        constructor.setAccessible(true);
    }/*  ww  w  . j av a  2  s. c  om*/
}

From source file:com.epam.catgenome.manager.externaldb.bindings.ExternalDBBindingTest.java

private void processFactory(Object factory)
        throws InvocationTargetException, IllegalAccessException, InstantiationException {
    List<Object> factoryObjects = new ArrayList<>();
    Map<Class, Object> objectMap = new HashMap<>();

    for (Method m : factory.getClass().getDeclaredMethods()) {
        if (Modifier.isPublic(m.getModifiers())) {
            Object o;// w  ww.  jav  a2 s . c  o  m
            if (m.getParameterCount() == 0) {
                o = m.invoke(factory);
            } else {
                Object[] params = new Object[m.getParameterCount()];
                for (int i = 0; i < m.getParameterCount(); i++) {
                    params[i] = createParam(m.getParameterTypes()[i]);
                }

                o = m.invoke(factory, params);
            }

            factoryObjects.add(o);
            objectMap.put(o.getClass(), o);
        }
    }

    for (Object o : factoryObjects) {
        for (Method m : o.getClass().getDeclaredMethods()) {
            if (Modifier.isPublic(m.getModifiers())) {
                if (m.getParameterTypes().length > 0) {
                    Class type = m.getParameterTypes()[0];
                    if (objectMap.containsKey(type)) {
                        m.invoke(o, objectMap.get(type));
                    } else {
                        Object param = createParam(type);

                        m.invoke(o, param);
                    }
                } else {
                    m.invoke(o);
                }
            }
        }
    }
}

From source file:de.bstreit.java.oscr.initialdata.AbstractDataContainer.java

/**
 * /*from  www. j  a va 2 s.  c  om*/
 * @param field
 * @return true, if the field is a constant (public static final) and of the
 *         expected type provided by {@link #getType()}
 */
private boolean isConstantAndOfType(Field field) {
    final int modifiers = field.getModifiers();

    final boolean isConstant = Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)
            && Modifier.isPublic(modifiers);

    final boolean correctType = field.getType().equals(getType());

    return isConstant && correctType;
}

From source file:de.hybris.platform.webservices.util.objectgraphtransformer.impl.AbstractNodeConfig.java

/**
 * Creates a lookup map containing all properties of passed type.
 * <p/>//from   w w w.  j a  v a  2 s  . c o m
 * Result maps a property name to a {@link PropertyConfig}.
 * </p>
 * Any property which keeps java bean standard is found and used for {@link PropertyConfig} creation. For finding all
 * properties {@link Introspector} is used which returns general {@link PropertyDescriptor}. But read- and write
 * methods provided by {@link PropertyDescriptor} are only used as "suggestion" here and are getting post-processed
 * to assure following criteria:
 * <p/>
 * - no bridge or synthetic methods are allowed <br/>
 * - co-variant return types are handled correctly <br/>
 * 
 * @param type
 * @return
 */
private Map<String, PropertyConfig> createPropertiesFor(Class<?> type) {
    final Map<String, PropertyConfig> result = new TreeMap<String, PropertyConfig>();
    final Set<String> done = new HashSet<String>();
    while (type != null) {
        // we are only interested in declared methods (no bridge/synthetic ones)
        final Method[] methods = type.getDeclaredMethods();
        for (final Method method : methods) {
            // only public, non-bridged methods are of interest
            if (!method.isBridge() && Modifier.isPublic(method.getModifiers())) {
                // potential bean-getter property?
                if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) {
                    // not processed yet?
                    final String methodName = method.getName();
                    if (!done.contains(methodName)) {
                        done.add(methodName);

                        final Matcher matcher = BEAN_GETTER.matcher(methodName);
                        String propertyName = null;
                        if (matcher.matches()) {
                            propertyName = matcher.group(1);
                        } else {
                            if (method.getReturnType().equals(boolean.class)) {
                                final Matcher matcher2 = BEAN_BOOLEAN_GETTER.matcher(methodName);
                                if (matcher2.matches()) {
                                    propertyName = matcher2.group(1);
                                }
                            }
                        }

                        if (propertyName != null) {
                            propertyName = normalizePropertyName(propertyName);

                            // get or create a PropertyConfig
                            DefaultPropertyConfig pCfg = (DefaultPropertyConfig) result.get(propertyName);
                            if (pCfg == null) {
                                pCfg = new DefaultPropertyConfig(propertyName, null, null);
                                result.put(propertyName, pCfg);
                            }
                            pCfg.setReadMethod(method);
                        }
                    }
                }

                // potential bean-setter property?
                if (method.getParameterTypes().length == 1 && method.getReturnType() == void.class) {
                    // not processed yet?
                    final String methodName = method.getName();
                    if (!done.contains(methodName)) {
                        done.add(methodName);
                        final Matcher setter = BEAN_SETTER.matcher(methodName);
                        if (setter.matches()) {
                            String propertyName = setter.group(1);
                            propertyName = Character.toLowerCase(propertyName.charAt(0))
                                    + propertyName.substring(1);

                            // get or create a PropertyConfig
                            DefaultPropertyConfig pCfg = (DefaultPropertyConfig) result.get(propertyName);
                            if (pCfg == null) {
                                pCfg = new DefaultPropertyConfig(propertyName, null, null);
                                result.put(propertyName, pCfg);
                            }
                            pCfg.setWriteMethod(method);
                        }
                    }
                }
            }

        }
        type = type.getSuperclass();
    }
    return result;
}

From source file:org.braiden.fpm2.util.PropertyUtils.java

private static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
    Method result = null;/* ww  w . j a  va  2 s  .c  o  m*/
    try {
        result = clazz.getMethod(methodName, params);

        if (result != null && !Modifier.isPublic(result.getModifiers())) {
            result = null;
        }
    } catch (NoSuchMethodException e) {

    }
    return result;
}

From source file:net.abhinavsarkar.spelhelper.ImplicitMethodResolver.java

@Override
public MethodExecutor resolve(final EvaluationContext context, final Object targetObject, final String name,
        final List<TypeDescriptor> argumentTypes) throws AccessException {
    if (targetObject == null) {
        return null;
    }//from   www.  j a  v  a2s.c o m
    Class<?> type = targetObject.getClass();
    String cacheKey = type.getName() + "." + name;
    if (CACHE.containsKey(cacheKey)) {
        MethodExecutor executor = CACHE.get(cacheKey);
        return executor == NULL_ME ? null : executor;
    }

    Method method = lookupMethod(context, type, name);
    if (method != null) {
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
            Class<?>[] parameterTypes = method.getParameterTypes();
            Class<?> firstParamType = parameterTypes[0];
            if (parameterTypes.length > 0 && firstParamType.isAssignableFrom(type)) {
                List<TypeDescriptor> newArgumentTypes = new ArrayList<TypeDescriptor>();
                newArgumentTypes.add(TypeDescriptor.valueOf(firstParamType));
                newArgumentTypes.addAll(argumentTypes);

                MethodExecutor executor = delegate.resolve(context, method.getDeclaringClass(), name,
                        newArgumentTypes);
                MethodExecutor wrappedExecutor = executor == null ? null : new ImplicitMethodExecutor(executor);
                if (wrappedExecutor == null) {
                    CACHE.putIfAbsent(cacheKey, NULL_ME);
                }
                return wrappedExecutor;
            }
        }
    }
    CACHE.putIfAbsent(cacheKey, NULL_ME);
    return null;
}

From source file:com.palantir.ptoss.util.Reflections.java

/**
 * Returns whether or not the given {@link Method} is public.
 *//*from   w  w  w  . j  a v  a  2  s  . c o  m*/
public static boolean isMethodPublic(Method method) {
    int modifiers = method.getModifiers();
    return Modifier.isPublic(modifiers);
}

From source file:org.apache.shiro.guice.BeanTypeListener.java

public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) {
    PropertyDescriptor propertyDescriptors[] = PropertyUtils.getPropertyDescriptors(type.getRawType());
    final Map<PropertyDescriptor, Key<?>> propertyDependencies = new HashMap<PropertyDescriptor, Key<?>>(
            propertyDescriptors.length);
    final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class);
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        if (propertyDescriptor.getWriteMethod() != null
                && Modifier.isPublic(propertyDescriptor.getWriteMethod().getModifiers())) {
            Type propertyType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];
            propertyDependencies.put(propertyDescriptor, createDependencyKey(propertyDescriptor, propertyType));
        }/*  www .  ja va2 s .co m*/
    }
    encounter.register(new MembersInjector<I>() {
        public void injectMembers(I instance) {
            for (Map.Entry<PropertyDescriptor, Key<?>> dependency : propertyDependencies.entrySet()) {
                try {
                    final Injector injector = injectorProvider.get();

                    Object value = injector.getInstance(getMappedKey(injector, dependency.getValue()));
                    dependency.getKey().getWriteMethod().invoke(instance, value);

                } catch (ConfigurationException e) {
                    // This is ok, it simply means that we can't fulfill this dependency.
                    // Is there a better way to do this?
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Couldn't set property " + dependency.getKey().getDisplayName(),
                            e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(
                            "We shouldn't have ever reached this point, we don't try to inject to non-accessible methods.",
                            e);
                }
            }

        }
    });
}

From source file:com.link_intersystems.beans.BeanClass.java

/**
 * Has the clazz a public default constructor?
 *
 * @param clazz//  w w w  .  j  a va 2  s  .  com
 * @return
 */
private static boolean hasBeanConstructor(Class<?> clazz) {
    try {
        Constructor<?> defaultConstructor = clazz.getDeclaredConstructor();
        int modifiers = defaultConstructor.getModifiers();
        return Modifier.isPublic(modifiers);
    } catch (NoSuchMethodException e) {
        return false;
    }
}