use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class ImportReferencesCollector method possibleStaticImportFound.
private void possibleStaticImportFound(Name name) {
if (fStaticImports == null || fASTRoot == null) {
return;
}
while (name.isQualifiedName()) {
name = ((QualifiedName) name).getQualifier();
}
if (!isAffected(name)) {
return;
}
IBinding binding = name.resolveBinding();
SimpleName simpleName = (SimpleName) name;
if (binding == null || binding instanceof ITypeBinding || !Modifier.isStatic(binding.getModifiers()) || simpleName.isDeclaration()) {
return;
}
if (binding instanceof IVariableBinding) {
IVariableBinding varBinding = (IVariableBinding) binding;
if (varBinding.isField()) {
varBinding = varBinding.getVariableDeclaration();
ITypeBinding declaringClass = varBinding.getDeclaringClass();
if (declaringClass != null && !declaringClass.isLocal()) {
if (new ScopeAnalyzer(fASTRoot).isDeclaredInScope(varBinding, simpleName, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
return;
fStaticImports.add(simpleName);
}
}
} else if (binding instanceof IMethodBinding) {
IMethodBinding methodBinding = ((IMethodBinding) binding).getMethodDeclaration();
ITypeBinding declaringClass = methodBinding.getDeclaringClass();
if (declaringClass != null && !declaringClass.isLocal()) {
if (new ScopeAnalyzer(fASTRoot).isDeclaredInScope(methodBinding, simpleName, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY))
return;
fStaticImports.add(simpleName);
}
}
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class StubUtility method getBaseNameFromExpression.
private static String getBaseNameFromExpression(IJavaProject project, Expression assignedExpression, int variableKind) {
String name = null;
if (assignedExpression instanceof CastExpression) {
assignedExpression = ((CastExpression) assignedExpression).getExpression();
}
if (assignedExpression instanceof Name) {
Name simpleNode = (Name) assignedExpression;
IBinding binding = simpleNode.resolveBinding();
if (binding instanceof IVariableBinding)
return getBaseName((IVariableBinding) binding, project);
return ASTNodes.getSimpleNameIdentifier(simpleNode);
} else if (assignedExpression instanceof MethodInvocation) {
name = ((MethodInvocation) assignedExpression).getName().getIdentifier();
} else if (assignedExpression instanceof SuperMethodInvocation) {
name = ((SuperMethodInvocation) assignedExpression).getName().getIdentifier();
} else if (assignedExpression instanceof FieldAccess) {
return ((FieldAccess) assignedExpression).getName().getIdentifier();
} else if (variableKind == NamingConventions.VK_STATIC_FINAL_FIELD && (assignedExpression instanceof StringLiteral || assignedExpression instanceof NumberLiteral)) {
String string = assignedExpression instanceof StringLiteral ? ((StringLiteral) assignedExpression).getLiteralValue() : ((NumberLiteral) assignedExpression).getToken();
StringBuffer res = new StringBuffer();
boolean needsUnderscore = false;
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (Character.isJavaIdentifierPart(ch)) {
if (res.length() == 0 && !Character.isJavaIdentifierStart(ch) || needsUnderscore) {
res.append('_');
}
res.append(ch);
needsUnderscore = false;
} else {
needsUnderscore = res.length() > 0;
}
}
if (res.length() > 0) {
return res.toString();
}
}
if (name != null) {
for (int i = 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) {
String curr = KNOWN_METHOD_NAME_PREFIXES[i];
if (name.startsWith(curr)) {
if (name.equals(curr)) {
// don't suggest 'get' as variable name
return null;
} else if (Character.isUpperCase(name.charAt(curr.length()))) {
return name.substring(curr.length());
}
}
}
}
return name;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewTypeProposals.
public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
Name node = refNode;
do {
String typeName = ASTNodes.getSimpleNameIdentifier(node);
Name qualifier = null;
// only propose to create types for qualifiers when the name starts with upper case
boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
if (isPossibleName) {
IPackageFragment enclosingPackage = null;
IType enclosingType = null;
if (node.isSimpleName()) {
enclosingPackage = (IPackageFragment) cu.getParent();
// don't suggest member type, user can select it in wizard
} else {
Name qualifierName = ((QualifiedName) node).getQualifier();
IBinding binding = qualifierName.resolveBinding();
if (binding != null && binding.isRecovered()) {
binding = null;
}
if (binding instanceof ITypeBinding) {
enclosingType = (IType) binding.getJavaElement();
} else if (binding instanceof IPackageBinding) {
qualifier = qualifierName;
enclosingPackage = (IPackageFragment) binding.getJavaElement();
} else {
IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
if (res != null && res.length > 0 && res[0] instanceof IType) {
enclosingType = (IType) res[0];
} else {
qualifier = qualifierName;
enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
}
}
}
int rel = relevance;
if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
rel += 3;
}
if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
// new member type
IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
//TODO NewCUUsingWizardProposal
if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS, enclosing, rel+3));
}
if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE, enclosing, rel+2));
}
if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM, enclosing, rel));
}
if ((kind & SimilarElementsRequestor.ANNOTATIONS) != 0) {
// proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION, enclosing, rel + 1));
addNullityAnnotationTypesProposals(cu, node, proposals);
}
}
}
node = qualifier;
} while (node != null);
// type parameter proposals
if (refNode.isSimpleName() && (kind & SimilarElementsRequestor.VARIABLES) != 0) {
CompilationUnit root = (CompilationUnit) refNode.getRoot();
String name = ((SimpleName) refNode).getIdentifier();
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
int baseRel = relevance;
if (isLikelyTypeParameterName(name)) {
baseRel += 8;
}
while (declaration != null) {
IBinding binding = null;
int rel = baseRel;
if (declaration instanceof MethodDeclaration) {
binding = ((MethodDeclaration) declaration).resolveBinding();
if (isLikelyMethodTypeParameterName(name))
rel += 2;
} else if (declaration instanceof TypeDeclaration) {
binding = ((TypeDeclaration) declaration).resolveBinding();
rel++;
}
if (binding != null) {
AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
proposals.add(proposal);
}
if (!Modifier.isStatic(declaration.getModifiers())) {
declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
} else {
declaration = null;
}
}
}
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method getMethodProposals.
public static void getMethodProposals(IInvocationContext context, IProblemLocation problem, boolean isOnlyParameterMismatch, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (!(selectedNode instanceof SimpleName)) {
return;
}
SimpleName nameNode = (SimpleName) selectedNode;
List<Expression> arguments;
Expression sender;
boolean isSuperInvocation;
ASTNode invocationNode = nameNode.getParent();
if (invocationNode instanceof MethodInvocation) {
MethodInvocation methodImpl = (MethodInvocation) invocationNode;
arguments = methodImpl.arguments();
sender = methodImpl.getExpression();
isSuperInvocation = false;
} else if (invocationNode instanceof SuperMethodInvocation) {
SuperMethodInvocation methodImpl = (SuperMethodInvocation) invocationNode;
arguments = methodImpl.arguments();
sender = methodImpl.getQualifier();
isSuperInvocation = true;
} else {
return;
}
String methodName = nameNode.getIdentifier();
int nArguments = arguments.size();
// corrections
IBinding[] bindings = (new ScopeAnalyzer(astRoot)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
HashSet<String> suggestedRenames = new HashSet<String>();
for (int i = 0; i < bindings.length; i++) {
IMethodBinding binding = (IMethodBinding) bindings[i];
String curr = binding.getName();
if (!curr.equals(methodName) && binding.getParameterTypes().length == nArguments && NameMatcher.isSimilarName(methodName, curr) && suggestedRenames.add(curr)) {
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethod_description, BasicElementLabels.getJavaElementName(curr));
proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), problem.getOffset(), problem.getLength(), curr, IProposalRelevance.CHANGE_METHOD));
}
}
suggestedRenames = null;
if (isOnlyParameterMismatch) {
ArrayList<IMethodBinding> parameterMismatchs = new ArrayList<IMethodBinding>();
for (int i = 0; i < bindings.length; i++) {
IMethodBinding binding = (IMethodBinding) bindings[i];
if (binding.getName().equals(methodName)) {
parameterMismatchs.add(binding);
}
}
addParameterMissmatchProposals(context, problem, parameterMismatchs, invocationNode, arguments, proposals);
}
if (sender == null) {
addStaticImportFavoriteProposals(context, nameNode, true, proposals);
}
// new method
addNewMethodProposals(cu, astRoot, sender, arguments, isSuperInvocation, invocationNode, methodName, proposals);
if (!isOnlyParameterMismatch && !isSuperInvocation && sender != null) {
addMissingCastParentsProposal(cu, (MethodInvocation) invocationNode, proposals);
}
if (!isSuperInvocation && sender == null && invocationNode.getParent() instanceof ThrowStatement) {
//$NON-NLS-1$ // do it the manual way, copting all the arguments is nasty
String str = "new ";
String label = CorrectionMessages.UnresolvedElementsSubProcessor_addnewkeyword_description;
int relevance = Character.isUpperCase(methodName.charAt(0)) ? IProposalRelevance.ADD_NEW_KEYWORD_UPPERCASE : IProposalRelevance.ADD_NEW_KEYWORD;
ReplaceCorrectionProposal proposal = new ReplaceCorrectionProposal(label, cu, invocationNode.getStartPosition(), 0, str, relevance);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class UnresolvedElementsSubProcessor method getConstructorProposals.
public static void getConstructorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
ITypeBinding targetBinding = null;
List<Expression> arguments = null;
IMethodBinding recursiveConstructor = null;
int type = selectedNode.getNodeType();
if (type == ASTNode.CLASS_INSTANCE_CREATION) {
ClassInstanceCreation creation = (ClassInstanceCreation) selectedNode;
IBinding binding = creation.getType().resolveBinding();
if (binding instanceof ITypeBinding) {
targetBinding = (ITypeBinding) binding;
arguments = creation.arguments();
}
} else if (type == ASTNode.SUPER_CONSTRUCTOR_INVOCATION) {
ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
if (typeBinding != null && !typeBinding.isAnonymous()) {
targetBinding = typeBinding.getSuperclass();
arguments = ((SuperConstructorInvocation) selectedNode).arguments();
}
} else if (type == ASTNode.CONSTRUCTOR_INVOCATION) {
ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
if (typeBinding != null && !typeBinding.isAnonymous()) {
targetBinding = typeBinding;
arguments = ((ConstructorInvocation) selectedNode).arguments();
recursiveConstructor = ASTResolving.findParentMethodDeclaration(selectedNode).resolveBinding();
}
}
if (targetBinding == null) {
return;
}
IMethodBinding[] methods = targetBinding.getDeclaredMethods();
ArrayList<IMethodBinding> similarElements = new ArrayList<IMethodBinding>();
for (int i = 0; i < methods.length; i++) {
IMethodBinding curr = methods[i];
if (curr.isConstructor() && recursiveConstructor != curr) {
// similar elements can contain a implicit default constructor
similarElements.add(curr);
}
}
addParameterMissmatchProposals(context, problem, similarElements, selectedNode, arguments, proposals);
if (targetBinding.isFromSource()) {
ITypeBinding targetDecl = targetBinding.getTypeDeclaration();
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, targetDecl);
if (targetCU != null) {
String[] args = new String[] { ASTResolving.getMethodSignature(ASTResolving.getTypeSignature(targetDecl), getParameterTypes(arguments), false) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createconstructor_description, args);
Image image = JavaElementImageProvider.getDecoratedImage(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
proposals.add(new NewMethodCorrectionProposal(label, targetCU, selectedNode, arguments, targetDecl, IProposalRelevance.CREATE_CONSTRUCTOR, image));
}
}
}
Aggregations