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.agimatec.validation.jsr303.util.SecureActions.java

private static void setAccessibility(Field field) {
    if (!Modifier.isPublic(field.getModifiers())
            || (Modifier.isPublic(field.getModifiers()) && Modifier.isAbstract(field.getModifiers()))) {
        field.setAccessible(true);//  www .  java2s .c o m
    }
}

From source file:com.baomidou.framework.common.util.BeanUtil.java

/**
 * ?// w w w  .jav a 2 s . co  m
 * 
 * @param source
 *            ?
 * @param target
 *            ?
 */
public static void copy(Object source, Object target) throws BeansException {
    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) {
            PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    /*
                     * no copy null properties
                     */
                    Object value = readMethod.invoke(source);
                    if (value != null) {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, value);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

public static Map<String, Map<Class<?>, Method>> findUnaryMethods(Class<?> clazz) {
    Map<String, Map<Class<?>, Method>> result = new TreeMap<>();

    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        int modified = method.getModifiers();
        if (Modifier.isPublic(modified) && method.getParameterTypes().length == 1) {
            Map<Class<?>, Method> methodMap = result.get(method.getName());
            if (methodMap == null) {
                methodMap = new HashMap<>();
                result.put(method.getName(), methodMap);
            }//from  www. j  a v  a 2 s.  c o m
            methodMap.put(method.getParameterTypes()[0], method);
        }
    }

    return result;
}

From source file:com.trafficspaces.api.model.Resource.java

public JSONObject getJSONObject() {
    JSONObject jsonObject = new JSONObject();
    Field[] fields = this.getClass().getFields();
    try {/*from w  ww .j a va 2 s.c om*/
        for (int i = 0; i < fields.length; i++) {

            Object fieldValue = fields[i].get(this);
            int fieldModifiers = fields[i].getModifiers();
            //System.out.println("name=" + fields[i].getName() + ", value=" + fieldValue + ", type=" +fields[i].getType() + 
            //      ", ispublic="+Modifier.isPublic(fieldModifiers) + ", isstatic="+Modifier.isStatic(fieldModifiers) + ", isnative="+Modifier.isNative(fieldModifiers));
            if (fieldValue != null && Modifier.isPublic(fieldModifiers) && !Modifier.isStatic(fieldModifiers)
                    && !Modifier.isNative(fieldModifiers)) {

                String name = fields[i].getName();
                Class type = fields[i].getType();

                if (type.isPrimitive()) {
                    if (type.equals(int.class)) {
                        jsonObject.put(name, fields[i].getInt(this));
                    } else if (type.equals(double.class)) {
                        jsonObject.put(name, fields[i].getDouble(this));
                    }
                } else if (type.isArray()) {
                    JSONObject jsonSubObject = new JSONObject();
                    JSONArray jsonArray = new JSONArray();

                    jsonObject.put(name, jsonSubObject);
                    jsonSubObject.put(name.substring(0, name.length() - 1), jsonArray);

                    Object[] values = (Object[]) fieldValue;
                    for (int j = 0; j < values.length; j++) {
                        if (values[j] != null && values[j] instanceof Resource) {
                            jsonArray.put(((Resource) values[j]).getJSONObject());
                        }
                    }
                } else if (Resource.class.isAssignableFrom(type)) {
                    jsonObject.put(name, ((Resource) fieldValue).getJSONObject());
                } else if (type.equals(String.class) && fieldValue != null) {
                    jsonObject.put(name, String.valueOf(fieldValue));
                }
            }
        }
    } catch (Exception nsfe) {
        //
    }
    return jsonObject;
}

From source file:org.apache.brooklyn.util.javalang.MethodAccessibleReflections.java

static boolean isAccessible(Member m) {
    return m != null && Modifier.isPublic(m.getModifiers());
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType.java

@SuppressWarnings("unchecked")
public static boolean supports(@SuppressWarnings("rawtypes") Class enumClass) {
    if (!isEnabled())
        return false;
    if (enumClass.isEnum()) {
        try {//  w w w.  j av  a 2 s . c o m
            Method idAccessor = enumClass.getMethod(ENUM_ID_ACCESSOR);
            int mods = idAccessor.getModifiers();
            if (Modifier.isPublic(mods) && !Modifier.isStatic(mods)) {
                Class<?> returnType = idAccessor.getReturnType();
                return returnType != null
                        && typeResolver.basic(returnType.getName()) instanceof AbstractStandardBasicType;
            }
        } catch (NoSuchMethodException e) {
            // ignore
        }
    }
    return false;
}

From source file:org.geoserver.catalog.impl.ModificationProxyCloner.java

/**
 * Best effort object cloning utility, tries different lightweight strategies, then falls back
 * on copy by XStream serialization (we use that one as we have a number of hooks to avoid deep
 * copying the catalog, and re-attaching to it, in there)
 * //from   w w  w.  j a  v  a2  s.com
 * @param source
 * @return
 */
static <T> T clone(T source) {
    // null?
    if (source == null) {
        return null;
    }

    // already a modification proxy?
    if (ModificationProxy.handler(source) != null) {
        return source;
    }

    // is it a catalog info?
    if (source instanceof CatalogInfo) {
        // mumble... shouldn't we wrap this one in a modification proxy object?
        return (T) ModificationProxy.create(source, getDeepestCatalogInfoInterface((CatalogInfo) source));
    }

    // if a known immutable?
    if (source instanceof String || source instanceof Byte || source instanceof Short
            || source instanceof Integer || source instanceof Float || source instanceof Double
            || source instanceof BigInteger || source instanceof BigDecimal) {
        return (T) source;
    }

    // is it cloneable?
    try {
        if (source instanceof Cloneable) {
            // methodutils does not seem to work against "clone()"...
            // return (T) MethodUtils.invokeExactMethod(source, "clone", null, null);
            Method method = source.getClass().getDeclaredMethod("clone");
            if (Modifier.isPublic(method.getModifiers()) && method.getParameterTypes().length == 0) {
                return (T) method.invoke(source);
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.FINE,
                "Source object is cloneable, yet it does not have a public no argument method 'clone'", e);
    }

    // does it have a copy constructor?
    Constructor copyConstructor = ConstructorUtils.getAccessibleConstructor(source.getClass(),
            source.getClass());
    if (copyConstructor != null) {
        try {
            return (T) copyConstructor.newInstance(source);
        } catch (Exception e) {
            LOGGER.log(Level.FINE, "Source has a copy constructor, but it failed, skipping to XStream", e);
        }
    }

    if (source instanceof Serializable) {
        return (T) SerializationUtils.clone((Serializable) source);
    } else {
        XStreamPersister persister = XSTREAM_PERSISTER_FACTORY.createXMLPersister();
        XStream xs = persister.getXStream();
        String xml = xs.toXML(source);
        T copy = (T) xs.fromXML(xml);
        return copy;
    }
}

From source file:org.pushio.webapp.helper.json.SerializeConfig.java

public ObjectSerializer createJavaBeanSerializer(Class<?> clazz) {
    if (!Modifier.isPublic(clazz.getModifiers())) {
        MyJavaBeanSerializer bean = new MyJavaBeanSerializer(clazz);
        return bean;
    }//from  ww w. j  a v  a2  s . co m
    return new MyJavaBeanSerializer(clazz);
}

From source file:org.apache.brooklyn.util.javalang.MethodAccessibleReflections.java

static boolean isAccessible(Class<?> c) {
    return c != null && Modifier.isPublic(c.getModifiers());
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

protected static void init(SCSession session, Class<? extends SCKeynodesBase> klass) {
    if (log.isDebugEnabled())
        log.debug("Start retrieving keynodes for " + klass);

    try {/*from w  ww  .  j ava2  s . c  om*/
        //
        // Search default segment for keynodes
        //
        SCSegment defaultSegment = null;
        {
            DefaultSegment annotation = klass.getAnnotation(DefaultSegment.class);
            if (annotation != null) {
                defaultSegment = session.openSegment(annotation.value());
                Validate.notNull(defaultSegment, "Default segment \"{0}\" not found", annotation.value());
                klass.getField(annotation.fieldName()).set(null, defaultSegment);
            }
        }

        Field[] fields = klass.getFields();
        for (Field field : fields) {
            int modifiers = field.getModifiers();

            if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
                Class<?> type = field.getType();
                if (type.equals(SCSegment.class)) {
                    //
                    // We have segment field. Load segment by uri.
                    //
                    SegmentURI annotation = field.getAnnotation(SegmentURI.class);

                    if (annotation != null) {
                        String uri = annotation.value();
                        SCSegment segment = session.openSegment(uri);
                        field.set(null, segment);
                    } else {
                        // May be it already has value?
                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have value");
                        }
                    }
                } else {
                    if (!(checkKeynode(session, defaultSegment, field) || checkKeynodeURI(session, field)
                            || checkKeynodesNumberPatternURI(session, klass, field))) {

                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have annotations and value");
                        }

                    }
                }
            }
        }
    } catch (Exception e) {
        // TODO: handle
        e.printStackTrace();
    }
}