Example usage for org.eclipse.jdt.core IClassFile isInterface

List of usage examples for org.eclipse.jdt.core IClassFile isInterface

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClassFile isInterface.

Prototype

boolean isInterface() throws JavaModelException;

Source Link

Document

Returns whether this type represents an interface.

Usage

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * Check if java element is an interface or abstract method or a method from
 * interface.//w  w  w .ja v a2  s .  c om
 */
public static boolean isAbstractOrInterface(IJavaElement javaEl) {
    if (javaEl == null) {
        return true;
    }
    boolean abstractOrInterface = false;
    try {
        switch (javaEl.getElementType()) {
        case IJavaElement.CLASS_FILE:
            IClassFile classFile = (IClassFile) javaEl;
            if (isOnClasspath(javaEl)) {
                abstractOrInterface = classFile.isInterface();
            } /*else {
               this is the case for eclipse-generated class files.
               if we do not perform the check in if, then we will have java model
               exception on classFile.isInterface() call.
              }*/
            break;
        case IJavaElement.COMPILATION_UNIT:
            ICompilationUnit cUnit = (ICompilationUnit) javaEl;
            IType type = cUnit.findPrimaryType();
            abstractOrInterface = type != null && type.isInterface();
            break;
        case IJavaElement.TYPE:
            abstractOrInterface = ((IType) javaEl).isInterface();
            break;
        case IJavaElement.METHOD:
            // test for "abstract" flag on method in a class
            abstractOrInterface = Flags.isAbstract(((IMethod) javaEl).getFlags());
            // "abstract" flags could be not exist on interface methods
            if (!abstractOrInterface) {
                IType ancestor = (IType) javaEl.getAncestor(IJavaElement.TYPE);
                abstractOrInterface = ancestor != null && ancestor.isInterface();
            }
            break;
        default:
            IType ancestor1 = (IType) javaEl.getAncestor(IJavaElement.TYPE);
            abstractOrInterface = ancestor1 != null && ancestor1.isInterface();
            break;
        }
    } catch (JavaModelException e) {
        // No point to log it here
        // BytecodeOutlinePlugin.log(e, IStatus.ERROR);
    }
    return abstractOrInterface;
}