Java Code Examples for org.eclipse.jdt.internal.compiler.lookup.Scope#CLASS_SCOPE

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.Scope#CLASS_SCOPE . 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: CompletionOnJavadocFieldReference.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected TypeBinding internalResolveType(Scope scope) {

		if (this.token != null) {
			return super.internalResolveType(scope);
		}

		// Resolve only receiver
		if (this.receiver == null) {
			this.actualReceiverType = scope.enclosingSourceType();
		} else if (scope.kind == Scope.CLASS_SCOPE) {
			this.actualReceiverType = this.receiver.resolveType((ClassScope) scope);
		} else {
			this.actualReceiverType = this.receiver.resolveType((BlockScope)scope);
		}
		return null;
	}
 
Example 2
Source File: InternalExtendedCompletionContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean canUseDiamond(String[] parameterTypes, char[] fullyQualifiedTypeName) {
	TypeBinding guessedType = null;
	char[][] cn = CharOperation.splitOn('.', fullyQualifiedTypeName);
	Scope scope = this.assistScope;
	if (scope.compilerOptions().sourceLevel < ClassFileConstants.JDK1_7) return false;
	// If no LHS or return type expected, then we can safely use diamond
	char[][] expectedTypekeys= this.completionContext.getExpectedTypesKeys();
	if (expectedTypekeys == null || expectedTypekeys.length == 0)
		return true;
	// Next, find out whether any of the constructor parameters are the same as one of the 
	// class type variables. If yes, diamond cannot be used.
	TypeReference ref;
	if (cn.length == 1) {
		ref = new SingleTypeReference(cn[0], 0);
	} else {
		ref = new QualifiedTypeReference(cn,new long[cn.length]);
	}
	switch (scope.kind) {
		case Scope.METHOD_SCOPE :
		case Scope.BLOCK_SCOPE :
			guessedType = ref.resolveType((BlockScope)scope);
			break;
		case Scope.CLASS_SCOPE :
			guessedType = ref.resolveType((ClassScope)scope);
			break;
	}
	if (guessedType != null && guessedType.isValidBinding()) {
		// the erasure must be used because guessedType can be a RawTypeBinding
		guessedType = guessedType.erasure();
		TypeVariableBinding[] typeVars = guessedType.typeVariables();
		for (int i = 0; i < parameterTypes.length; i++) {
			for (int j = 0; j < typeVars.length; j++) {
				if (CharOperation.equals(parameterTypes[i].toCharArray(), typeVars[j].sourceName))
					return false;
			}
		}
		return true;
	}
	return false;
}
 
Example 3
Source File: CodeSnippetSingleNameReference.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check and/or redirect the field access to the delegate receiver if any
 */
public TypeBinding getReceiverType(BlockScope currentScope) {
	Scope scope = currentScope.parent;
	while (true) {
			switch (scope.kind) {
				case Scope.CLASS_SCOPE :
					return ((ClassScope) scope).referenceContext.binding;
				default:
					scope = scope.parent;
			}
	}
}
 
Example 4
Source File: LambdaExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void tagAsHavingErrors() {
	this.ignoreFurtherInvestigation = true;
	Scope parent = this.enclosingScope.parent;
	while (parent != null) {
		switch(parent.kind) {
			case Scope.CLASS_SCOPE:
			case Scope.METHOD_SCOPE:
				parent.referenceContext().tagAsHavingErrors();
				return;
			default:
				parent = parent.parent;
				break;
		}
	}
}
 
Example 5
Source File: MissingTypesGuesser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void guess(TypeReference typeRef, Scope scope, GuessedTypeRequestor requestor) {
	this.substituedTypes = new HashMap();
	this.originalTypes = new HashMap();
	this.combinationsCount = 1;

	TypeReference convertedType = convert(typeRef);

	if(convertedType == null) return;

	QualifiedTypeReference[] substituedTypeNodes = getSubstituedTypes();
	int length = substituedTypeNodes.length;

	int[] substitutionsIndexes = new int[substituedTypeNodes.length];
	char[][][][] subtitutions = new char[substituedTypeNodes.length][][][];
	char[][][] originalTypeNames = new char[substituedTypeNodes.length][][];
	for (int i = 0; i < substituedTypeNodes.length; i++) {
		subtitutions[i] = getSubstitution(substituedTypeNodes[i]);
		originalTypeNames[i] = getOriginal(substituedTypeNodes[i]);
	}

	ResolutionCleaner resolutionCleaner = new ResolutionCleaner();
	for (int i = 0; i < this.combinationsCount; i++) {

		nextSubstitution(substituedTypeNodes, subtitutions, substitutionsIndexes);


		this.problemFactory.startCheckingProblems();
		TypeBinding guessedType = null;
		switch (scope.kind) {
			case Scope.METHOD_SCOPE :
			case Scope.BLOCK_SCOPE :
				resolutionCleaner.cleanUp(convertedType, (BlockScope)scope);
				guessedType = convertedType.resolveType((BlockScope)scope);
				break;
			case Scope.CLASS_SCOPE :
				resolutionCleaner.cleanUp(convertedType, (ClassScope)scope);
				guessedType = convertedType.resolveType((ClassScope)scope);
				break;
		}
		this.problemFactory.stopCheckingProblems();
		if (!this.problemFactory.hasForbiddenProblems) {
			if (guessedType != null) {
				Binding[] missingElements = new Binding[length];
				int[] missingElementsStarts = new int[length];
				int[] missingElementsEnds = new int[length];

				if(computeMissingElements(
						substituedTypeNodes,
						originalTypeNames,
						missingElements,
						missingElementsStarts,
						missingElementsEnds)) {
					requestor.accept(
							guessedType.capture(scope, typeRef.sourceEnd),
							missingElements,
							missingElementsStarts,
							missingElementsEnds,
							this.problemFactory.hasAllowedProblems);
				}
			}
		}
	}
}
 
Example 6
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;
}