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

The following examples show how to use org.eclipse.jdt.core.IJavaElement#INITIALIZER . 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: 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 2
Source File: MethodReferencesSearchRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void acceptSearchMatch(SearchMatch match) {
       if (fRequireExactMatch && (match.getAccuracy() != SearchMatch.A_ACCURATE)) {
           return;
       }

       if (match.isInsideDocComment()) {
           return;
       }

       if (match.getElement() != null && match.getElement() instanceof IMember) {
           IMember member= (IMember) match.getElement();
           switch (member.getElementType()) {
               case IJavaElement.METHOD:
               case IJavaElement.TYPE:
               case IJavaElement.FIELD:
               case IJavaElement.INITIALIZER:
                   fSearchResults.addMember(member, member, match.getOffset(), match.getOffset()+match.getLength());
                   break;
           }
       }
   }
 
Example 3
Source File: GenericRefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
protected IType transplantHandle(IJavaElement parent, IType element) {
	switch (parent.getElementType()) {
		case IJavaElement.COMPILATION_UNIT:
			return ((ICompilationUnit) parent).getType(element.getElementName());
		case IJavaElement.CLASS_FILE:
			if (parent instanceof IOrdinaryClassFile) {
				return ((IOrdinaryClassFile) parent).getType();
			}
			throw new IllegalStateException(element.toString());
		case IJavaElement.METHOD:
			return ((IMethod) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.FIELD:
			return ((IField) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.INITIALIZER:
			return ((IInitializer) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.TYPE:
			return ((IType) parent).getType(element.getElementName(), element.getOccurrenceCount());
		default:
			throw new IllegalStateException(element.toString());
	}
}
 
Example 4
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates if a member (possible from another package) is visible from
 * elements in a package.
 * @param member The member to test the visibility for
 * @param pack The package in focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisible(IMember member, IPackageFragment pack) throws JavaModelException {

	int type= member.getElementType();
	if  (type == IJavaElement.INITIALIZER ||  (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
		return false;
	}

	int otherflags= member.getFlags();
	IType declaringType= member.getDeclaringType();
	if (Flags.isPublic(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) {
		return true;
	} else if (Flags.isPrivate(otherflags)) {
		return false;
	}

	IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	return (pack != null && otherpack != null && isSamePackage(pack, otherpack));
}
 
Example 5
Source File: RenameElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see MultiOperation
 */
protected void verify(IJavaElement element) throws JavaModelException {
	if (element == null || !element.exists())
		error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);

	if (element.isReadOnly())
		error(IJavaModelStatusConstants.READ_ONLY, element);

	if (!(element instanceof ISourceReference))
		error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);

	int elementType = element.getElementType();
	if (elementType < IJavaElement.TYPE || elementType == IJavaElement.INITIALIZER)
		error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);

	verifyRenaming(element);
}
 
Example 6
Source File: MethodsLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType getDefiningType(Object element) throws JavaModelException {
	int kind= ((IJavaElement) element).getElementType();

	if (kind != IJavaElement.METHOD && kind != IJavaElement.FIELD && kind != IJavaElement.INITIALIZER) {
		return null;
	}
	IType declaringType= ((IMember) element).getDeclaringType();
	if (kind != IJavaElement.METHOD) {
		return declaringType;
	}
	ITypeHierarchy hierarchy= fHierarchy.getHierarchy();
	if (hierarchy == null) {
		return declaringType;
	}
	IMethod method= (IMethod) element;
	MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy);
	IMethod res= tester.findDeclaringMethod(method, true);
	if (res == null || method.equals(res)) {
		return declaringType;
	}
	return res.getDeclaringType();
}
 
Example 7
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected IType transplantHandle(IJavaElement parent, IType element) {
	switch (parent.getElementType()) {
		case IJavaElement.COMPILATION_UNIT:
			return ((ICompilationUnit) parent).getType(element.getElementName());
		case IJavaElement.CLASS_FILE:
			return ((IClassFile) parent).getType();
		case IJavaElement.METHOD:
			return ((IMethod) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.FIELD:
			return ((IField) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.INITIALIZER:
			return ((IInitializer) parent).getType(element.getElementName(), element.getOccurrenceCount());
		case IJavaElement.TYPE:
			return ((IType) parent).getType(element.getElementName(), element.getOccurrenceCount());
		default:
			throw new IllegalStateException(element.toString());
	}
}
 
Example 8
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.
 * @param member The member to test the visibility for
 * @param pack The package of the focus element focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisibleInHierarchy(IMember member, IPackageFragment pack) throws JavaModelException {
	int type= member.getElementType();
	if  (type == IJavaElement.INITIALIZER ||  (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
		return false;
	}

	int otherflags= member.getFlags();

	IType declaringType= member.getDeclaringType();
	if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) {
		return true;
	} else if (Flags.isPrivate(otherflags)) {
		return false;
	}

	IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
	return (pack != null && pack.equals(otherpack));
}
 
Example 9
Source File: PasteAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ASTNode createNewNodeToInsertToType(TypedSource source, ASTRewrite rewrite) {
	switch(source.getType()){
		case IJavaElement.TYPE:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.TYPE_DECLARATION);
		case IJavaElement.METHOD:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.METHOD_DECLARATION);
		case IJavaElement.FIELD:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.FIELD_DECLARATION);
		case IJavaElement.INITIALIZER:
			return rewrite.createStringPlaceholder(source.getSource(), ASTNode.INITIALIZER);
		default: Assert.isTrue(false);
			return null;
	}
}
 
Example 10
Source File: JavaElementUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.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 11
Source File: CreateInitializerOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see CreateElementInCUOperation#generateResultHandle
 */
protected IJavaElement generateResultHandle() {
	try {
		//update the children to be current
		getType().getCompilationUnit().close();
		if (this.anchorElement == null) {
			return getType().getInitializer(this.numberOfInitializers);
		} else {
			IJavaElement[] children = getType().getChildren();
			int count = 0;
			for (int i = 0; i < children.length; i++) {
				IJavaElement child = children[i];
				if (child.equals(this.anchorElement)) {
					if (child .getElementType() == IJavaElement.INITIALIZER && this.insertionPolicy == CreateElementInCUOperation.INSERT_AFTER) {
						count++;
					}
					return getType().getInitializer(count);
				} else
					if (child.getElementType() == IJavaElement.INITIALIZER) {
						count++;
					}
			}
		}
	} catch (JavaModelException e) {
		// type doesn't exist: ignore
	}
	return null;
}
 
Example 12
Source File: TypedSource.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean canCreateForType(int type){
	return 		type == IJavaElement.FIELD
			|| 	type == IJavaElement.TYPE
			|| 	type == IJavaElement.IMPORT_CONTAINER
			|| 	type == IJavaElement.IMPORT_DECLARATION
			|| 	type == IJavaElement.INITIALIZER
			|| 	type == IJavaElement.METHOD
			|| 	type == IJavaElement.PACKAGE_DECLARATION;
}
 
Example 13
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 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: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public final IJavaElement transplantHandle(IJavaElement element) {
	IJavaElement parent= element.getParent();
	if (parent != null)
		parent= transplantHandle(parent); // recursive

	switch (element.getElementType()) {
		case IJavaElement.JAVA_MODEL:
			return transplantHandle((IJavaModel) element);

		case IJavaElement.JAVA_PROJECT:
			return transplantHandle((IJavaProject) element);

		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element);

		case IJavaElement.PACKAGE_FRAGMENT:
			return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element);

		case IJavaElement.COMPILATION_UNIT:
			return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element);

		case IJavaElement.CLASS_FILE:
			return transplantHandle((IPackageFragment) parent, (IClassFile) element);

		case IJavaElement.TYPE:
			return transplantHandle(parent, (IType) element);

		case IJavaElement.FIELD:
			return transplantHandle((IType) parent, (IField) element);

		case IJavaElement.METHOD:
			return transplantHandle((IType) parent, (IMethod) element);

		case IJavaElement.INITIALIZER:
			return transplantHandle((IType) parent, (IInitializer) element);

		case IJavaElement.PACKAGE_DECLARATION:
			return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element);

		case IJavaElement.IMPORT_CONTAINER:
			return transplantHandle((ICompilationUnit) parent, (IImportContainer) element);

		case IJavaElement.IMPORT_DECLARATION:
			return transplantHandle((IImportContainer) parent, (IImportDeclaration) element);

		case IJavaElement.LOCAL_VARIABLE:
			return transplantHandle((ILocalVariable) element);

		case IJavaElement.TYPE_PARAMETER:
			return transplantHandle((IMember) parent, (ITypeParameter) element);

		case IJavaElement.ANNOTATION:
			return transplantHandle((IAnnotatable) parent, (IAnnotation) element);

		default:
			throw new IllegalArgumentException(element.toString());
	}

}
 
Example 16
Source File: OpenTypeHierarchyUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Converts the input to a possible input candidates
 * @param input input
 * @return the possible candidates
 */
public static IJavaElement[] getCandidates(Object input) {
	if (!(input instanceof IJavaElement)) {
		return null;
	}
	try {
		IJavaElement elem= (IJavaElement) input;
		switch (elem.getElementType()) {
			case IJavaElement.INITIALIZER:
			case IJavaElement.METHOD:
			case IJavaElement.FIELD:
			case IJavaElement.TYPE:
			case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			case IJavaElement.JAVA_PROJECT:
				return new IJavaElement[] { elem };
			case IJavaElement.PACKAGE_FRAGMENT:
				if (((IPackageFragment)elem).containsJavaResources())
					return new IJavaElement[] {elem};
				break;
			case IJavaElement.PACKAGE_DECLARATION:
				return new IJavaElement[] { elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT) };
			case IJavaElement.IMPORT_DECLARATION:
				IImportDeclaration decl= (IImportDeclaration) elem;
				if (decl.isOnDemand()) {
					elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
				} else {
					elem= elem.getJavaProject().findType(elem.getElementName());
				}
				if (elem == null)
					return null;
				return new IJavaElement[] {elem};

			case IJavaElement.CLASS_FILE:
				return new IJavaElement[] { ((IClassFile)input).getType() };
			case IJavaElement.COMPILATION_UNIT: {
				ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
				if (cu != null) {
					IType[] types= cu.getTypes();
					if (types.length > 0) {
						return types;
					}
				}
				break;
			}
			default:
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
	return null;
}
 
Example 17
Source File: ProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Computes the adornment flags for the given element.
 *
 * @param obj the element to compute the flags for
 *
 * @return the adornment flags
 */
protected int computeAdornmentFlags(Object obj) {
	try {
		if (obj instanceof IJavaElement) {
			IJavaElement element= (IJavaElement) obj;
			int type= element.getElementType();
			switch (type) {
				case IJavaElement.JAVA_MODEL:
				case IJavaElement.JAVA_PROJECT:
				case IJavaElement.PACKAGE_FRAGMENT_ROOT:
					int flags= getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_INFINITE, null);
					switch (type) {
						case IJavaElement.PACKAGE_FRAGMENT_ROOT:
							IPackageFragmentRoot root= (IPackageFragmentRoot) element;
							if (flags != ERRORTICK_ERROR && root.getKind() == IPackageFragmentRoot.K_SOURCE && isIgnoringOptionalProblems(root.getRawClasspathEntry())) {
								flags= ERRORTICK_IGNORE_OPTIONAL_PROBLEMS;
							}
							break;
						case IJavaElement.JAVA_PROJECT:
							IJavaProject project= (IJavaProject) element;
							if (flags != ERRORTICK_ERROR && flags != ERRORTICK_BUILDPATH_ERROR && isIgnoringOptionalProblems(project)) {
								flags= ERRORTICK_IGNORE_OPTIONAL_PROBLEMS;
							}
							break;
					}
					return flags;
				case IJavaElement.PACKAGE_FRAGMENT:
					return getPackageErrorTicksFromMarkers((IPackageFragment) element);
				case IJavaElement.COMPILATION_UNIT:
				case IJavaElement.CLASS_FILE:
					return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_ONE, null);
				case IJavaElement.PACKAGE_DECLARATION:
				case IJavaElement.IMPORT_DECLARATION:
				case IJavaElement.IMPORT_CONTAINER:
				case IJavaElement.TYPE:
				case IJavaElement.INITIALIZER:
				case IJavaElement.METHOD:
				case IJavaElement.FIELD:
				case IJavaElement.LOCAL_VARIABLE:
					ICompilationUnit cu= (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
					if (cu != null) {
						ISourceReference ref= (type == IJavaElement.COMPILATION_UNIT) ? null : (ISourceReference) element;
						// The assumption is that only source elements in compilation unit can have markers
						IAnnotationModel model= isInJavaAnnotationModel(cu);
						int result= 0;
						if (model != null) {
							// open in Java editor: look at annotation model
							result= getErrorTicksFromAnnotationModel(model, ref);
						} else {
							result= getErrorTicksFromMarkers(cu.getResource(), IResource.DEPTH_ONE, ref);
						}
						fCachedRange= null;
						return result;
					}
					break;
				default:
			}
		} else if (obj instanceof IResource) {
			return getErrorTicksFromMarkers((IResource) obj, IResource.DEPTH_INFINITE, null);
		}
	} catch (CoreException e) {
		if (e instanceof JavaModelException) {
			if (((JavaModelException) e).isDoesNotExist()) {
				return 0;
			}
		}
		if (e.getStatus().getCode() == IResourceStatus.MARKER_NOT_FOUND) {
			return 0;
		}

		JavaPlugin.log(e);
	}
	return 0;
}
 
Example 18
Source File: JavaElementLabelComposer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Appends the label for a Java element with the flags as defined by this class.
 *
 * @param element the element to render
 * @param flags the rendering flags.
 */
public void appendElementLabel(IJavaElement element, long flags) {
	int type= element.getElementType();
	IPackageFragmentRoot root= null;

	if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) {
		root= JavaModelUtil.getPackageFragmentRoot(element);
	}
	if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) {
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
	}

	switch (type) {
	case IJavaElement.METHOD:
		appendMethodLabel((IMethod) element, flags);
		break;
	case IJavaElement.FIELD:
		appendFieldLabel((IField) element, flags);
		break;
	case IJavaElement.LOCAL_VARIABLE:
		appendLocalVariableLabel((ILocalVariable) element, flags);
		break;
	case IJavaElement.TYPE_PARAMETER:
		appendTypeParameterLabel((ITypeParameter) element, flags);
		break;
	case IJavaElement.INITIALIZER:
		appendInitializerLabel((IInitializer) element, flags);
		break;
	case IJavaElement.TYPE:
		appendTypeLabel((IType) element, flags);
		break;
	case IJavaElement.CLASS_FILE:
		appendClassFileLabel((IClassFile) element, flags);
		break;
	case IJavaElement.COMPILATION_UNIT:
		appendCompilationUnitLabel((ICompilationUnit) element, flags);
		break;
	case IJavaElement.PACKAGE_FRAGMENT:
		appendPackageFragmentLabel((IPackageFragment) element, flags);
		break;
	case IJavaElement.PACKAGE_FRAGMENT_ROOT:
		appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags);
		break;
	case IJavaElement.IMPORT_CONTAINER:
	case IJavaElement.IMPORT_DECLARATION:
	case IJavaElement.PACKAGE_DECLARATION:
		appendDeclarationLabel(element, flags);
		break;
	case IJavaElement.JAVA_PROJECT:
	case IJavaElement.JAVA_MODEL:
		fBuilder.append(element.getElementName());
		break;
	default:
		fBuilder.append(element.getElementName());
	}

	if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) {
		fBuilder.append(JavaElementLabels.CONCAT_STRING);
		appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED);
	}
}
 
Example 19
Source File: HandleFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create handle by adding child to parent obtained by recursing into parent scopes.
 */
public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) {
	IJavaElement newElement = (IJavaElement)knownScopes.get(scope);
	if (newElement != null) return newElement;

	switch(scope.kind) {
		case Scope.COMPILATION_UNIT_SCOPE :
			newElement = unit;
			break;
		case Scope.CLASS_SCOPE :
			IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
			switch (parentElement.getElementType()) {
				case IJavaElement.COMPILATION_UNIT :
					newElement = ((ICompilationUnit)parentElement).getType(new String(scope.enclosingSourceType().sourceName));
					break;
				case IJavaElement.TYPE :
					newElement = ((IType)parentElement).getType(new String(scope.enclosingSourceType().sourceName));
					break;
				case IJavaElement.FIELD :
				case IJavaElement.INITIALIZER :
				case IJavaElement.METHOD :
				    IMember member = (IMember)parentElement;
				    if (member.isBinary()) {
				        return null;
				    } else {
						newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1);
						// increment occurrence count if collision is detected
						if (newElement != null) {
							while (!existingElements.add(newElement)) ((SourceRefElement)newElement).occurrenceCount++;
						}
				    }
					break;
			}
			if (newElement != null) {
				knownScopes.put(scope, newElement);
			}
			break;
		case Scope.METHOD_SCOPE :
			if (scope.isLambdaScope()) {
				parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
				LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext();
				if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && 
						!(expression.descriptor instanceof ProblemMethodBinding)) { // chain in lambda element only if resolved properly.
					//newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression).getMethod();
					newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression).getMethod();
					knownScopes.put(scope, newElement);
					return newElement;
				}
				return parentElement;
			}
			IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
			MethodScope methodScope = (MethodScope) scope;
			if (methodScope.isInsideInitializer()) {
				// inside field or initializer, must find proper one
				TypeDeclaration type = methodScope.referenceType();
				int occurenceCount = 1;
				int length = type.fields == null ? 0 : type.fields.length;
				for (int i = 0; i < length; i++) {
					FieldDeclaration field = type.fields[i];
					if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) {
						switch (field.getKind()) {
							case AbstractVariableDeclaration.FIELD :
							case AbstractVariableDeclaration.ENUM_CONSTANT :
								newElement = parentType.getField(new String(field.name));
								break;
							case AbstractVariableDeclaration.INITIALIZER :
								newElement = parentType.getInitializer(occurenceCount);
								break;
						}
						break;
					} else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) {
						occurenceCount++;
					}
				}
			} else {
				// method element
				AbstractMethodDeclaration method = methodScope.referenceMethod();
				newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method));
				if (newElement != null) {
					knownScopes.put(scope, newElement);
				}
			}
			break;
		case Scope.BLOCK_SCOPE :
			// standard block, no element per se
			newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes);
			break;
	}
	return newElement;
}
 
Example 20
Source File: ReorgUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static String createNamePattern(IJavaElement element) throws JavaModelException {
	switch(element.getElementType()){
		case IJavaElement.CLASS_FILE:
			return RefactoringCoreMessages.ReorgUtils_3;
		case IJavaElement.COMPILATION_UNIT:
			return RefactoringCoreMessages.ReorgUtils_4;
		case IJavaElement.FIELD:
			return RefactoringCoreMessages.ReorgUtils_5;
		case IJavaElement.IMPORT_CONTAINER:
			return RefactoringCoreMessages.ReorgUtils_6;
		case IJavaElement.IMPORT_DECLARATION:
			return RefactoringCoreMessages.ReorgUtils_7;
		case IJavaElement.INITIALIZER:
			return RefactoringCoreMessages.ReorgUtils_8;
		case IJavaElement.JAVA_PROJECT:
			return RefactoringCoreMessages.ReorgUtils_9;
		case IJavaElement.METHOD:
			if (((IMethod)element).isConstructor()) {
				return RefactoringCoreMessages.ReorgUtils_10;
			} else {
				return RefactoringCoreMessages.ReorgUtils_11;
			}
		case IJavaElement.PACKAGE_DECLARATION:
			return RefactoringCoreMessages.ReorgUtils_12;
		case IJavaElement.PACKAGE_FRAGMENT:
			if (JavaElementUtil.isDefaultPackage(element)) {
				return RefactoringCoreMessages.ReorgUtils_13;
			} else {
				return RefactoringCoreMessages.ReorgUtils_14;
			}
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			if (((IPackageFragmentRoot) element).isArchive()) {
				return RefactoringCoreMessages.ReorgUtils_21;
			}
			if (isSourceFolder(element)) {
				return RefactoringCoreMessages.ReorgUtils_15;
			}
			if (isClassFolder(element)) {
				return RefactoringCoreMessages.ReorgUtils_16;
			}
			return RefactoringCoreMessages.ReorgUtils_17;
		case IJavaElement.TYPE:
			IType type= (IType)element;
			if (type.isAnonymous()) {
				return RefactoringCoreMessages.ReorgUtils_20;
			}
			return RefactoringCoreMessages.ReorgUtils_18;
		default:
			Assert.isTrue(false);
			return null;
	}
}