Java Code Examples for org.eclipse.jdt.core.IJavaElement#TYPE

The following examples show how to use org.eclipse.jdt.core.IJavaElement#TYPE . 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: SelectionRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void acceptLocalTypeParameter(TypeVariableBinding typeVariableBinding) {
	IJavaElement res;
	if(typeVariableBinding.declaringElement instanceof ParameterizedTypeBinding) {
		LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeVariableBinding.declaringElement).genericType();
		res = findLocalElement(localTypeBinding.sourceStart());
	} else {
		SourceTypeBinding typeBinding = (SourceTypeBinding)typeVariableBinding.declaringElement;
		res = findLocalElement(typeBinding.sourceStart());
	}
	if (res != null && res.getElementType() == IJavaElement.TYPE) {
		IType type = (IType) res;
		ITypeParameter typeParameter = type.getTypeParameter(new String(typeVariableBinding.sourceName));
		if (typeParameter.exists()) {
			addElement(typeParameter);
			if(SelectionEngine.DEBUG){
				System.out.print("SELECTION - accept type parameter("); //$NON-NLS-1$
				System.out.print(typeParameter.toString());
				System.out.println(")"); //$NON-NLS-1$
			}
		}
	}
}
 
Example 2
Source File: MoveStaticMembersProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean canMoveToInterface(IMember member, boolean is18OrHigher) throws JavaModelException {
	int flags= member.getFlags();
	switch (member.getElementType()) {
		case IJavaElement.FIELD:
			if (!(Flags.isStatic(flags) && Flags.isFinal(flags)))
				return false;
			if (Flags.isEnum(flags))
				return false;
			VariableDeclarationFragment declaration= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) member, fSource.getRoot());
			if (declaration != null)
				return declaration.getInitializer() != null;
			return false;
		case IJavaElement.TYPE: {
			IType type= (IType) member;
			if (type.isInterface() && !Checks.isTopLevel(type))
				return true;
			return Flags.isStatic(flags);
		}
		case IJavaElement.METHOD: {
			return is18OrHigher && Flags.isStatic(flags);
		}
		default:
			return false;
	}
}
 
Example 3
Source File: HierarchyLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected boolean isInDifferentHierarchyScope(IType type) {
	if (fFilter != null && !fFilter.select(null, null, type)) {
		return true;
	}
	IJavaElement[] input= fHierarchy.getInputElements();
	if (input == null)
		return false;
	for (int i= 0; i < input.length; i++) {
		if (input[i] == null || input[i].getElementType() == IJavaElement.TYPE) {
			return false;
		}
		IJavaElement parent= type.getAncestor(input[i].getElementType());
		if (input[i].getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
			if (parent == null || parent.getElementName().equals(input[i].getElementName())) {
				return false;
			}
		} else if (input[i].equals(parent)) {
			return false;
		}
	}
	return true;
}
 
Example 4
Source File: DOMInitializer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see IDOMNode#getJavaElement
 */
public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException {
	if (parent.getElementType() == IJavaElement.TYPE) {
		int count = 1;
		IDOMNode previousNode = getPreviousNode();
		while (previousNode != null) {
			if (previousNode instanceof DOMInitializer) {
				count++;
			}
			previousNode = previousNode.getPreviousNode();
		}
		return ((IType) parent).getInitializer(count);
	} else {
		throw new IllegalArgumentException(Messages.element_illegalParent);
	}
}
 
Example 5
Source File: JavaElementResourceMapping.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ResourceMapping create(IJavaElement element) {
	switch (element.getElementType()) {
		case IJavaElement.TYPE:
			return create((IType)element);
		case IJavaElement.COMPILATION_UNIT:
			return create((ICompilationUnit)element);
		case IJavaElement.CLASS_FILE:
			return create((IClassFile)element);
		case IJavaElement.PACKAGE_FRAGMENT:
			return create((IPackageFragment)element);
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return create((IPackageFragmentRoot)element);
		case IJavaElement.JAVA_PROJECT:
			return create((IJavaProject)element);
		case IJavaElement.JAVA_MODEL:
			return create((IJavaModel)element);
		default:
			return null;
	}

}
 
Example 6
Source File: JavaMatchFilter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isApplicable(JavaSearchQuery query) {
	QuerySpecification spec= query.getSpecification();
	if (spec instanceof ElementQuerySpecification) {
		ElementQuerySpecification elementSpec= (ElementQuerySpecification) spec;
		IJavaElement element= elementSpec.getElement();
		switch (element.getElementType()) {
			case IJavaElement.TYPE:
			case IJavaElement.METHOD:
			case IJavaElement.FIELD:
			case IJavaElement.PACKAGE_FRAGMENT:
				return true;
			default:
				return false;
		}
	} else if (spec instanceof PatternQuerySpecification) {
		return true;
	}
	return false;
}
 
Example 7
Source File: PackagesView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected IJavaElement findElementToSelect(IJavaElement je) {
	if (je == null)
		return null;

	switch (je.getElementType()) {
		case IJavaElement.PACKAGE_FRAGMENT:
			return je;
		case IJavaElement.COMPILATION_UNIT:
			return ((ICompilationUnit)je).getParent();
		case IJavaElement.CLASS_FILE:
			return ((IClassFile)je).getParent();
		case IJavaElement.TYPE:
			return ((IType)je).getPackageFragment();
		default:
			return findElementToSelect(je.getParent());
	}
}
 
Example 8
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.FIELD:
			return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
		case IJavaElement.IMPORT_CONTAINER:
			return getImportNodes((IImportContainer)element, cuNode);
		case IJavaElement.IMPORT_DECLARATION:
			return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
		case IJavaElement.INITIALIZER:
			return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
		case IJavaElement.METHOD:
			return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
		case IJavaElement.PACKAGE_DECLARATION:
			return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
		case IJavaElement.TYPE:
			return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
		default:
			Assert.isTrue(false, String.valueOf(element.getElementType()));
			return null;
	}
}
 
Example 9
Source File: JavaElementUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String createSignature(IMember member){
	switch (member.getElementType()){
		case IJavaElement.FIELD:
			return createFieldSignature((IField)member);
		case IJavaElement.TYPE:
			return BasicElementLabels.getJavaElementName(((IType)member).getFullyQualifiedName('.'));
		case IJavaElement.INITIALIZER:
			return RefactoringCoreMessages.JavaElementUtil_initializer;
		case IJavaElement.METHOD:
			return createMethodSignature((IMethod)member);
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example 10
Source File: GenericRefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected ITypeParameter transplantHandle(IMember parent, ITypeParameter element) {
	switch (parent.getElementType()) {
		case IJavaElement.TYPE:
			return ((IType) parent).getTypeParameter(element.getElementName());
		case IJavaElement.METHOD:
			return ((IMethod) parent).getTypeParameter(element.getElementName());
		default:
			throw new IllegalStateException(element.toString());
	}
}
 
Example 11
Source File: CreateTypeOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IJavaModelStatus verify() {
	IJavaModelStatus status = super.verify();
	if (!status.isOK())
		return status;
	try {
		IJavaElement parent = getParentElement();
		if (this.anchorElement != null && this.anchorElement.getElementType() == IJavaElement.FIELD
				&& parent.getElementType() == IJavaElement.TYPE && ((IType)parent).isEnum())
			return new JavaModelStatus(IJavaModelStatusConstants.INVALID_SIBLING, this.anchorElement);
	} catch (JavaModelException e) {
		return e.getJavaModelStatus();
	}
	return JavaModelStatus.VERIFIED_OK;
}
 
Example 12
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IJavaElement getAsTypeOrCu(IJavaElement element) {
	//try to get type first
	if (element.getElementType() == IJavaElement.COMPILATION_UNIT || element.getElementType() == IJavaElement.TYPE)
		return element;
	IJavaElement ancestorType= element.getAncestor(IJavaElement.TYPE);
	if (ancestorType != null)
		return ancestorType;
	return ReorgUtils.getCompilationUnit(element);
}
 
Example 13
Source File: JavaElementImageProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean confirmAbstract(IMember element) throws JavaModelException {
	// never show the abstract symbol on interfaces
	if (element.getElementType() == IJavaElement.TYPE) {
		return ! JavaModelUtil.isInterfaceOrAnnotation((IType) element);
	}
	return true;
}
 
Example 14
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
protected void copyToDestination(IJavaElement element, CompilationUnitRewrite targetRewriter, CompilationUnit sourceCuNode, CompilationUnit targetCuNode) throws CoreException {
	final ASTRewrite rewrite= targetRewriter.getASTRewrite();
	switch (element.getElementType()) {
		case IJavaElement.FIELD:
			copyMemberToDestination((IMember) element, targetRewriter, sourceCuNode, targetCuNode, createNewFieldDeclarationNode(((IField) element), rewrite, sourceCuNode));
			break;
		case IJavaElement.IMPORT_CONTAINER:
			copyImportsToDestination((IImportContainer) element, rewrite, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.IMPORT_DECLARATION:
			copyImportToDestination((IImportDeclaration) element, rewrite, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.INITIALIZER:
			copyInitializerToDestination((IInitializer) element, targetRewriter, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.METHOD:
			copyMethodToDestination((IMethod) element, targetRewriter, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.PACKAGE_DECLARATION:
			copyPackageDeclarationToDestination((IPackageDeclaration) element, rewrite, sourceCuNode, targetCuNode);
			break;
		case IJavaElement.TYPE:
			copyTypeToDestination((IType) element, targetRewriter, sourceCuNode, targetCuNode);
			break;

		default:
			Assert.isTrue(false);
	}
}
 
Example 15
Source File: DefaultJavaFoldingStructureProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void update(FoldingStructureComputationContext ctx) {
if (ctx == null)
	return;

Map<JavaProjectionAnnotation, Position> additions= new HashMap<JavaProjectionAnnotation, Position>();
List<JavaProjectionAnnotation> deletions= new ArrayList<JavaProjectionAnnotation>();
List<JavaProjectionAnnotation> updates= new ArrayList<JavaProjectionAnnotation>();

computeFoldingStructure(ctx);
Map<JavaProjectionAnnotation, Position> newStructure= ctx.fMap;
Map<IJavaElement, List<Tuple>> oldStructure= computeCurrentStructure(ctx);

Iterator<JavaProjectionAnnotation> e= newStructure.keySet().iterator();
while (e.hasNext()) {
	JavaProjectionAnnotation newAnnotation= e.next();
	Position newPosition= newStructure.get(newAnnotation);

	IJavaElement element= newAnnotation.getElement();
	/*
	 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130472 and
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=127445 In the presence of syntax
	 * errors, anonymous types may have a source range offset of 0. When such a situation is
	 * encountered, we ignore the proposed folding range: if no corresponding folding range
	 * exists, it is silently ignored; if there *is* a matching folding range, we ignore the
	 * position update and keep the old range, in order to keep the folding structure
	 * stable.
	 */
	boolean isMalformedAnonymousType= newPosition.getOffset() == 0 && element.getElementType() == IJavaElement.TYPE && isInnerType((IType) element);
	List<Tuple> annotations= oldStructure.get(element);
	if (annotations == null) {
		if (!isMalformedAnonymousType)
			additions.put(newAnnotation, newPosition);
	} else {
		Iterator<Tuple> x= annotations.iterator();
		boolean matched= false;
		while (x.hasNext()) {
			Tuple tuple= x.next();
			JavaProjectionAnnotation existingAnnotation= tuple.annotation;
			Position existingPosition= tuple.position;
			if (newAnnotation.isComment() == existingAnnotation.isComment()) {
				boolean updateCollapsedState= ctx.allowCollapsing() && existingAnnotation.isCollapsed() != newAnnotation.isCollapsed();
				if (!isMalformedAnonymousType && existingPosition != null && (!newPosition.equals(existingPosition) || updateCollapsedState)) {
					existingPosition.setOffset(newPosition.getOffset());
					existingPosition.setLength(newPosition.getLength());
					if (updateCollapsedState)
						if (newAnnotation.isCollapsed())
							existingAnnotation.markCollapsed();
						else
							existingAnnotation.markExpanded();
					updates.add(existingAnnotation);
				}
				matched= true;
				x.remove();
				break;
			}
		}
		if (!matched)
			additions.put(newAnnotation, newPosition);

		if (annotations.isEmpty())
			oldStructure.remove(element);
	}
}

Iterator<List<Tuple>> iter= oldStructure.values().iterator();
while (iter.hasNext()) {
	List<Tuple> list= iter.next();
	int size= list.size();
	for (int i= 0; i < size; i++)
		deletions.add(list.get(i).annotation);
}

match(deletions, additions, updates, ctx);

Annotation[] deletedArray= deletions.toArray(new Annotation[deletions.size()]);
Annotation[] changedArray= updates.toArray(new Annotation[updates.size()]);
ctx.getModel().modifyAnnotations(deletedArray, additions, changedArray);

ctx.fScanner.setSource(null);
  }
 
Example 16
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static IType getAncestorType(IJavaElement destinationElement) {
	return destinationElement.getElementType() == IJavaElement.TYPE ? (IType)destinationElement: (IType)destinationElement.getAncestor(IJavaElement.TYPE);
}
 
Example 17
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean canPasteToCu(int elementType) {
	return	elementType == IJavaElement.PACKAGE_DECLARATION ||
			elementType == IJavaElement.TYPE ||
			elementType == IJavaElement.IMPORT_DECLARATION;
}
 
Example 18
Source File: FindBrokenNLSKeysAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean canEnable(IStructuredSelection selection) {
	Object[] selected= selection.toArray();
	for (int i= 0; i < selected.length; i++) {
		try {
			if (selected[i] instanceof IJavaElement) {
				IJavaElement elem= (IJavaElement) selected[i];
				if (elem.exists()) {
					switch (elem.getElementType()) {
						case IJavaElement.TYPE:
							if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
								return true;
							}
							return false;
						case IJavaElement.COMPILATION_UNIT:
							return true;
						case IJavaElement.IMPORT_CONTAINER:
							return false;
						case IJavaElement.PACKAGE_FRAGMENT:
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
							return (root.getKind() == IPackageFragmentRoot.K_SOURCE);
						case IJavaElement.JAVA_PROJECT:
							return true;
					}
				}
			} else if (selected[i] instanceof LogicalPackage) {
				return true;
			} else if (selected[i] instanceof IFile) {
				IFile file= (IFile)selected[i];
				if ("properties".equalsIgnoreCase(file.getFileExtension())) //$NON-NLS-1$
					return true;
			} else if (selected[i] instanceof IWorkingSet) {
				IWorkingSet workingSet= (IWorkingSet) selected[i];
				return IWorkingSetIDs.JAVA.equals(workingSet.getId());
			}
		} catch (JavaModelException e) {
			if (!e.isDoesNotExist()) {
				JavaPlugin.log(e);
			}
		}
	}
	return false;
}
 
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 isExtractSupertypeAvailable(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 IMethod) {
		final IMethod method = (IMethod) member;
		if (method.isConstructor()) {
			return false;
		}
		if (JdtFlags.isNative(method)) {
			return false;
		}
		member = method.getDeclaringType();
	} else if (member instanceof IField) {
		member = member.getDeclaringType();
	}
	if (member instanceof IType) {
		if (JdtFlags.isEnum(member) || JdtFlags.isAnnotation(member)) {
			return false;
		}
		if (member.getDeclaringType() != null && !JdtFlags.isStatic(member)) {
			return false;
		}
		if (((IType) member).isAnonymous()) {
			return false; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=253727
		}
		if (((IType) member).isLambda()) {
			return false;
		}
	}
	return true;
}
 
Example 20
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus initialize(JavaRefactoringArguments extended) {
	final String handle= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
	if (handle != null) {
		final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle, false);
		if (element == null || !element.exists() || element.getElementType() != IJavaElement.TYPE)
			return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getProcessorName(), IJavaRefactorings.RENAME_TYPE);
		else
			fType= (IType) element;
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
	final String name= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
	if (name != null && !"".equals(name)) //$NON-NLS-1$
		setNewElementName(name);
	else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
	final String patterns= extended.getAttribute(ATTRIBUTE_PATTERNS);
	if (patterns != null && !"".equals(patterns)) //$NON-NLS-1$
		fFilePatterns= patterns;
	final String references= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES);
	if (references != null) {
		fUpdateReferences= Boolean.valueOf(references).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES));
	final String matches= extended.getAttribute(ATTRIBUTE_TEXTUAL_MATCHES);
	if (matches != null) {
		fUpdateTextualMatches= Boolean.valueOf(matches).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_TEXTUAL_MATCHES));
	final String qualified= extended.getAttribute(ATTRIBUTE_QUALIFIED);
	if (qualified != null) {
		fUpdateQualifiedNames= Boolean.valueOf(qualified).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_QUALIFIED));
	final String similarDeclarations= extended.getAttribute(ATTRIBUTE_SIMILAR_DECLARATIONS);
	if (similarDeclarations != null)
		fUpdateSimilarElements= Boolean.valueOf(similarDeclarations).booleanValue();
	else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_SIMILAR_DECLARATIONS));
	final String similarDeclarationsMatchingStrategy= extended.getAttribute(ATTRIBUTE_MATCHING_STRATEGY);
	if (similarDeclarationsMatchingStrategy != null) {
		try {
			fRenamingStrategy= Integer.valueOf(similarDeclarationsMatchingStrategy).intValue();
		} catch (NumberFormatException e) {
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new String[] { similarDeclarationsMatchingStrategy,
					ATTRIBUTE_QUALIFIED }));
		}
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_MATCHING_STRATEGY));
	return new RefactoringStatus();
}