java.lang.Class.getModifiers()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(143)

本文整理了Java中java.lang.Class.getModifiers()方法的一些代码示例,展示了Class.getModifiers()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getModifiers()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getModifiers

Class.getModifiers介绍

[英]Returns an integer that represents the modifiers of the class represented by this Class. The returned value is a combination of bits defined by constants in the Modifier class.
[中]返回一个整数,该整数表示该类所表示的类的修饰符。返回值是由修饰符类中的常量定义的位的组合。

代码示例

代码示例来源:origin: spring-projects/spring-framework

public int getModifiers() {
    return clazz.getModifiers();
  }
};

代码示例来源:origin: spring-projects/spring-framework

public static int findPackageProtected(Class[] classes) {
  for (int i = 0; i < classes.length; i++) {
    if (!Modifier.isPublic(classes[i].getModifiers())) {
      return i;
    }
  }
  return 0;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isFinal() {
  return Modifier.isFinal(this.introspectedClass.getModifiers());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isAbstract() {
  return Modifier.isAbstract(this.introspectedClass.getModifiers());
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine if the supplied class is an <em>inner class</em>,
 * i.e. a non-static member of an enclosing class.
 * @return {@code true} if the supplied class is an inner class
 * @since 5.0.5
 * @see Class#isMemberClass()
 */
public static boolean isInnerClass(Class<?> clazz) {
  return (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isCompilable() {
  return (Modifier.isPublic(this.member.getModifiers()) &&
      Modifier.isPublic(this.member.getDeclaringClass().getModifiers()));
}

代码示例来源:origin: spring-projects/spring-framework

private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
  Assert.notNull(clazz, "Class must not be null");
  int modifiers = clazz.getModifiers();
  return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
}

代码示例来源:origin: spring-projects/spring-framework

public ReflectiveElementFactory(Class<? extends E> elementClass) {
  Assert.notNull(elementClass, "Element class must not be null");
  Assert.isTrue(!elementClass.isInterface(), "Element class must not be an interface type");
  Assert.isTrue(!Modifier.isAbstract(elementClass.getModifiers()), "Element class cannot be an abstract class");
  this.elementClass = elementClass;
}

代码示例来源:origin: spring-projects/spring-framework

private boolean canCreateCopy(Class<?> requiredType) {
  return (!requiredType.isInterface() && !Modifier.isAbstract(requiredType.getModifiers()) &&
      Modifier.isPublic(requiredType.getModifiers()) && ClassUtils.hasConstructor(requiredType));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Make the given constructor accessible, explicitly setting it accessible
 * if necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).
 * @param ctor the constructor to make accessible
 * @see java.lang.reflect.Constructor#setAccessible
 */
@SuppressWarnings("deprecation")  // on JDK 9
public static void makeAccessible(Constructor<?> ctor) {
  if ((!Modifier.isPublic(ctor.getModifiers()) ||
      !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) {
    ctor.setAccessible(true);
  }
}

代码示例来源:origin: google/guava

@Override
 public boolean apply(Class<?> cls) {
  return visibility.isVisible(cls.getModifiers());
 }
};

代码示例来源:origin: spring-projects/spring-framework

/**
 * Make the given method accessible, explicitly setting it accessible if
 * necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).
 * @param method the method to make accessible
 * @see java.lang.reflect.Method#setAccessible
 */
@SuppressWarnings("deprecation")  // on JDK 9
public static void makeAccessible(Method method) {
  if ((!Modifier.isPublic(method.getModifiers()) ||
      !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
    method.setAccessible(true);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean isIndependent() {
  return (!hasEnclosingClass() ||
      (this.introspectedClass.getDeclaringClass() != null &&
          Modifier.isStatic(this.introspectedClass.getModifiers())));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Make the given field accessible, explicitly setting it accessible if
 * necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).
 * @param field the field to make accessible
 * @see java.lang.reflect.Field#setAccessible
 */
@SuppressWarnings("deprecation")  // on JDK 9
public static void makeAccessible(Field field) {
  if ((!Modifier.isPublic(field.getModifiers()) ||
      !Modifier.isPublic(field.getDeclaringClass().getModifiers()) ||
      Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
    field.setAccessible(true);
  }
}

代码示例来源:origin: google/guava

/**
 * Verifies that {@code ctor} produces a {@link NullPointerException} or {@link
 * UnsupportedOperationException} whenever <i>any</i> of its non-nullable parameters are null.
 */
public void testConstructor(Constructor<?> ctor) {
 Class<?> declaringClass = ctor.getDeclaringClass();
 checkArgument(
   Modifier.isStatic(declaringClass.getModifiers())
     || declaringClass.getEnclosingClass() == null,
   "Cannot test constructor of non-static inner class: %s",
   declaringClass.getName());
 Class<?>[] types = ctor.getParameterTypes();
 for (int nullIndex = 0; nullIndex < types.length; nullIndex++) {
  testConstructorParameter(ctor, nullIndex);
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private Class<?> discoverPublicDeclaringClass(Method method, Class<?> clazz) {
  if (Modifier.isPublic(clazz.getModifiers())) {
    try {
      clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
      return clazz;
    }
    catch (NoSuchMethodException ex) {
      // Continue below...
    }
  }
  if (clazz.getSuperclass() != null) {
    return discoverPublicDeclaringClass(method, clazz.getSuperclass());
  }
  return null;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public org.springframework.jmx.export.metadata.ManagedResource getManagedResource(Class<?> beanClass) throws InvalidMetadataException {
  ManagedResource ann = AnnotationUtils.findAnnotation(beanClass, ManagedResource.class);
  if (ann == null) {
    return null;
  }
  Class<?> declaringClass = AnnotationUtils.findAnnotationDeclaringClass(ManagedResource.class, beanClass);
  Class<?> target = (declaringClass != null && !declaringClass.isInterface() ? declaringClass : beanClass);
  if (!Modifier.isPublic(target.getModifiers())) {
    throw new InvalidMetadataException("@ManagedResource class '" + target.getName() + "' must be public");
  }
  org.springframework.jmx.export.metadata.ManagedResource managedResource = new org.springframework.jmx.export.metadata.ManagedResource();
  AnnotationBeanUtils.copyPropertiesToBean(ann, managedResource, this.embeddedValueResolver);
  return managedResource;
}

代码示例来源:origin: google/guava

@Override
public final boolean isOverridable() {
 return !(isFinal()
   || isPrivate()
   || isStatic()
   || Modifier.isFinal(getDeclaringClass().getModifiers()));
}

代码示例来源:origin: google/guava

void doTestNulls(Class<?> cls, Visibility visibility)
  throws ParameterNotInstantiableException, IllegalAccessException, InvocationTargetException,
    FactoryMethodReturnsNullException {
 if (!Modifier.isAbstract(cls.getModifiers())) {
  nullPointerTester.testConstructors(cls, visibility);
 }
 nullPointerTester.testStaticMethods(cls, visibility);
 if (hasInstanceMethodToTestNulls(cls, visibility)) {
  Object instance = instantiate(cls);
  if (instance != null) {
   nullPointerTester.testInstanceMethods(instance, visibility);
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void loadPackagePrivateFactory() {
  List<DummyPackagePrivateFactory> factories =
      SpringFactoriesLoader.loadFactories(DummyPackagePrivateFactory.class, null);
  assertEquals(1, factories.size());
  assertTrue((factories.get(0).getClass().getModifiers() & Modifier.PUBLIC) == 0);
}

相关文章

Class类方法