Java Code Examples for java.lang.reflect.Modifier#isInterface()

The following examples show how to use java.lang.reflect.Modifier#isInterface() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectionReaderImpl.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a collection of candidate sources -- i.e., any class that has the @SourceType annotation
 */
protected static Collection<Class<? extends ConnectionConf<?, ?>>> getCandidateSources(ScanResult scanResult) {
  ImmutableList.Builder<Class<? extends ConnectionConf<?, ?>>> candidates = new ImmutableList.Builder<>();
  for(Class<?> input : scanResult.getAnnotatedClasses(SourceType.class)) {
    try {
      if (Modifier.isAbstract(input.getModifiers())
        || Modifier.isInterface(input.getModifiers())
        || !ConnectionConf.class.isAssignableFrom(input)) {
        logger.warn("Failure trying to recognize SourceConf for {}. Expected a concrete implementation of SourceConf.", input.getName());
        continue;
      }
    } catch (Exception e) {
      logger.warn("Failure trying to recognize SourceConf for {}", input.getName(), e);
      continue;
    }
    // Check done just above
    candidates.add((Class<? extends ConnectionConf<?, ?>>) input);
  }
  return candidates.build();
}
 
Example 2
Source File: JavaAdapterBytecodeGenerator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void emitSuperCall(final InstructionAdapter mv, final Class<?> owner, final String name, final String methodDesc) {
    mv.visitVarInsn(ALOAD, 0);
    int nextParam = 1;
    final Type methodType = Type.getMethodType(methodDesc);
    for(final Type t: methodType.getArgumentTypes()) {
        mv.load(nextParam, t);
        nextParam += t.getSize();
    }

    // default method - non-abstract, interface method
    if (Modifier.isInterface(owner.getModifiers())) {
        // we should call default method on the immediate "super" type - not on (possibly)
        // the indirectly inherited interface class!
        mv.invokespecial(Type.getInternalName(findInvokespecialOwnerFor(owner)), name, methodDesc, false);
    } else {
        mv.invokespecial(superClassName, name, methodDesc, false);
    }
    mv.areturn(methodType.getReturnType());
}
 
Example 3
Source File: JavaAdapterBytecodeGenerator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void emitSuperCall(final InstructionAdapter mv, final Class<?> owner, final String name, final String methodDesc) {
    mv.visitVarInsn(ALOAD, 0);
    int nextParam = 1;
    final Type methodType = Type.getMethodType(methodDesc);
    for(final Type t: methodType.getArgumentTypes()) {
        mv.load(nextParam, t);
        nextParam += t.getSize();
    }

    // default method - non-abstract, interface method
    if (Modifier.isInterface(owner.getModifiers())) {
        // we should call default method on the immediate "super" type - not on (possibly)
        // the indirectly inherited interface class!
        mv.invokespecial(Type.getInternalName(findInvokespecialOwnerFor(owner)), name, methodDesc, false);
    } else {
        mv.invokespecial(superClassName, name, methodDesc, false);
    }
    mv.areturn(methodType.getReturnType());
}
 
Example 4
Source File: JavaTranslator.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
private static void fillClassDecl(MetaObject.InterfaceInfo[] interfaces,
                                  MetaObject metaObject,
                                  List<ELEMENT> words,
                                  QualifiedTypesMap namesMapper) {
    MetaObject.AnnotationInfo[] annotations = metaObject.getAnnotations();
    for (MetaObject.AnnotationInfo annot : annotations) {
        words.add(new ELEMENT("@" + annot.annotationStr + " \n", TAG.ANNOTATION));
    }

    int mod = metaObject.getModifiers();
    words.add(new ELEMENT(Modifier.toString(mod), TAG.MODIFIER));

    if (!Modifier.isInterface(mod)) {
        words.add(new ELEMENT(" class", TAG.MODIFIER));
    }

    words.add(new ELEMENT(" " + namesMapper.getTypeNull(metaObject.getName()),
            TAG.IDENTIFIER));
    words.add(new ELEMENT(metaObject.getClassGenerics(metaObject.getName()),
            TAG.IDENTIFIER));

    if (metaObject.getSuperclass() != null) {
        words.add(new ELEMENT(" extends ", TAG.MODIFIER));
        words.add(new ELEMENT(namesMapper.getType(metaObject.getSuperclass()),
                TAG.IDENTIFIER));
        words.add(new ELEMENT(metaObject.getSuperclassGenerics(), TAG.IDENTIFIER));
    }

    if (interfaces.length != 0) {
        words.add(new ELEMENT(" implements ", TAG.MODIFIER));
        for (MetaObject.InterfaceInfo iface : interfaces) {
            words.add(new ELEMENT(namesMapper.getType(iface.interfaceStr),
                    TAG.IDENTIFIER));
            words.add(new ELEMENT(iface.genericsStr, TAG.IDENTIFIER));
            words.add(new ELEMENT(", ", TAG.IDENTIFIER));
        }
        words.remove(words.size() - 1);
    }
    words.add(new ELEMENT("\n{", TAG.IDENTIFIER));
}
 
Example 5
Source File: Introspector.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic method for testing that a MBean of a given class can be
 * instantiated by the MBean server.<p>
 * This method checks that:
 * <ul><li>The given class is a concrete class.</li>
 *     <li>The given class exposes at least one public constructor.</li>
 * </ul>
 * If these conditions are not met, throws a NotCompliantMBeanException.
 * @param c The class of the MBean we want to create.
 * @exception NotCompliantMBeanException if the MBean class makes it
 *            impossible to instantiate the MBean from within the
 *            MBeanServer.
 *
 **/
public static void testCreation(Class<?> c)
    throws NotCompliantMBeanException {
    // Check if the class is a concrete class
    final int mods = c.getModifiers();
    if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
        throw new NotCompliantMBeanException("MBean class must be concrete");
    }

    // Check if the MBean has a public constructor
    final Constructor<?>[] consList = c.getConstructors();
    if (consList.length == 0) {
        throw new NotCompliantMBeanException("MBean class must have public constructor");
    }
}
 
Example 6
Source File: UnsafeAllocator.java    From sagetv with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the class can be instantiated by unsafe allocator. If the instance has interface or abstract modifiers
 * throw an {@link java.lang.UnsupportedOperationException}
 * @param c instance of the class to be checked
 */
private static void assertInstantiable(Class<?> c) {
  int modifiers = c.getModifiers();
  if (Modifier.isInterface(modifiers)) {
    throw new UnsupportedOperationException("Interface can't be instantiated! Interface name: " + c.getName());
  }
  if (Modifier.isAbstract(modifiers)) {
    throw new UnsupportedOperationException("Abstract class can't be instantiated! Class name: " + c.getName());
  }
}
 
Example 7
Source File: Introspector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic method for testing that a MBean of a given class can be
 * instantiated by the MBean server.<p>
 * This method checks that:
 * <ul><li>The given class is a concrete class.</li>
 *     <li>The given class exposes at least one public constructor.</li>
 * </ul>
 * If these conditions are not met, throws a NotCompliantMBeanException.
 * @param c The class of the MBean we want to create.
 * @exception NotCompliantMBeanException if the MBean class makes it
 *            impossible to instantiate the MBean from within the
 *            MBeanServer.
 *
 **/
public static void testCreation(Class<?> c)
    throws NotCompliantMBeanException {
    // Check if the class is a concrete class
    final int mods = c.getModifiers();
    if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
        throw new NotCompliantMBeanException("MBean class must be concrete");
    }

    // Check if the MBean has a public constructor
    final Constructor<?>[] consList = c.getConstructors();
    if (consList.length == 0) {
        throw new NotCompliantMBeanException("MBean class must have public constructor");
    }
}
 
Example 8
Source File: Introspector.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Basic method for testing that a MBean of a given class can be
 * instantiated by the MBean server.<p>
 * This method checks that:
 * <ul><li>The given class is a concrete class.</li>
 *     <li>The given class exposes at least one public constructor.</li>
 * </ul>
 * If these conditions are not met, throws a NotCompliantMBeanException.
 * @param c The class of the MBean we want to create.
 * @exception NotCompliantMBeanException if the MBean class makes it
 *            impossible to instantiate the MBean from within the
 *            MBeanServer.
 *
 **/
public static void testCreation(Class<?> c)
    throws NotCompliantMBeanException {
    // Check if the class is a concrete class
    final int mods = c.getModifiers();
    if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
        throw new NotCompliantMBeanException("MBean class must be concrete");
    }

    // Check if the MBean has a public constructor
    final Constructor<?>[] consList = c.getConstructors();
    if (consList.length == 0) {
        throw new NotCompliantMBeanException("MBean class must have public constructor");
    }
}
 
Example 9
Source File: Introspector.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic method for testing that a MBean of a given class can be
 * instantiated by the MBean server.<p>
 * This method checks that:
 * <ul><li>The given class is a concrete class.</li>
 *     <li>The given class exposes at least one public constructor.</li>
 * </ul>
 * If these conditions are not met, throws a NotCompliantMBeanException.
 * @param c The class of the MBean we want to create.
 * @exception NotCompliantMBeanException if the MBean class makes it
 *            impossible to instantiate the MBean from within the
 *            MBeanServer.
 *
 **/
public static void testCreation(Class<?> c)
    throws NotCompliantMBeanException {
    // Check if the class is a concrete class
    final int mods = c.getModifiers();
    if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
        throw new NotCompliantMBeanException("MBean class must be concrete");
    }

    // Check if the MBean has a public constructor
    final Constructor<?>[] consList = c.getConstructors();
    if (consList.length == 0) {
        throw new NotCompliantMBeanException("MBean class must have public constructor");
    }
}
 
Example 10
Source File: KryoSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public T createInstance() {
	if(Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers()) ) {
		return null;
	} else {
		checkKryoInitialized();
		try {
			return kryo.newInstance(type);
		} catch(Throwable e) {
			return null;
		}
	}
}
 
Example 11
Source File: DefaultAggregateRootInternalHandlerProvider.java    From enode with MIT License 4 votes vote down vote up
private boolean isInterfaceOrObjectClass(Class type) {
    return Modifier.isInterface(type.getModifiers()) || type.equals(Object.class);
}
 
Example 12
Source File: ModelRuleInspector.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Validates that the given class is effectively static and has no instance state.
 *
 * @param source the class the validate
 */
public void validate(Class<?> source) throws InvalidModelRuleDeclarationException {

    // TODO - exceptions thrown here should point to some extensive documentation on the concept of class rule sources

    int modifiers = source.getModifiers();

    if (Modifier.isInterface(modifiers)) {
        throw invalid(source, "must be a class, not an interface");
    }
    if (Modifier.isAbstract(modifiers)) {
        throw invalid(source, "class cannot be abstract");
    }

    if (source.getEnclosingClass() != null) {
        if (Modifier.isStatic(modifiers)) {
            if (Modifier.isPrivate(modifiers)) {
                throw invalid(source, "class cannot be private");
            }
        } else {
            throw invalid(source, "enclosed classes must be static and non private");
        }
    }

    Class<?> superclass = source.getSuperclass();
    if (!superclass.equals(Object.class)) {
        throw invalid(source, "cannot have superclass");
    }

    Constructor<?>[] constructors = source.getDeclaredConstructors();
    if (constructors.length > 1) {
        throw invalid(source, "cannot have more than one constructor");
    }

    Constructor<?> constructor = constructors[0];
    if (constructor.getParameterTypes().length > 0) {
        throw invalid(source, "constructor cannot take any arguments");
    }

    Field[] fields = source.getDeclaredFields();
    for (Field field : fields) {
        int fieldModifiers = field.getModifiers();
        if (!field.isSynthetic() && !(Modifier.isStatic(fieldModifiers) && Modifier.isFinal(fieldModifiers))) {
            throw invalid(source, "field " + field.getName() + " is not static final");
        }
    }

}
 
Example 13
Source File: StringUtils.java    From arthas with Apache License 2.0 4 votes vote down vote up
/**
 * 翻译Modifier值
 *
 * @param mod modifier
 * @return 翻译值
 */
public static String modifier(int mod, char splitter) {
    StringBuilder sb = new StringBuilder();
    if (Modifier.isAbstract(mod)) {
        sb.append("abstract").append(splitter);
    }
    if (Modifier.isFinal(mod)) {
        sb.append("final").append(splitter);
    }
    if (Modifier.isInterface(mod)) {
        sb.append("interface").append(splitter);
    }
    if (Modifier.isNative(mod)) {
        sb.append("native").append(splitter);
    }
    if (Modifier.isPrivate(mod)) {
        sb.append("private").append(splitter);
    }
    if (Modifier.isProtected(mod)) {
        sb.append("protected").append(splitter);
    }
    if (Modifier.isPublic(mod)) {
        sb.append("public").append(splitter);
    }
    if (Modifier.isStatic(mod)) {
        sb.append("static").append(splitter);
    }
    if (Modifier.isStrict(mod)) {
        sb.append("strict").append(splitter);
    }
    if (Modifier.isSynchronized(mod)) {
        sb.append("synchronized").append(splitter);
    }
    if (Modifier.isTransient(mod)) {
        sb.append("transient").append(splitter);
    }
    if (Modifier.isVolatile(mod)) {
        sb.append("volatile").append(splitter);
    }
    if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}
 
Example 14
Source File: BaseApplication.java    From simple-robot-core with Apache License 2.0 4 votes vote down vote up
/**
 * 使用一个Class来指定启动器。
 * 如果这个类存在{@link SimpleRobotApplication}注解,则以注解信息为主。
 * 如果不存在,则判断是否为{@link Application}接口的子类。如果是,尝试实例化,否则抛出异常。
 * @param appClass 启动类
 * @param args      参数
 */
public CONTEXT run(Class<?> appClass, String... args){
    SimpleRobotApplication applicationAnno = AnnotationUtils.getAnnotation(appClass, SimpleRobotApplication.class);
    if(applicationAnno == null){
        int modifiers = appClass.getModifiers();
        // interface or abstract
        if(Modifier.isInterface(modifiers) || Modifier.isAbstract(modifiers)){
            throw new RobotRunException(1, appClass + "can not be a simple-robot-application: cannot found @SimpleRobotApplication, and is an interface class or an Abstract class.");
        }
        // is child ?
        if(FieldUtils.isChild(appClass, Application.class)){
            // yes, child.
            try {
                Application<CONFIG> newInstance = (Application<CONFIG>) appClass.newInstance();
                return run(newInstance, args);
            } catch (Exception e) {
                throw new RobotRunException(1, appClass + "can not be a simple-robot-application: cannot get newInstance.", e);
            }
        }else{
            throw new RobotRunException(1, appClass + "can not be a simple-robot-application: cannot found @SimpleRobotApplication, and not implement Application interface.");

        }
    }else{
        // has annotation
        Class<?> application = applicationAnno.application();
        if(application.equals(Application.class)){
            application = appClass;
        }

        // get configuration
        SimpleRobotConfiguration configAnnotation = AnnotationUtils.getAnnotation(appClass, SimpleRobotConfiguration.class);
        CONFIG conf = getConf();
        Class<CONFIG> confClass = (Class<CONFIG>) conf.getClass();

        AutoResourceApplication<CONFIG> autoResourceApplication = AutoResourceApplication.autoConfig(confClass, applicationAnno, configAnnotation, application);

        // 正常启动
        return run(autoResourceApplication, args);
    }

}
 
Example 15
Source File: ClassInfo.java    From PATDroid with Apache License 2.0 4 votes vote down vote up
/**
 * @return if the class is an interface
 */
public boolean isInterface() {
    return Modifier.isInterface(mutableDetail.accessFlags);
}
 
Example 16
Source File: ClassUtils.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public static boolean isInterface(Class<?> clazz) {
    return Modifier.isInterface( clazz.getModifiers() );
}
 
Example 17
Source File: ClassDocImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return true if this is a class, not an interface.
 */
@Override
public boolean isClass() {
    return !Modifier.isInterface(getModifiers());
}
 
Example 18
Source File: AnalyzedClass.java    From glowroot with Apache License 2.0 4 votes vote down vote up
boolean isInterface() {
    return Modifier.isInterface(modifiers());
}
 
Example 19
Source File: GridUriDeploymentFileProcessor.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Check that class may be instantiated as {@link org.apache.ignite.compute.ComputeTask} and used
 * in deployment.
 *
 * Loaded task class must implement interface {@link org.apache.ignite.compute.ComputeTask}.
 * Only non-abstract, non-interfaces and public classes allowed.
 * Inner static classes also allowed for loading.
 *
 * @param cls Class to check
 * @return {@code true} if class allowed for deployment.
 */
private static boolean isAllowedTaskClass(Class<?> cls) {
    if (!ComputeTask.class.isAssignableFrom(cls))
        return false;

    int modifiers = cls.getModifiers();

    return !Modifier.isAbstract(modifiers) && !Modifier.isInterface(modifiers) &&
        (!cls.isMemberClass() || Modifier.isStatic(modifiers)) && Modifier.isPublic(modifiers);
}
 
Example 20
Source File: ReflectionUtils.java    From Taira with MIT License 2 votes vote down vote up
/**
 * check interface or abstract class
 *
 * @return true if abstract or interface
 */
static boolean isInterfaceOrAbstract(Class clazz) {
    return clazz != null && (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(
        clazz.getModifiers()));
}