Java Code Examples for org.eclipse.jdt.core.IType#isClass()

The following examples show how to use org.eclipse.jdt.core.IType#isClass() . 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: ResolveMainMethodHandler.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
private static List<IType> getPotentialMainClassTypesInChildren(IType type) throws JavaModelException {
    IType[] children = type.getTypes();
    if (children.length == 0) {
        return Collections.emptyList();
    }

    List<IType> result = new ArrayList<>();
    for (IType child : children) {
        // main method can only exist in the static class or top level class.
        if (child.isClass() && Flags.isStatic(child.getFlags())) {
            result.add(child);
            result.addAll(getPotentialMainClassTypesInChildren(child));
        }
    }

    return result;
}
 
Example 2
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isExtractSupertypeAvailable(final IMember[] members) throws JavaModelException {
	if (members != null && members.length != 0) {
		final IType type = getTopLevelType(members);
		if (type != null && !type.isClass()) {
			return false;
		}
		for (int index = 0; index < members.length; index++) {
			if (!isExtractSupertypeAvailable(members[index])) {
				return false;
			}
		}
		return members.length == 1 || isCommonDeclaringType(members);
	}
	return false;
}
 
Example 3
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isExtractClassAvailable(IType type) throws JavaModelException {
	if (type == null) {
		return false;
	}
	if (!type.exists()) {
		return false;
	}
	return ReorgUtils.isInsideCompilationUnit(type) && type.isClass() && !type.isAnonymous() && !type.isLambda();
}
 
Example 4
Source File: ExtractSupertypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public IType[] getCandidateTypes(final RefactoringStatus status, final IProgressMonitor monitor) {
	Assert.isNotNull(monitor);
	if (fPossibleCandidates == null || fPossibleCandidates.length == 0) {
		final IType declaring= getDeclaringType();
		if (declaring != null) {
			try {
				monitor.beginTask(RefactoringCoreMessages.ExtractSupertypeProcessor_computing_possible_types, 10);
				final IType superType= getDeclaringSuperTypeHierarchy(new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)).getSuperclass(declaring);
				if (superType != null) {
					fPossibleCandidates= superType.newTypeHierarchy(fOwner, new SubProgressMonitor(monitor, 9, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)).getSubtypes(superType);
					final LinkedList<IType> list= new LinkedList<IType>(Arrays.asList(fPossibleCandidates));
					final Set<String> names= new HashSet<String>();
					for (final Iterator<IType> iterator= list.iterator(); iterator.hasNext();) {
						final IType type= iterator.next();
						if (type.isReadOnly() || type.isBinary() || type.isAnonymous() || !type.isClass() || names.contains(type.getFullyQualifiedName()))
							iterator.remove();
						else
							names.add(type.getFullyQualifiedName());
					}
					fPossibleCandidates= list.toArray(new IType[list.size()]);
				}
			} catch (JavaModelException exception) {
				JavaPlugin.log(exception);
			} finally {
				monitor.done();
			}
		}
	}
	return fPossibleCandidates;
}
 
Example 5
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isExtractSupertypeAvailable(final IMember[] members) throws JavaModelException {
	if (members != null && members.length != 0) {
		final IType type= getTopLevelType(members);
		if (type != null && !type.isClass())
			return false;
		for (int index= 0; index < members.length; index++) {
			if (!isExtractSupertypeAvailable(members[index]))
				return false;
		}
		return members.length == 1 || isCommonDeclaringType(members);
	}
	return false;
}
 
Example 6
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isExtractClassAvailable(IType type) throws JavaModelException {
	if (type == null)
		return false;
	if (!type.exists())
		return false;
	return ReorgUtils.isInsideCompilationUnit(type) && type.isClass() && !type.isAnonymous()  && !type.isLambda();
}
 
Example 7
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isException(IType iType, IProgressMonitor pm) throws JavaModelException {
	try{
		if (! iType.isClass())
			return false;
		IType[] superTypes= iType.newSupertypeHierarchy(pm).getAllSupertypes(iType);
		for (int i= 0; i < superTypes.length; i++) {
			if ("java.lang.Throwable".equals(superTypes[i].getFullyQualifiedName())) //$NON-NLS-1$
				return true;
		}
		return false;
	} finally{
		pm.done();
	}
}
 
Example 8
Source File: CallHierarchy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isPossibleInputElement(Object element){
  	if (! (element instanceof IMember))
  		return false;

  	if (element instanceof IType) {
	IType type= (IType) element;
	try {
		return type.isClass() || type.isEnum();
	} catch (JavaModelException e) {
		return false;
	}
}

  	return true;
  }
 
Example 9
Source File: PotentialProgrammingProblemsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addTypes(IType[] allSubtypes, HashSet<ICompilationUnit> cus, List<IType> types) throws JavaModelException {
	for (int i= 0; i < allSubtypes.length; i++) {
		IType type= allSubtypes[i];

		IField field= type.getField(NAME_FIELD);
		if (!field.exists()) {
			if (type.isClass() && cus.contains(type.getCompilationUnit())){
				types.add(type);
			}
		}
	}
}
 
Example 10
Source File: PotentialProgrammingProblemsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void collectChildrenWithMissingSerialVersionId(IJavaElement[] children, IType serializable, List<IType> result) throws JavaModelException {
	for (int i= 0; i < children.length; i++) {
		IJavaElement child= children[i];
		if (child instanceof IType) {
			IType type= (IType)child;

			if (type.isClass()) {
  					IField field= type.getField(NAME_FIELD);
  					if (!field.exists()) {
  						ITypeHierarchy hierarchy= type.newSupertypeHierarchy(new NullProgressMonitor());
  						IType[] interfaces= hierarchy.getAllSuperInterfaces(type);
  						for (int j= 0; j < interfaces.length; j++) {
  							if (interfaces[j].equals(serializable)) {
  								result.add(type);
  								break;
  							}
  						}
				}
			}

			collectChildrenWithMissingSerialVersionId(type.getChildren(), serializable, result);
		} else if (child instanceof IMethod) {
			collectChildrenWithMissingSerialVersionId(((IMethod)child).getChildren(), serializable, result);
		} else if (child instanceof IField) {
			collectChildrenWithMissingSerialVersionId(((IField)child).getChildren(), serializable, result);
		}
	}
}
 
Example 11
Source File: GenerateMethodAbstractAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Can this action be enabled on the specified selection?
 * 
 * @param selection the selection to test
 * @return <code>true</code> if it can be enabled, <code>false</code>
 *         otherwise
 * @throws JavaModelException if the kind of the selection cannot be
 *             determined
 */
boolean canEnable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.size() == 1) {
		final Object element= selection.getFirstElement();
		if (element instanceof IType) {
			final IType type= (IType) element;
			return type.getCompilationUnit() != null && type.isClass();
		}
		if (element instanceof ICompilationUnit)
			return true;
	}
	return false;
}
 
Example 12
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendTypeDeclaration(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
		if (type.isAnnotation()) {
			appendFlags(type);
			fBuffer.append(" @interface "); //$NON-NLS-1$
			fBuffer.append(type.getElementName());
			fBuffer.append("{\n"); //$NON-NLS-1$
			appendMembers(type, new SubProgressMonitor(monitor, 1));
			fBuffer.append("}"); //$NON-NLS-1$
		} else if (type.isInterface()) {
			appendFlags(type);
			fBuffer.append(" interface "); //$NON-NLS-1$
			fBuffer.append(type.getElementName());
			appendTypeParameters(type.getTypeParameters());
			appendSuperInterfaceTypes(type);
			fBuffer.append("{\n"); //$NON-NLS-1$
			appendMembers(type, new SubProgressMonitor(monitor, 1));
			fBuffer.append("}"); //$NON-NLS-1$
		} else if (type.isClass()) {
			appendFlags(type);
			fBuffer.append(" class "); //$NON-NLS-1$
			fBuffer.append(type.getElementName());
			appendTypeParameters(type.getTypeParameters());
			final String signature= type.getSuperclassTypeSignature();
			if (signature != null) {
				fBuffer.append(" extends "); //$NON-NLS-1$
				fBuffer.append(Signature.toString(signature));
			}
			appendSuperInterfaceTypes(type);
			fBuffer.append("{\n"); //$NON-NLS-1$
			appendMembers(type, new SubProgressMonitor(monitor, 1));
			fBuffer.append("}"); //$NON-NLS-1$
		} else if (type.isEnum()) {
			appendFlags(type);
			fBuffer.append(" enum "); //$NON-NLS-1$
			fBuffer.append(type.getElementName());
			appendSuperInterfaceTypes(type);
			fBuffer.append("{\n"); //$NON-NLS-1$
			appendEnumConstants(type);
			appendMembers(type, new SubProgressMonitor(monitor, 1));
			fBuffer.append("}"); //$NON-NLS-1$
		}
	} finally {
		monitor.done();
	}
}