Example usage for java.lang.reflect Modifier isPrivate

List of usage examples for java.lang.reflect Modifier isPrivate

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isPrivate.

Prototype

public static boolean isPrivate(int mod) 

Source Link

Document

Return true if the integer argument includes the private modifier, false otherwise.

Usage

From source file:org.soyatec.windowsazure.table.internal.CloudTableRest.java

/**
 * Deserial the xml to object accord to the give model class.
 *
 * @param partitionKey//  ww w.  ja  va  2s .  com
 * @param rowKey
 * @param eTag
 * @param timestamp
 * @param values
 * @return Instance of the given model class.
 */
private ITableServiceEntity createObjectByModelClass(String partitionKey, String rowKey, String eTag,
        String timestamp, List<ICloudTableColumn> values) {
    ITableServiceEntity newInstance = instanceModel(partitionKey, rowKey, eTag, timestamp, values);
    if (newInstance == null) {
        return null;
    }
    // Copy Field
    if (values != null && !values.isEmpty()) {
        for (ICloudTableColumn column : values) {
            Field field = null;
            try {
                field = newInstance.getClass().getDeclaredField(column.getName());
            } catch (NoSuchFieldException e) {
                continue;
            }

            if (field == null) {
                continue;
            }
            int modifier = field.getModifiers();
            if (Modifier.isPrivate(modifier) && Modifier.isFinal(modifier) && Modifier.isStatic(modifier)) {
                if (getModelClass() != null) {
                    Logger.debug(MessageFormat.format(
                            "{0} class {1} is a static final field. Can not set data to it.",
                            getModelClass().getClass(), field.getName()));
                }
                continue;
            }

            boolean accessible = field.isAccessible();
            if (!accessible) {
                field.setAccessible(true);
            }
            ETableColumnType type = column.getType();
            String value = column.getValue();
            if (value == null) {
                continue;
            } else {
                try {
                    if (type == null) {
                        setStringOrObjectField(newInstance, column, field, value);
                    } else if (type.equals(ETableColumnType.TYPE_BINARY)) {
                        field.set(newInstance, Base64.decode(value));
                    } else if (type.equals(ETableColumnType.TYPE_BOOL)) {
                        field.setBoolean(newInstance, Boolean.parseBoolean(value));
                    } else if (type.equals(ETableColumnType.TYPE_DATE_TIME)) {
                        field.set(newInstance, Utilities.tryGetDateTimeFromTableEntry(value));
                    } else if (type.equals(ETableColumnType.TYPE_DOUBLE)) {
                        field.setDouble(newInstance, Double.parseDouble(value));
                    } else if (type.equals(ETableColumnType.TYPE_GUID)) {
                        Guid guid = new Guid();
                        try {
                            Field valueField = guid.getClass().getDeclaredField("value");
                            boolean accessiable = valueField.isAccessible();
                            if (!accessible) {
                                valueField.setAccessible(true);
                            }
                            valueField.set(guid, value);
                            valueField.setAccessible(accessiable);
                            field.set(newInstance, guid);
                        } catch (NoSuchFieldException e) {
                            Logger.error(e.getMessage(), e);
                        }
                    } else if (type.equals(ETableColumnType.TYPE_INT)) {
                        try {
                            field.setInt(newInstance, Integer.parseInt(value));
                        } catch (Exception e) {
                            field.setByte(newInstance, Byte.parseByte(value));
                        }
                    } else if (type.equals(ETableColumnType.TYPE_LONG)) {
                        field.setLong(newInstance, Long.parseLong(value));
                    } else if (type.equals(ETableColumnType.TYPE_STRING)) {
                        setStringOrObjectField(newInstance, column, field, value);
                    }
                } catch (Exception e) {
                    Logger.error(
                            MessageFormat.format("{0} class filed {1} set failed.", getModelClass(), value), e);
                }
            }
            // revert aaccessible
            field.setAccessible(accessible);
        }
    }
    return newInstance;
}

From source file:h2o.common.spring.util.ClassUtils.java

/**
 * Determine whether the given method is overridable in the given target class.
 * @param method the method to check// ww w  .ja va2s  . c  o  m
 * @param targetClass the target class to check against
 */
@SuppressWarnings("rawtypes")
private static boolean isOverridable(Method method, Class targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

From source file:com.laidians.utils.ClassUtils.java

/**
 * Determine whether the given method is overridable in the given target class.
 * @param method the method to check/*  w w  w  . j av  a 2s. c  o m*/
 * @param targetClass the target class to check against
 */
private static boolean isOverridable(Method method, Class targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

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

public static boolean canUse(Class<?> c) {
    //if (Throwable.class.isAssignableFrom(c))
    //   return false;
    if (Modifier.isPrivate(c.getModifiers()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated class {}", c.getName());
        return false;
    }//from w  ww .  jav  a2  s  .c  o m

    if (c.isAnonymousClass()) {
        return false;
    }

    if (ANONYMOUS_MATCHER1.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (ANONYMOUS_MATCHER2.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (c.getName().startsWith("junit"))
        return false;

    if (isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) {
        return false;
    }

    if (c.getEnclosingClass() != null) {
        if (!canUse(c.getEnclosingClass()))
            return false;
    }

    if (c.getDeclaringClass() != null) {
        if (!canUse(c.getDeclaringClass()))
            return false;
    }

    // If the SUT is not in the default package, then
    // we cannot import classes that are in the default
    // package
    if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c);
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            return true;
        }
    }

    logger.debug("Not public");
    return false;
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Determine whether the given method is overridable in the given target class.
 * @param method the method to check/* w  w w  .j  ava2s.com*/
 * @param targetClass the target class to check against
 */
private static boolean isOverridable(Method method, Class<?> targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

From source file:com.freetmp.common.util.ClassUtils.java

private static boolean isOverridable(Method method, Class<?> targetClass) {
    if (Modifier.isPrivate(method.getModifiers())) {
        return false;
    }/*from  ww w  .  j  av  a  2s  . c om*/
    if (Modifier.isPublic(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) {
        return true;
    }
    return getPackageName(method.getDeclaringClass()).equals(getPackageName(targetClass));
}

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

public static boolean canUse(Field f, Class<?> ownerClass) {

    // TODO we could enable some methods from Object, like getClass
    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 (!Properties.USE_DEPRECATED && f.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated field {}", f.getName());
        return false;
    }/*from   w w w . j ava2 s . c o  m*/

    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;
    }

    if (!f.getType().equals(String.class) && !canUse(f.getType())) {
        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
        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())) {
        //              && !Modifier.isProtected(f.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);

        String declaredPackageName = ClassUtils.getPackageName(f.getDeclaringClass());

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

    return false;
}

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

public static boolean canUse(Method m, Class<?> ownerClass) {

    if (m.isBridge()) {
        logger.debug("Excluding bridge method: {}", m.toString());
        return false;
    }/*from w  w  w .  ja v  a  2s .c  o  m*/

    if (m.isSynthetic()) {
        logger.debug("Excluding synthetic method: {}", m.toString());
        return false;
    }

    if (!Properties.USE_DEPRECATED && m.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Excluding deprecated method {}", m.getName());
        return false;
    }

    if (m.isAnnotationPresent(Test.class)) {
        logger.debug("Excluding test method {}", m.getName());
        return false;
    }

    if (m.isAnnotationPresent(EvoSuiteExclude.class)) {
        logger.debug("Excluding method with exclusion annotation {}", m.getName());
        return false;
    }

    if (m.getDeclaringClass().equals(java.lang.Object.class)) {
        return false;
    }

    if (!m.getReturnType().equals(String.class) && !canUse(m.getReturnType())) {
        return false;
    }

    if (m.getDeclaringClass().equals(Enum.class)) {
        return false;
        /*
        if (m.getName().equals("valueOf") || m.getName().equals("values")
         || m.getName().equals("ordinal")) {
           logger.debug("Excluding valueOf for Enum " + m.toString());
           return false;
        }
        // Skip compareTo on enums (like Randoop)
        if (m.getName().equals("compareTo") && m.getParameterTypes().length == 1
         && m.getParameterTypes()[0].equals(Enum.class))
           return false;
           */
    }

    if (m.getDeclaringClass().equals(java.lang.Thread.class))
        return false;

    // Hashcode only if we need to cover it
    if (m.getName().equals("hashCode") && !m.getDeclaringClass().equals(Properties.getTargetClass()))
        return false;

    // Randoop special case: just clumps together a bunch of hashCodes, so skip it
    if (m.getName().equals("deepHashCode") && m.getDeclaringClass().equals(Arrays.class))
        return false;

    // Randoop special case: differs too much between JDK installations
    if (m.getName().equals("getAvailableLocales"))
        return false;

    if (m.getName().equals(ClassResetter.STATIC_RESET)) {
        logger.debug("Ignoring static reset class");
        return false;
    }

    if (isForbiddenNonDeterministicCall(m)) {
        return false;
    }

    if (!Properties.CONSIDER_MAIN_METHODS && m.getName().equals("main") && Modifier.isStatic(m.getModifiers())
            && Modifier.isPublic(m.getModifiers())) {
        logger.debug("Ignoring static main method ");
        return false;
    }

    /*
    if(m.getTypeParameters().length > 0) {
       logger.debug("Cannot handle generic methods at this point");
       if(m.getDeclaringClass().equals(Properties.getTargetClass())) {
    LoggingUtils.getEvoLogger().info("* Skipping method "+m.getName()+": generic methods are not handled yet");
       }
       return false;
    }
    */

    // If default or
    if (Modifier.isPublic(m.getModifiers())) {
        makeAccessible(m);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(m.getModifiers())) {
        //              && !Modifier.isProtected(m.getModifiers())) {
        String packageName = ClassUtils.getPackageName(ownerClass);
        String declaredPackageName = ClassUtils.getPackageName(m.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) {
            makeAccessible(m);
            return true;
        }
    }

    return false;
}

From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java

private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
    try {/*from w ww  . j  a va2  s .co  m*/
        if (type.isArray()) {
            Class<?> componentType = type.getComponentType();
            // TODO - only handles 2-dimensional arrays
            if (componentType.isArray()) {
                Object array = Array.newInstance(componentType, 1);
                Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
                return array;
            } else {
                return Array.newInstance(componentType, 0);
            }
        } else if (Collection.class.isAssignableFrom(type)) {
            TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
            return CollectionFactory.createCollection(type,
                    (elementDesc != null ? elementDesc.getType() : null), 16);
        } else if (Map.class.isAssignableFrom(type)) {
            TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
            return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
        } else {
            Constructor<?> ctor = type.getDeclaredConstructor();
            if (Modifier.isPrivate(ctor.getModifiers())) {
                throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor);
            }
            return BeanUtils.instantiateClass(ctor);
        }
    } catch (Throwable ex) {
        throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
                "Could not instantiate property type [" + type.getName()
                        + "] to auto-grow nested property path",
                ex);
    }
}

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

public static boolean canUse(Constructor<?> c) {

    if (c.isSynthetic()) {
        return false;
    }//w  w w .  ja  v  a 2 s  .co  m

    // synthetic constructors are OK
    if (Modifier.isAbstract(c.getDeclaringClass().getModifiers()))
        return false;

    // TODO we could enable some methods from Object, like getClass
    //if (c.getDeclaringClass().equals(java.lang.Object.class))
    //   return false;// handled here to avoid printing reasons

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

    if (c.getDeclaringClass().isAnonymousClass())
        return false;

    if (c.getDeclaringClass().isLocalClass()) {
        logger.debug("Skipping constructor of local class {}", c.getName());
        return false;
    }

    if (c.getDeclaringClass().isMemberClass() && !canUse(c.getDeclaringClass()))
        return false;

    if (!Properties.USE_DEPRECATED && c.getAnnotation(Deprecated.class) != null) {
        logger.debug("Skipping deprecated constructor {}", c.getName());
        return false;
    }

    if (isForbiddenNonDeterministicCall(c)) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        makeAccessible(c);
        return true;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            makeAccessible(c);
            return true;
        }
    }

    return false;
}