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

The following examples show how to use org.eclipse.jdt.core.IType#isAnnotation() . 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: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isPullUpAvailable(IMember member) throws JavaModelException {
	if (!member.exists())
		return false;
	final int type= member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE)
		return false;
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
		return false;
	if (!Checks.isAvailable(member))
		return false;
	if (member instanceof IType) {
		if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member))
			return false;
	}
	if (member instanceof IMethod) {
		final IMethod method= (IMethod) member;
		if (method.isConstructor())
			return false;
		if (JdtFlags.isNative(method))
			return false;
		final IType declaring= method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation())
			return false;
	}
	return true;
}
 
Example 2
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 3
Source File: InterfaceFilter.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 {
			IType type= (IType) element;
			return !type.isInterface() || type.isAnnotation();
		} catch (JavaModelException ex) {
			return true;
		}
	}
	return true;
}
 
Example 4
Source File: GenerateNewConstructorUsingFieldsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IStructuredSelection selection) {
	try {
		IType selectionType= getSelectedType(selection);
		if (selectionType == null) {
			MessageDialog.openInformation(getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_not_applicable);
			notifyResult(false);
			return;
		}

		IField[] selectedFields= getSelectedFields(selection);

		if (canRunOn(selectedFields)) {
			run(selectedFields[0].getDeclaringType(), selectedFields, false);
			return;
		}
		Object firstElement= selection.getFirstElement();

		if (firstElement instanceof IType) {
			run((IType) firstElement, new IField[0], false);
		} else if (firstElement instanceof ICompilationUnit) {
			IType type= ((ICompilationUnit) firstElement).findPrimaryType();
			if (type.isAnnotation()) {
				MessageDialog.openInformation(getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_annotation_not_applicable);
				notifyResult(false);
				return;
			} else if (type.isInterface()) {
				MessageDialog.openInformation(getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_interface_not_applicable);
				notifyResult(false);
				return;
			} else
				run(((ICompilationUnit) firstElement).findPrimaryType(), new IField[0], false);
		}
	} catch (CoreException exception) {
		ExceptionHandler.handle(exception, getShell(), ActionMessages.GenerateConstructorUsingFieldsAction_error_title, ActionMessages.GenerateConstructorUsingFieldsAction_error_actionfailed);
	}
}
 
Example 5
Source File: GenerateNewConstructorUsingFieldsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IField[] getSelectedFields(IStructuredSelection selection) {
	List<?> elements= selection.toList();
	if (elements.size() > 0) {
		IField[] fields= new IField[elements.size()];
		ICompilationUnit unit= null;
		for (int index= 0; index < elements.size(); index++) {
			if (elements.get(index) instanceof IField) {
				IField field= (IField) elements.get(index);
				if (index == 0) {
					// remember the CU of the first element
					unit= field.getCompilationUnit();
					if (unit == null) {
						return null;
					}
				} else if (!unit.equals(field.getCompilationUnit())) {
					// all fields must be in the same CU
					return null;
				}
				try {
					final IType declaringType= field.getDeclaringType();
					if (declaringType.isInterface() || declaringType.isAnnotation() || declaringType.isAnonymous())
						return null;
				} catch (JavaModelException exception) {
					JavaPlugin.log(exception);
					return null;
				}
				fields[index]= field;
			} else {
				return null;
			}
		}
		return fields;
	}
	return null;
}
 
Example 6
Source File: GenerateNewConstructorUsingFieldsAction.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 (getSelectedFields(selection) != null)
		return true;

	if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) {
		IType type= (IType) selection.getFirstElement();
		return type.getCompilationUnit() != null && !type.isInterface() && !type.isAnnotation() && !type.isAnonymous();
	}

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

	return false;
}
 
Example 7
Source File: AddDelegateMethodsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean canRunOn(IType type) throws JavaModelException {
	if (type == null || type.getCompilationUnit() == null) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddDelegateMethodsAction_not_in_source_file);
		return false;
	} else if (type.isAnnotation()) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddDelegateMethodsAction_annotation_not_applicable);
		return false;
	} else if (type.isInterface()) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddDelegateMethodsAction_interface_not_applicable);
		return false;
	}
	return canRunOn(type.getFields());
}
 
Example 8
Source File: AddGetterSetterAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IStructuredSelection selection) {
	try {
		IField[] selectedFields= getSelectedFields(selection);
		if (canRunOn(selectedFields)) {
			run(selectedFields[0].getDeclaringType(), selectedFields, false);
			return;
		}
		Object firstElement= selection.getFirstElement();

		if (firstElement instanceof IType)
			run((IType) firstElement, new IField[0], false);
		else if (firstElement instanceof ICompilationUnit) {
			// http://bugs.eclipse.org/bugs/show_bug.cgi?id=38500
			IType type= ((ICompilationUnit) firstElement).findPrimaryType();
			// type can be null if file has a bad encoding
			if (type == null) {
				MessageDialog.openError(getShell(),
						ActionMessages.AddGetterSetterAction_no_primary_type_title,
						ActionMessages.AddGetterSetterAction_no_primary_type_message);
				notifyResult(false);
				return;
			}
			if (type.isAnnotation()) {
				MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddGetterSetterAction_annotation_not_applicable);
				notifyResult(false);
				return;
			} else if (type.isInterface()) {
				MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddGetterSetterAction_interface_not_applicable);
				notifyResult(false);
				return;
			} else
				run(((ICompilationUnit) firstElement).findPrimaryType(), new IField[0], false);
		}
	} catch (CoreException e) {
		ExceptionHandler.handle(e, getShell(), DIALOG_TITLE, ActionMessages.AddGetterSetterAction_error_actionfailed);
	}

}
 
Example 9
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param fullyQualifiedTypeName the fully qualified name of the intermediary method
 * @return status for type name. Use {@link #setIntermediaryMethodName(String)} to check for overridden methods.
 */
public RefactoringStatus setIntermediaryTypeName(String fullyQualifiedTypeName) {
	IType target= null;

	try {
		if (fullyQualifiedTypeName.length() == 0)
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_type_not_selected_error);

		// find type (now including secondaries)
		target= getProject().findType(fullyQualifiedTypeName, new NullProgressMonitor());
		if (target == null || !target.exists())
			return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_type_does_not_exist_error, BasicElementLabels.getJavaElementName(fullyQualifiedTypeName)));
		if (target.isAnnotation())
			return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_annotation);
		if (target.isInterface() && !(JavaModelUtil.is18OrHigher(target.getJavaProject()) && JavaModelUtil.is18OrHigher(getProject())))
			return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_on_interface);
	} catch (JavaModelException e) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_unable_determine_declaring_type);
	}

	if (target.isReadOnly())
		return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_readonly);

	if (target.isBinary())
		return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_binary);

	fIntermediaryType= target;

	return new RefactoringStatus();
}
 
Example 10
Source File: AnonymousTypeCompletionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Image getImageForType(IType type) {
	String imageName= JavaPluginImages.IMG_OBJS_CLASS; // default
	try {
		if (type.isAnnotation()) {
			imageName= JavaPluginImages.IMG_OBJS_ANNOTATION;
		} else if (type.isInterface()) {
			imageName= JavaPluginImages.IMG_OBJS_INTERFACE;
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
	return JavaPluginImages.get(imageName);
}
 
Example 11
Source File: GenerateGetterSetterOperation.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean supportsGetterSetter(IType type) throws JavaModelException {
	if (type == null || type.isAnnotation() || type.isInterface() || type.getCompilationUnit() == null) {
		return false;
	}

	return true;
}
 
Example 12
Source File: GenerateDelegateMethodsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean supportsGenerateDelegateMethods(IType type) throws JavaModelException {
	if (type == null || type.getCompilationUnit() == null || type.isAnnotation() || type.isInterface()) {
		return false;
	}

	IField[] fields = type.getFields();
	int count = 0;
	for (IField field : fields) {
		if (!JdtFlags.isEnum(field) && !hasPrimitiveType(field) && !isArray(field)) {
			count++;
		}
	}

	return count > 0;
}
 
Example 13
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isPushDownAvailable(final IMember member) throws JavaModelException {
	if (!member.exists()) {
		return false;
	}
	final int type = member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD) {
		return false;
	}
	if (JdtFlags.isEnum(member)) {
		return false;
	}
	if (!Checks.isAvailable(member)) {
		return false;
	}
	if (JdtFlags.isStatic(member)) {
		return false;
	}
	if (type == IJavaElement.METHOD) {
		final IMethod method = (IMethod) member;
		if (method.isConstructor()) {
			return false;
		}
		if (JdtFlags.isNative(method)) {
			return false;
		}
		final IType declaring = method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation()) {
			return false;
		}
	}
	return true;
}
 
Example 14
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isPullUpAvailable(IMember member) throws JavaModelException {
	if (!member.exists()) {
		return false;
	}
	final int type = member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE) {
		return false;
	}
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE) {
		return false;
	}
	if (!Checks.isAvailable(member)) {
		return false;
	}
	if (member instanceof IType) {
		if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member)) {
			return false;
		}
	}
	if (member instanceof IMethod) {
		final IMethod method = (IMethod) member;
		if (method.isConstructor()) {
			return false;
		}
		if (JdtFlags.isNative(method)) {
			return false;
		}
		final IType declaring = method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation()) {
			return false;
		}
	}
	return true;
}
 
Example 15
Source File: SourceAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private Optional<Either<Command, CodeAction>> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind) {
	try {
		if (type == null || type.isAnnotation() || type.isInterface() || type.isAnonymous() || type.getCompilationUnit() == null) {
			return Optional.empty();
		}
	} catch (JavaModelException e) {
		return Optional.empty();
	}

	if (preferenceManager.getClientPreferences().isGenerateConstructorsPromptSupported()) {
		CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type);
		if (status.constructors.length == 0) {
			return Optional.empty();
		}
		if (status.constructors.length == 1 && status.fields.length == 0) {
			TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields);
			return convertToWorkspaceEditAction(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, edit);
		}

		Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params));
		if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) {
			CodeAction codeAction = new CodeAction(ActionMessages.GenerateConstructorsAction_ellipsisLabel);
			codeAction.setKind(kind);
			codeAction.setCommand(command);
			codeAction.setDiagnostics(Collections.emptyList());
			return Optional.of(Either.forRight(codeAction));
		} else {
			return Optional.of(Either.forLeft(command));
		}
	}

	return Optional.empty();
}
 
Example 16
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 17
Source File: AddGetterSetterAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void run(IType type, IField[] preselected, boolean editor) throws CoreException {
	if (type.isAnnotation()) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddGetterSetterAction_annotation_not_applicable);
		notifyResult(false);
		return;
	} else if (type.isInterface()) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddGetterSetterAction_interface_not_applicable);
		notifyResult(false);
		return;
	} else if (type.getCompilationUnit() == null) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddGetterSetterAction_error_not_in_source_file);
		notifyResult(false);
		return;
	}
	if (!ElementValidator.check(type, getShell(), DIALOG_TITLE, editor)) {
		notifyResult(false);
		return;
	}
	if (!ActionUtil.isEditable(getShell(), type)) {
		notifyResult(false);
		return;
	}

	ILabelProvider lp= new AddGetterSetterLabelProvider();
	resetNumEntries();
	Map<IField, GetterSetterEntry[]> entries= createGetterSetterMapping(type);
	if (entries.isEmpty()) {
		MessageDialog.openInformation(getShell(), DIALOG_TITLE, ActionMessages.AddGettSetterAction_typeContainsNoFields_message);
		notifyResult(false);
		return;
	}
	AddGetterSetterContentProvider cp= new AddGetterSetterContentProvider(entries);
	GetterSetterTreeSelectionDialog dialog= new GetterSetterTreeSelectionDialog(getShell(), lp, cp, fEditor, type);
	dialog.setComparator(new JavaElementComparator());
	dialog.setTitle(DIALOG_TITLE);
	String message= ActionMessages.AddGetterSetterAction_dialog_label;
	dialog.setMessage(message);
	dialog.setValidator(createValidator(fNumEntries));
	dialog.setContainerMode(true);
	dialog.setSize(60, 18);
	dialog.setInput(type);

	if (preselected.length > 0) {
		dialog.setInitialSelections(preselected);
		dialog.setExpandedElements(preselected);
	}
	final Set<IField> keySet= new LinkedHashSet<IField>(entries.keySet());
	int dialogResult= dialog.open();
	if (dialogResult == Window.OK) {
		Object[] result= dialog.getResult();
		if (result == null) {
			notifyResult(false);
			return;
		}
		fSort= dialog.getSortOrder();
		fSynchronized= dialog.getSynchronized();
		fFinal= dialog.getFinal();
		fVisibility= dialog.getVisibilityModifier();
		fGenerateComment= dialog.getGenerateComment();
		IField[] getterFields, setterFields, getterSetterFields;
		if (fSort) {
			getterFields= getGetterFields(result, keySet);
			setterFields= getSetterFields(result, keySet);
			getterSetterFields= new IField[0];
		} else {
			getterFields= getGetterOnlyFields(result, keySet);
			setterFields= getSetterOnlyFields(result, keySet);
			getterSetterFields= getGetterSetterFields(result, keySet);
		}
		generate(type, getterFields, setterFields, getterSetterFields, new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(type.getCompilationUnit(), true), dialog.getElementPosition());
	}
	notifyResult(dialogResult == Window.OK);
}
 
Example 18
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isUseSuperTypeAvailable(final IType type) throws JavaModelException {
	return type != null && type.exists() && !type.isAnnotation() && !type.isAnonymous() && !type.isLambda();
}
 
Example 19
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean isUseSuperTypeAvailable(final IType type) throws JavaModelException {
	return type != null && type.exists() && !type.isAnnotation() && !type.isAnonymous() && !type.isLambda();
}
 
Example 20
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean isExtractInterfaceAvailable(final IType type) throws JavaModelException {
	return Checks.isAvailable(type) && !type.isBinary() && !type.isReadOnly() && !type.isAnnotation() && !type.isAnonymous() && !type.isLambda();
}