Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#isRecovered()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isRecovered() . 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: JavaTypeHierarchyExtractor.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 2
Source File: JavaTypeHierarchyExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 3
Source File: JavaTypeHierarchyExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 4
Source File: Resolver.java    From DesigniteJava with Apache License 2.0 5 votes vote down vote up
private void inferTypeInfo(SM_Project parentProject, TypeInfo typeInfo, Type typeOfVar, SM_Type callerType) {
	ITypeBinding iType = typeOfVar.resolveBinding();
	/*
	 * In some cases, the above statement doesnt resolve the binding even if the
	 * type is present in the same project (and we dont know the reason). We need to
	 * handle this situation explicitly by checking the 'binding' property of iType.
	 * if it is of type MissingTypeBinding, we need to search the type in the
	 * present project. We may have to use import statements to identify the package
	 * in which this (to be resolved) type exists.
	 */

	// The case that the iType is RecoveredTypeBinding which leads to
	// ProblemReferenceBinding and consequently to MissingTypeBinding
	if (iType == null) {
		inferPrimitiveType(parentProject, typeInfo, iType);
		infereParametrized(parentProject, typeInfo, iType);
	} else if (iType.isRecovered()) {
		// Search in the ast explicitly and assign
		String unresolvedTypeName = typeOfVar.toString().replace("[]", ""); // cover the Array case
		SM_Type matchedType = manualLookupForUnresolvedType(parentProject, unresolvedTypeName, callerType);
		if (matchedType != null) {
			manualInferUnresolvedTypeType(typeInfo, matchedType);
		}
	} else {
		inferPrimitiveType(parentProject, typeInfo, iType);
		infereParametrized(parentProject, typeInfo, iType);
	}
}
 
Example 5
Source File: CreateAsyncInterfaceProposal.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
  ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
  if (binding != null && !binding.isRecovered()) {
    return binding;
  }
  return null;
}
 
Example 6
Source File: OrganizeImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean needsImport(ITypeBinding typeBinding, SimpleName ref) {
	if (!typeBinding.isTopLevel() && !typeBinding.isMember() || typeBinding.isRecovered()) {
		return false; // no imports for anonymous, local, primitive types or parameters types
	}
	int modifiers= typeBinding.getModifiers();
	if (Modifier.isPrivate(modifiers)) {
		return false; // imports for privates are not required
	}
	ITypeBinding currTypeBinding= Bindings.getBindingOfParentType(ref);
	if (currTypeBinding == null) {
		if (ASTNodes.getParent(ref, ASTNode.PACKAGE_DECLARATION) != null) {
			return true; // reference in package-info.java
		}
		return false; // not in a type
	}
	if (!Modifier.isPublic(modifiers)) {
		if (!currTypeBinding.getPackage().getName().equals(typeBinding.getPackage().getName())) {
			return false; // not visible
		}
	}

	ASTNode parent= ref.getParent();
	while (parent instanceof Type) {
		parent= parent.getParent();
	}
	if (parent instanceof AbstractTypeDeclaration && parent.getParent() instanceof CompilationUnit) {
		return true;
	}

	if (typeBinding.isMember()) {
		if (fAnalyzer.isDeclaredInScope(typeBinding, ref, ScopeAnalyzer.TYPES | ScopeAnalyzer.CHECK_VISIBILITY))
			return false;
	}
	return true;
}
 
Example 7
Source File: OrganizeImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tries to find the given type name and add it to the import structure.
 * @param ref the name node
 */
public void add(SimpleName ref) {
	String typeName= ref.getIdentifier();

	if (fImportsAdded.contains(typeName)) {
		return;
	}

	IBinding binding= ref.resolveBinding();
	if (binding != null) {
		if (binding.getKind() != IBinding.TYPE) {
			return;
		}
		ITypeBinding typeBinding= (ITypeBinding) binding;
		if (typeBinding.isArray()) {
			typeBinding= typeBinding.getElementType();
		}
		typeBinding= typeBinding.getTypeDeclaration();
		if (!typeBinding.isRecovered()) {
			if (needsImport(typeBinding, ref)) {
				fImpStructure.addImport(typeBinding);
				fImportsAdded.add(typeName);
			}
			return;
		}
	} else {
		if (fDoIgnoreLowerCaseNames && typeName.length() > 0) {
			char ch= typeName.charAt(0);
			if (Strings.isLowerCase(ch) && Character.isLetter(ch)) {
				return;
			}
		}
	}
	fImportsAdded.add(typeName);
	fUnresolvedTypes.put(typeName, new UnresolvedTypeData(ref));
}
 
Example 8
Source File: NewCUUsingWizardProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
	if (fTypeKind == K_ANNOTATION) {
		return null;
	}

	AST ast= node.getAST();
	node= ASTNodes.getNormalizedNode(node);
	ASTNode parent= node.getParent();
	switch (parent.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
				return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
			}
			break;
		case ASTNode.THROW_STATEMENT :
			return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
				return ast.resolveWellKnownType("java.lang.Exception"); //$NON-NLS-1$
			}
			break;
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
		case ASTNode.FIELD_DECLARATION:
			return null; // no guessing for LHS types, cannot be a supertype of a known type
		case ASTNode.PARAMETERIZED_TYPE:
			return null; // Inheritance doesn't help: A<X> z= new A<String>(); ->
	}
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null && !binding.isRecovered()) {
		return binding;
	}
	return null;
}
 
Example 9
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance,
		Collection<ChangeCorrectionProposal> proposals) throws CoreException {
	SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);

	// try to resolve type in context -> highest severity
	String resolvedTypeName= null;
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null) {
		ITypeBinding simpleBinding= binding;
		if (simpleBinding.isArray()) {
			simpleBinding= simpleBinding.getElementType();
		}
		simpleBinding= simpleBinding.getTypeDeclaration();

		if (!simpleBinding.isRecovered()) {
			resolvedTypeName= simpleBinding.getQualifiedName();
			CUCorrectionProposal proposal= createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
			proposals.add(proposal);
			if (proposal instanceof AddImportCorrectionProposal) {
				proposal.setRelevance(relevance + elements.length + 2);
			}

			if (binding.isParameterizedType()
					&& (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType)
					&& !(node.getParent().getParent() instanceof Type)) {
				proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
			}
		}
	} else {
		ASTNode normalizedNode= ASTNodes.getNormalizedNode(node);
		if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
			ITypeBinding normBinding= ASTResolving.guessBindingForTypeReference(normalizedNode);
			if (normBinding != null && !normBinding.isRecovered()) {
				proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
			}
		}
	}

	// add all similar elements
	for (int i= 0; i < elements.length; i++) {
		SimilarElement elem= elements[i];
		if ((elem.getKind() & TypeKinds.ALL_TYPES) != 0) {
			String fullName= elem.getName();
			if (!fullName.equals(resolvedTypeName)) {
				proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
			}
		}
	}
}
 
Example 10
Source File: TypeContextChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private ITypeBinding[] resolveBindings(String[] types, RefactoringStatus[] results, boolean firstPass) throws CoreException {
	//TODO: split types into parameterTypes and returnType
	int parameterCount= types.length - 1;
	ITypeBinding[] typeBindings= new ITypeBinding[types.length];

	StringBuffer cuString= new StringBuffer();
	cuString.append(fStubTypeContext.getBeforeString());
	int offsetBeforeMethodName= appendMethodDeclaration(cuString, types, parameterCount);
	cuString.append(fStubTypeContext.getAfterString());

	// need a working copy to tell the parser where to resolve (package visible) types
	ICompilationUnit wc= fMethod.getCompilationUnit().getWorkingCopy(new WorkingCopyOwner() {/*subclass*/}, new NullProgressMonitor());
	try {
		wc.getBuffer().setContents(cuString.toString());
		CompilationUnit compilationUnit= new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(wc, true);
		ASTNode method= NodeFinder.perform(compilationUnit, offsetBeforeMethodName, METHOD_NAME.length()).getParent();
		Type[] typeNodes= new Type[types.length];
		if (method instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration= (MethodDeclaration) method;
			typeNodes[parameterCount]= methodDeclaration.getReturnType2();
			List<SingleVariableDeclaration> parameters= methodDeclaration.parameters();
			for (int i= 0; i < parameterCount; i++)
				typeNodes[i]= parameters.get(i).getType();

		} else if (method instanceof AnnotationTypeMemberDeclaration) {
			typeNodes[0]= ((AnnotationTypeMemberDeclaration) method).getType();
		}

		for (int i= 0; i < types.length; i++) {
			Type type= typeNodes[i];
			if (type == null) {
				String msg= Messages.format(RefactoringCoreMessages.TypeContextChecker_couldNotResolveType, BasicElementLabels.getJavaElementName(types[i]));
				results[i]= RefactoringStatus.createErrorStatus(msg);
				continue;
			}
			results[i]= new RefactoringStatus();
			IProblem[] problems= ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS);
			if (problems.length > 0) {
				for (int p= 0; p < problems.length; p++)
					if (isError(problems[p], type))
						results[i].addError(problems[p].getMessage());
			}
			ITypeBinding binding= handleBug84585(type.resolveBinding());
			if (firstPass && (binding == null || binding.isRecovered())) {
				types[i]= qualifyTypes(type, results[i]);
			}
			typeBindings[i]= binding;
		}
		return typeBindings;
	} finally {
		wc.discardWorkingCopy();
	}
}
 
Example 11
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
	SimilarElement[] elements= SimilarElementsRequestor.findSimilarElement(cu, node, kind);

	// try to resolve type in context -> highest severity
	String resolvedTypeName= null;
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null) {
		ITypeBinding simpleBinding= binding;
		if (simpleBinding.isArray()) {
			simpleBinding= simpleBinding.getElementType();
		}
		simpleBinding= simpleBinding.getTypeDeclaration();

		if (!simpleBinding.isRecovered()) {
			resolvedTypeName= simpleBinding.getQualifiedName();
			CUCorrectionProposal proposal= createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
			proposals.add(proposal);
			if (proposal instanceof AddImportCorrectionProposal)
				proposal.setRelevance(relevance + elements.length + 2);

			if (binding.isParameterizedType()
					&& (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType)
					&& !(node.getParent().getParent() instanceof Type)) {
				proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
			}
		}
	} else {
		ASTNode normalizedNode= ASTNodes.getNormalizedNode(node);
		if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
			ITypeBinding normBinding= ASTResolving.guessBindingForTypeReference(normalizedNode);
			if (normBinding != null && !normBinding.isRecovered()) {
				proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
			}
		}
	}

	// add all similar elements
	for (int i= 0; i < elements.length; i++) {
		SimilarElement elem= elements[i];
		if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
			String fullName= elem.getName();
			if (!fullName.equals(resolvedTypeName)) {
				proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
			}
		}
	}
}