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:bboss.org.apache.velocity.util.introspection.ClassMap.java

private void populateMethodCacheWith(MethodCache methodCache, Class classToReflect) {
    if (debugReflection && log.isDebugEnabled()) {
        log.debug("Reflecting " + classToReflect);
    }/*from  w  w w . j  ava  2  s  . com*/

    try {
        Method[] methods = classToReflect.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            int modifiers = methods[i].getModifiers();
            if (Modifier.isPublic(modifiers)) {
                methodCache.put(methods[i]);
            }
        }
    } catch (SecurityException se) // Everybody feels better with...
    {
        if (log.isDebugEnabled()) {
            log.debug("While accessing methods of " + classToReflect + ": ", se);
        }
    }
}

From source file:org.drools.guvnor.server.contenthandler.ModelContentHandler.java

private boolean isClassVisible(ClassLoader cl, String className, String assetPackageName) {
    try {/*from  ww  w.  ja  va 2s  . c  o m*/
        Class<?> cls = cl.loadClass(className);
        int modifiers = cls.getModifiers();
        if (Modifier.isPublic(modifiers)) {
            return true;
        }
        String packageName = className.substring(0, className.lastIndexOf("."));
        if (!packageName.equals(assetPackageName)) {
            return false;
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:ca.uhn.fhir.rest.server.RestfulServer.java

private void assertProviderIsValid(Object theNext) throws ConfigurationException {
    if (Modifier.isPublic(theNext.getClass().getModifiers()) == false) {
        throw new ConfigurationException(
                "Can not use provider '" + theNext.getClass() + "' - Class must be public");
    }//from w  w w .  ja  va  2s .  com
}

From source file:org.apache.tajo.engine.function.FunctionLoader.java

private static StaticMethodInvocationDesc extractStaticMethodInvocation(Method method) {
    Preconditions.checkArgument(Modifier.isPublic(method.getModifiers()));
    Preconditions.checkArgument(Modifier.isStatic(method.getModifiers()));

    String methodName = method.getName();
    Class returnClass = method.getReturnType();
    Class[] paramClasses = method.getParameterTypes();
    return new StaticMethodInvocationDesc(method.getDeclaringClass(), methodName, returnClass, paramClasses);
}

From source file:com.googlecode.loosejar.ClassLoaderAnalyzer.java

@SuppressWarnings("unchecked")
private Enumeration<URL> findManifestResources() {
    //invoke #findResource(String) method reflectively as it is protected in java.lang.ClassLoader
    try {//from w  ww . j av  a2s  .  co m
        Method method = findMethod(classLoader.getClass(), "findResources", new Class<?>[] { String.class });

        //attempt to disable security check for non-public methods.
        if (!Modifier.isPublic(method.getModifiers()) && !method.isAccessible()) {
            method.setAccessible(true);
        }

        // This will return a transitive closure of all jars on the classpath
        // in the form of
        // jar:file:/foo/bar/baz.jar!/META-INF/MANIFEST.MF
        return (Enumeration<URL>) method.invoke(classLoader, "META-INF/MANIFEST.MF");

    } catch (IllegalAccessException e) {
        log("Failed to invoke #findResources(String) method on classloader [" + classLoader + "]. "
                + "No access permissions.");
    } catch (InvocationTargetException e) {
        log("Failed to invoke #findResources(String) method on classloader [" + classLoader + "]. "
                + "The classloader is likely no longer available.");
    }
    return null;
}

From source file:com.lakeside.data.sqldb.BaseDao.java

/**
 * ?/*from  w  w w.  j a v  a2 s.  com*/
 * @param entity
 * @return
 */
public T merge(final T entity) {
    Assert.notNull(entity, "entity?");
    Session session = getSession();
    String idName = getIdName();
    PropertyDescriptor idp = BeanUtils.getPropertyDescriptor(entityClass, idName);
    PK idvalue = null;
    try {
        idvalue = (PK) idp.getReadMethod().invoke(entity);
    } catch (Exception e) {
        throw new FatalBeanException("Could not copy properties from source to target", e);
    }
    T dest = null;
    if (idvalue != null) {
        dest = (T) session.get(entityClass, idvalue);
    }
    if (dest != null) {
        // merge the properties
        PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(entityClass);
        for (PropertyDescriptor p : descriptors) {
            if (p.getWriteMethod() != null) {
                try {
                    Method readMethod = p.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    Object value = readMethod.invoke(entity);
                    if (value == null) {
                        continue;
                    }
                    Method writeMethod = p.getWriteMethod();
                    if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                        writeMethod.setAccessible(true);
                    }
                    writeMethod.invoke(dest, value);
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    } else {
        // destination object is empty, save the entity object parameted
        dest = entity;
    }
    session.saveOrUpdate(dest);
    logger.debug("merge entity: {}", entity);
    return dest;
}

From source file:com.github.jknack.handlebars.helper.DefaultHelperRegistry.java

/**
 * <p>//  w w w  . j  a va 2 s. c  om
 * Register all the helper methods for the given helper source.
 * </p>
 *
 * @param source The helper source.
 * @param clazz The helper source class.
 */
private void registerDynamicHelper(final Object source, final Class<?> clazz) {
    int size = helpers.size();
    int replaced = 0;
    if (clazz != Object.class) {
        Set<String> overloaded = new HashSet<String>();
        // Keep backing up the inheritance hierarchy.
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            boolean isPublic = Modifier.isPublic(method.getModifiers());
            String helperName = method.getName();
            if (isPublic && CharSequence.class.isAssignableFrom(method.getReturnType())) {
                boolean isStatic = Modifier.isStatic(method.getModifiers());
                if (source != null || isStatic) {
                    if (helpers.containsKey(helperName)) {
                        replaced++;
                    }
                    isTrue(overloaded.add(helperName), "name conflict found: " + helperName);
                    registerHelper(helperName, new MethodHelper(method, source));
                }
            }
        }
    }
    isTrue((size + replaced) != helpers.size(), "No helper method was found in: " + clazz.getName());
}

From source file:com.weibo.api.motan.config.AbstractConfig.java

@Override
public String toString() {
    try {// ww  w  .  j av a  2 s.  com
        StringBuilder buf = new StringBuilder();
        buf.append("<motan:");
        buf.append(getTagName(getClass()));
        Method[] methods = getClass().getMethods();
        for (Method method : methods) {
            try {
                String name = method.getName();
                if ((name.startsWith("get") || name.startsWith("is")) && !"getClass".equals(name)
                        && !"get".equals(name) && !"is".equals(name) && Modifier.isPublic(method.getModifiers())
                        && method.getParameterTypes().length == 0 && isPrimitive(method.getReturnType())) {
                    int i = name.startsWith("get") ? 3 : 2;
                    String key = name.substring(i, i + 1).toLowerCase() + name.substring(i + 1);
                    Object value = method.invoke(this);
                    if (value != null) {
                        buf.append(" ");
                        buf.append(key);
                        buf.append("=\"");
                        buf.append(value);
                        buf.append("\"");
                    }
                }
            } catch (Exception e) {
                LoggerUtil.warn(e.getMessage(), e);
            }
        }
        buf.append(" />");
        return buf.toString();
    } catch (Throwable t) { // 
        LoggerUtil.warn(t.getMessage(), t);
        return super.toString();
    }
}

From source file:com.haulmont.cuba.core.app.serialization.EntitySerialization.java

protected void makeFieldAccessible(Field field) {
    if (field != null && !Modifier.isPublic(field.getModifiers())) {
        field.setAccessible(true);
    }
}

From source file:net.servicefixture.util.ReflectionUtils.java

public static String getDefaultValue(Class type) {
    if (isEnumarationPatternClass(type)) {
        StringBuilder sb = new StringBuilder();
        Field[] fields = type.getFields();
        for (int i = 0; i < fields.length; i++) {
            if (type.equals(fields[i].getType()) && Modifier.isFinal(fields[i].getModifiers())
                    && Modifier.isPublic(fields[i].getModifiers())
                    && Modifier.isStatic(fields[i].getModifiers())) {
                try {
                    sb.append(fields[i].getName()).append(";");
                } catch (IllegalArgumentException e) {
                }//  w w  w  . j  av a  2  s .c o m
            }
        }
        return sb.toString();
    } else if (type.isEnum()) {
        StringBuilder sb = new StringBuilder();
        try {
            Object[] array = (Object[]) type.getMethod("values").invoke(null);
            for (int i = 0; i < array.length; i++) {
                sb.append(array[i]).append(";");
            }
        } catch (Exception e) {
        }
        return sb.toString();

    } else if (Calendar.class.isAssignableFrom(type) || XMLGregorianCalendar.class.isAssignableFrom(type)) {
        return "${calendar.after(0)}";
    } else if (Date.class.isAssignableFrom(type)) {
        return "${date.after(0)}";
    } else if (Number.class.isAssignableFrom(type) || "long".equals(type.getName())
            || "int".equals(type.getName()) || "short".equals(type.getName()) || "float".equals(type.getName())
            || "double".equals(type.getName())) {
        return "0";
    } else if (String.class.equals(type)) {
        return "String";
    } else if (Boolean.class.equals(type) || "boolean".equals(type.getName())) {
        return "true";
    }

    return type.getName();
}