List of usage examples for java.lang.reflect Modifier isPrivate
public static boolean isPrivate(int mod)
From source file:org.evosuite.setup.TestUsageChecker.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)) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && !f.getDeclaringClass().equals(targetClass)) { logger.debug("Skipping deprecated field " + f.getName()); return false; }//from www . j a v a2s .c om } 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; } // 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())) { // && !Modifier.isProtected(f.getModifiers())) { String packageName = ClassUtils.getPackageName(ownerClass); String declaredPackageName = ClassUtils.getPackageName(f.getDeclaringClass()); if (packageName.equals(Properties.CLASS_PREFIX) && packageName.equals(declaredPackageName)) { TestClusterUtils.makeAccessible(f); return true; } } return false; }
From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java
@Test public void publicMethods() { memberCriteria.membersOfType(Method.class); memberCriteria.withAccess(AccessType.PUBLIC); ClassCriteria classCriteria = new ClassCriteria(); Iterable<Class<?>> classIterable = classCriteria.getIterable(ArrayList.class); Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable); assertTrue(memberIterable.iterator().hasNext()); for (Member member : memberIterable) { assertTrue("member must be a method", member instanceof Method); int modifiers = member.getModifiers(); assertTrue(Modifier.isPublic(modifiers)); assertFalse(Modifier.isProtected(modifiers)); assertFalse(Modifier.isPrivate(modifiers)); }/*from w w w . j a v a 2 s .com*/ }
From source file:org.eclipse.buildship.docs.source.SourceMetaDataVisitor.java
private int extractModifiers(GroovySourceAST t, AbstractLanguageElement metaData) { final int modifiers = extractModifiers(t); metaData.setAbstract(Modifier.isAbstract(modifiers)); if (groovy) { metaData.setPublic(!Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers)); } else {//from w ww . j a v a 2s. c om metaData.setPublic(Modifier.isPublic(modifiers) || (metaData instanceof MethodMetaData && ((MethodMetaData) metaData).getOwnerClass().isInterface())); } return modifiers; }
From source file:org.apache.hadoop.fs.TestFilterFileSystem.java
@Test public void testFilterFileSystem() throws Exception { for (Method m : FileSystem.class.getDeclaredMethods()) { if (Modifier.isStatic(m.getModifiers())) continue; if (Modifier.isPrivate(m.getModifiers())) continue; if (Modifier.isFinal(m.getModifiers())) continue; try {/* w w w . ja va 2 s . c o m*/ DontCheck.class.getMethod(m.getName(), m.getParameterTypes()); LOG.info("Skipping " + m); } catch (NoSuchMethodException exc) { LOG.info("Testing " + m); try { FilterFileSystem.class.getDeclaredMethod(m.getName(), m.getParameterTypes()); } catch (NoSuchMethodException exc2) { LOG.error("FilterFileSystem doesn't implement " + m); throw exc2; } } } }
From source file:org.gradle.build.docs.dsl.SourceMetaDataVisitor.java
private void maybeAddPropertyFromField(GroovySourceAST t) { GroovySourceAST parentNode = getParentNode(); boolean isField = parentNode != null && parentNode.getType() == OBJBLOCK; if (!isField) { return;//w w w . j av a 2 s .com } int modifiers = extractModifiers(t); boolean isConst = getCurrentClass().isInterface() || (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)); if (isConst) { visitConst(t); return; } boolean isProp = groovy && !Modifier.isStatic(modifiers) && !Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers); if (!isProp) { return; } ASTIterator children = new ASTIterator(t); children.skip(MODIFIERS); String propertyName = extractIdent(t); TypeMetaData propertyType = extractTypeName(children.current); ClassMetaData currentClass = getCurrentClass(); MethodMetaData getterMethod = currentClass .addMethod(String.format("get%s", StringUtils.capitalize(propertyName)), propertyType, ""); currentClass.addReadableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t), getterMethod); if (!Modifier.isFinal(modifiers)) { MethodMetaData setterMethod = currentClass .addMethod(String.format("set%s", StringUtils.capitalize(propertyName)), TypeMetaData.VOID, ""); setterMethod.addParameter(propertyName, propertyType); currentClass.addWriteableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t), setterMethod); } }
From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java
@Test public void defaultAccessMethods() { memberCriteria.membersOfType(Method.class); memberCriteria.withAccess(AccessType.DEFAULT); ClassCriteria classCriteria = new ClassCriteria(); Iterable<Class<?>> classIterable = classCriteria.getIterable(Class.class); Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable); assertTrue(memberIterable.iterator().hasNext()); for (Member member : memberIterable) { assertTrue("member must be a method", member instanceof Method); int modifiers = member.getModifiers(); assertFalse(Modifier.isPublic(modifiers)); assertFalse(Modifier.isProtected(modifiers)); assertFalse(Modifier.isPrivate(modifiers)); }//w w w . j ava2 s. com }
From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java
private static void addFields(Object target, Class<?> startClass, Class<?> stopClass, LinkedHashMap<String, Object> map) { if (startClass != stopClass) { addFields(target, startClass.getSuperclass(), stopClass, map); }//w w w.ja v a2s . c o m Field[] fields = startClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers()) || Modifier.isPrivate(field.getModifiers())) { continue; } try { field.setAccessible(true); Object o = field.get(target); if (o != null && o.getClass().isArray()) { try { o = Arrays.asList((Object[]) o); } catch (Throwable e) { } } map.put(field.getName(), o); } catch (Throwable e) { e.printStackTrace(); } } }
From source file:com.link_intersystems.lang.reflect.Member2.java
/** * @param referenceMember//from w w w . j a va 2 s. co m * @return true if the access modifier of this {@link Member2} are less * restrictive than the access modifier of the referenceInvokable. * <p> * Logic according to the java language specification: * * <pre> * The access modifier (6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs. In more detail: * <ul> * <li>If the overridden or hidden method is <code>public</code>, * then the overriding or hiding method must be <code>public</code>; * otherwise, a compile-time error occurs. <a name="39550"></a></li> * <li>If the overridden or hidden method is <code>protected</code>, * then the overriding or hiding method must be * <code>protected</code> or <code>public</code>; otherwise, a * compile-time error occurs. <a name="39551"></a></li> * <li>If the overridden or hidden method has default (package) * access, then the overriding or hiding method must not be * <code>private</code>; otherwise, a compile-time error occurs. * * </li> * </ul> * </pre> * * <pre> * +---------------------------------------------------------------------+ * | this | * +----------------+----------------+-------------------+---------------+ * | public | protected | package (default) | private | * +-----------+-------------------+----------------+----------------+-------------------+---------------+ * | | public | true | false | false | false | * | +-------------------+----------------+----------------+-------------------+---------------+ * | reference | protected | true | true | false | false | * | +-------------------+----------------+----------------+-------------------+---------------+ * | invokable | package (default) | true | true | true | false | * | +-------------------+----------------+----------------+-------------------+---------------+ * | | private | false | false | false | false | * +-----------+-------------------+----------------+----------------+-------------------+---------------+ * </pre> * * </p> * @since 1.0.0.0 */ protected boolean isAccessModifierOverriddingCompatible(Member2<?> referenceMember) { int referenceModifiers = referenceMember.getModifiers(); int invokableModifiers = getModifiers(); if (Modifier.isPrivate(referenceModifiers) || Modifier.isPrivate(invokableModifiers)) { return false; } if (Modifier.isPublic(referenceModifiers)) { return Modifier.isPublic(invokableModifiers); } if (Modifier.isProtected(referenceModifiers)) { return Modifier.isProtected(invokableModifiers) || Modifier.isPublic(invokableModifiers); } return true; }
From source file:com.link_intersystems.lang.reflect.criteria.MemberCriteriaTest.java
@Test public void defaultAccessMethodsWithStaticNativeOnly() { memberCriteria.membersOfType(Method.class); memberCriteria.withAccess(AccessType.DEFAULT); memberCriteria.withModifiers(Modifier.STATIC | Modifier.NATIVE); ClassCriteria classCriteria = new ClassCriteria(); Iterable<Class<?>> classIterable = classCriteria.getIterable(Class.class); Iterable<Member> memberIterable = memberCriteria.getIterable(classIterable); assertTrue(memberIterable.iterator().hasNext()); for (Member member : memberIterable) { assertTrue("member must be a method", member instanceof Method); int modifiers = member.getModifiers(); assertFalse(Modifier.isPublic(modifiers)); assertFalse(Modifier.isProtected(modifiers)); assertFalse(Modifier.isPrivate(modifiers)); assertTrue(Modifier.isStatic(modifiers)); assertTrue(Modifier.isNative(modifiers)); }//w ww . j av a 2 s. c o m }
From source file:org.eclipse.buildship.docs.source.SourceMetaDataVisitor.java
private void maybeAddPropertyFromField(GroovySourceAST t) { GroovySourceAST parentNode = getParentNode(); boolean isField = parentNode != null && parentNode.getType() == OBJBLOCK; if (!isField) { return;/* w ww . java 2s. c o m*/ } int modifiers = extractModifiers(t); boolean isConst = getCurrentClass().isInterface() || (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)); if (isConst) { visitConst(t); return; } boolean isProp = groovy && !Modifier.isStatic(modifiers) && !Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && !Modifier.isPrivate(modifiers); if (!isProp) { return; } ASTIterator children = new ASTIterator(t); children.skip(MODIFIERS); String propertyName = extractIdent(t); TypeMetaData propertyType = extractTypeName(children.current); ClassMetaData currentClass = getCurrentClass(); MethodMetaData getterMethod = currentClass .addMethod(String.format("get%s", StringUtils.capitalize(propertyName)), propertyType, ""); PropertyMetaData property = currentClass.addReadableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t), getterMethod); findAnnotations(t, property); if (!Modifier.isFinal(modifiers)) { MethodMetaData setterMethod = currentClass .addMethod(String.format("set%s", StringUtils.capitalize(propertyName)), TypeMetaData.VOID, ""); setterMethod.addParameter(propertyName, propertyType); currentClass.addWriteableProperty(propertyName, propertyType, getJavaDocCommentsBeforeNode(t), setterMethod); } }