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

The following examples show how to use org.eclipse.jdt.core.IType#isEnum() . 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: HierarchyProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected RefactoringStatus checkDeclaringType(final IProgressMonitor monitor) throws JavaModelException {
	try {
		final IType type= getDeclaringType();
		if (type.isEnum())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.HierarchyRefactoring_enum_members);
		if (type.isAnnotation())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.HierarchyRefactoring_annotation_members);
		if (type.isInterface())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.HierarchyRefactoring_interface_members);
		if (type.isBinary())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.HierarchyRefactoring_members_of_binary);
		if (type.isReadOnly())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.HierarchyRefactoring_members_of_read_only);
		return new RefactoringStatus();
	} finally {
		if (monitor != null)
			monitor.done();
	}
}
 
Example 2
Source File: AddUnimplementedConstructorsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void run(IStructuredSelection selection) {
	Shell shell= getShell();
	try {
		IType type= getSelectedType(selection);
		if (type == null) {
			MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_not_applicable);
			return;
		}
		if (type.isAnnotation()) {
			MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_annotation_not_applicable);
			return;
		} else if (type.isInterface()) {
			MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_interface_not_applicable);
			return;
		} else if (type.isEnum()) {
			MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.AddUnimplementedConstructorsAction_enum_not_applicable);
			return;
		}
		run(shell, type, false);
	} catch (CoreException e) {
		ExceptionHandler.handle(e, shell, getDialogTitle(), null);
	}
}
 
Example 3
Source File: GenerateConstructorUsingFieldsSelectionDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public GenerateConstructorUsingFieldsSelectionDialog(Shell parent, ILabelProvider labelProvider, GenerateConstructorUsingFieldsContentProvider contentProvider, CompilationUnitEditor editor, IType type, IMethodBinding[] superConstructors) throws JavaModelException {
	super(parent, labelProvider, contentProvider, editor, type, true);
	fTreeViewerAdapter= new GenerateConstructorUsingFieldsTreeViewerAdapter();

	fSuperConstructors= superConstructors;

	IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings();
	fGenConstructorSettings= dialogSettings.getSection(SETTINGS_SECTION);
	if (fGenConstructorSettings == null) {
		fGenConstructorSettings= dialogSettings.addNewSection(SETTINGS_SECTION);
		fGenConstructorSettings.put(OMIT_SUPER, false);
	}

	final boolean isEnum= type.isEnum();
	fOmitSuper= fGenConstructorSettings.getBoolean(OMIT_SUPER) || isEnum;
	if (isEnum)
		setVisibility(Modifier.PRIVATE);
}
 
Example 4
Source File: CreateFieldOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * By default the new field is positioned after the last existing field
 * declaration, or as the first member in the type if there are no
 * field declarations.
 */
protected void initializeDefaultPosition() {
	IType parentElement = getType();
	try {
		IField[] fields = parentElement.getFields();
		if (fields != null && fields.length > 0) {
			final IField lastField = fields[fields.length - 1];
			if (parentElement.isEnum()) {
				IField field = lastField;
				if (!field.isEnumConstant()) {
					createAfter(lastField);
				}
			} else {
				createAfter(lastField);
			}
		} else {
			IJavaElement[] elements = parentElement.getChildren();
			if (elements != null && elements.length > 0) {
				createBefore(elements[0]);
			}
		}
	} catch (JavaModelException e) {
		// type doesn't exist: ignore
	}
}
 
Example 5
Source File: SourceAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean supportsHashCodeEquals(IInvocationContext context, IType type) {
	try {
		if (type == null || type.isAnnotation() || type.isInterface() || type.isEnum() || type.getCompilationUnit() == null) {
			return false;
		}
		RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
		CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
		ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
		return (typeBinding == null) ? false : Arrays.stream(typeBinding.getDeclaredFields()).filter(f -> !Modifier.isStatic(f.getModifiers())).findAny().isPresent();
	} catch (JavaModelException e) {
		return false;
	}
}
 
Example 6
Source File: SourceAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean supportsGenerateToString(IType type) {
	try {
		if (type == null || type.isAnnotation() || type.isInterface() || type.isEnum() || type.isAnonymous() || type.getCompilationUnit() == null) {
			return false;
		}
	} catch (JavaModelException e) {
		// do nothing.
	}

	return true;
}
 
Example 7
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 8
Source File: GenerateMethodAbstractAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
void checkAndRun(IType type) throws CoreException {
	if (type == null) {
		MessageDialog.openInformation(getShell(), getErrorCaption(),
				ActionMessages.GenerateMethodAbstractAction_error_not_applicable);
		notifyResult(false);
	}
	if (!ElementValidator.check(type, getShell(), getErrorCaption(), false)
			|| ! ActionUtil.isEditable(fEditor, getShell(), type)) {
		notifyResult(false);
		return;
	}
	if (type == null) {
		MessageDialog.openError(getShell(), getErrorCaption(), ActionMessages.GenerateMethodAbstractAction_error_removed_type);
		notifyResult(false);
		return;
	}
	if (type.isAnnotation()) {
		MessageDialog.openInformation(getShell(), getErrorCaption(), ActionMessages.GenerateMethodAbstractAction_annotation_not_applicable);
		notifyResult(false);
		return;
	}
	if (type.isInterface()) {
		MessageDialog.openInformation(getShell(), getErrorCaption(), ActionMessages.GenerateMethodAbstractAction_interface_not_applicable);
		notifyResult(false);
		return;
	}
	if (type.isEnum()) {
		MessageDialog.openInformation(getShell(), getErrorCaption(), ActionMessages.GenerateMethodAbstractAction_enum_not_applicable);
		notifyResult(false);
		return;
	}
	if (type.isAnonymous()) {
		MessageDialog.openError(getShell(), getErrorCaption(), ActionMessages.GenerateMethodAbstractAction_anonymous_type_not_applicable);
		notifyResult(false);
		return;
	}
	run(getShell(), type);
}
 
Example 9
Source File: AddUnimplementedConstructorsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean canEnable(IStructuredSelection selection) throws JavaModelException {
	if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) {
		IType type= (IType) selection.getFirstElement();
		return type.getCompilationUnit() != null && !type.isInterface() && !type.isEnum() && !type.isAnonymous();
	}

	if ((selection.size() == 1) && (selection.getFirstElement() instanceof ICompilationUnit))
		return true;

	return false;
}
 
Example 10
Source File: ClassFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean select(Viewer viewer, Object parent, Object element) {
	if (element instanceof IType) {
		try {
			final IType type= (IType)element;
			return type.isInterface() || type.isEnum();
		} catch (JavaModelException ex) {
			return true;
		}
	}
	return true;
}
 
Example 11
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Determines what kind of AST node was selected, and returns an error status
 * if the kind of node is inappropriate for this refactoring.
 * @param pm
 * @return a RefactoringStatus indicating whether the selection is valid
 * @throws JavaModelException
 */
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
	try {
		pm.beginTask(RefactoringCoreMessages.IntroduceFactory_examiningSelection, 2);

		fSelectedNode= getTargetNode(fCUHandle, fSelectionStart, fSelectionLength);

		if (fSelectedNode == null)
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_notAConstructorInvocation);

		// getTargetNode() must return either a ClassInstanceCreation or a
		// constructor MethodDeclaration; nothing else.
		if (fSelectedNode instanceof ClassInstanceCreation) {
			ClassInstanceCreation classInstanceCreation= (ClassInstanceCreation)fSelectedNode;
			fCtorBinding= classInstanceCreation.resolveConstructorBinding();
		} else if (fSelectedNode instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration= (MethodDeclaration)fSelectedNode;
			fCtorBinding= methodDeclaration.resolveBinding();
		}

		if (fCtorBinding == null)
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unableToResolveConstructorBinding);

		// If this constructor is of a generic type, get the generic version,
		// not some instantiation thereof.
		fCtorBinding= fCtorBinding.getMethodDeclaration();

		pm.worked(1);

		// We don't handle constructors of nested types at the moment
		if (fCtorBinding.getDeclaringClass().isNested())
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unsupportedNestedTypes);

		ITypeBinding	ctorType= fCtorBinding.getDeclaringClass();
		IType			ctorOwningType= (IType) ctorType.getJavaElement();

		if (ctorOwningType.isBinary())
			// Can't modify binary CU; don't know what CU to put factory method
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInBinaryClass);
		if (ctorOwningType.isEnum())
			// Doesn't make sense to encapsulate enum constructors
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInEnum);

		// Put the generated factory method inside the type that owns the constructor
		fFactoryUnitHandle= ctorOwningType.getCompilationUnit();
		fFactoryCU= getASTFor(fFactoryUnitHandle);

		Name	ctorOwnerName= (Name) NodeFinder.perform(fFactoryCU, ctorOwningType.getNameRange());

		fCtorOwningClass= (AbstractTypeDeclaration) ASTNodes.getParent(ctorOwnerName, AbstractTypeDeclaration.class);
		fFactoryOwningClass= fCtorOwningClass;

		pm.worked(1);

		if (fNewMethodName == null)
			return setNewMethodName("create" + fCtorBinding.getName());//$NON-NLS-1$
		else
			return new RefactoringStatus();
	} finally {
		pm.done();
	}
}
 
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();
	}
}
 
Example 13
Source File: DefaultJavaFoldingStructureProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns <code>true</code> if <code>type</code> is an anonymous enum declaration,
 * <code>false</code> otherwise. See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=143276
 *
 * @param type the type to test
 * @return <code>true</code> if <code>type</code> is an anonymous enum declaration
 * @since 3.3
 */
private boolean isAnonymousEnum(IType type) {
	try {
		return type.isEnum() && type.isAnonymous();
	} catch (JavaModelException x) {
		return false; // optimistically
	}
}