use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class RenameAnalyzeUtil method analyzeLocalRenames.
/**
* This method analyzes a set of local variable renames inside one cu. It checks whether
* any new compile errors have been introduced by the rename(s) and whether the correct
* node(s) has/have been renamed.
*
* @param analyzePackages the LocalAnalyzePackages containing the information about the local renames
* @param cuChange the TextChange containing all local variable changes to be applied.
* @param oldCUNode the fully (incl. bindings) resolved AST node of the original compilation unit
* @param recovery whether statements and bindings recovery should be performed when parsing the changed CU
* @return a RefactoringStatus containing errors if compile errors or wrongly renamed nodes are found
* @throws CoreException thrown if there was an error greating the preview content of the change
*/
public static RefactoringStatus analyzeLocalRenames(LocalAnalyzePackage[] analyzePackages, TextChange cuChange, CompilationUnit oldCUNode, boolean recovery) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
ICompilationUnit compilationUnit = (ICompilationUnit) oldCUNode.getJavaElement();
String newCuSource = cuChange.getPreviewContent(new NullProgressMonitor());
CompilationUnit newCUNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(newCuSource, compilationUnit, true, recovery, null);
result.merge(analyzeCompileErrors(newCuSource, newCUNode, oldCUNode));
if (result.hasError())
return result;
for (int i = 0; i < analyzePackages.length; i++) {
ASTNode enclosing = getEnclosingBlockOrMethodOrLambda(analyzePackages[i].fDeclarationEdit, cuChange, newCUNode);
// get new declaration
IRegion newRegion = RefactoringAnalyzeUtil.getNewTextRange(analyzePackages[i].fDeclarationEdit, cuChange);
ASTNode newDeclaration = NodeFinder.perform(newCUNode, newRegion.getOffset(), newRegion.getLength());
Assert.isTrue(newDeclaration instanceof Name);
VariableDeclaration declaration = getVariableDeclaration((Name) newDeclaration);
Assert.isNotNull(declaration);
SimpleName[] problemNodes = ProblemNodeFinder.getProblemNodes(enclosing, declaration, analyzePackages[i].fOccurenceEdits, cuChange);
result.merge(RefactoringAnalyzeUtil.reportProblemNodes(newCuSource, problemNodes));
}
return result;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class UnresolvedElementsSubProcessor method getExpressionBaseName.
private static String getExpressionBaseName(Expression expr) {
IBinding argBinding = Bindings.resolveExpressionBinding(expr, true);
if (argBinding instanceof IVariableBinding) {
IJavaProject project = null;
ASTNode root = expr.getRoot();
if (root instanceof CompilationUnit) {
ITypeRoot typeRoot = ((CompilationUnit) root).getTypeRoot();
if (typeRoot != null)
project = typeRoot.getJavaProject();
}
return StubUtility.getBaseName((IVariableBinding) argBinding, project);
}
if (expr instanceof SimpleName)
return ((SimpleName) expr).getIdentifier();
return null;
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class UnresolvedElementsSubProcessor method doEqualNumberOfParameters.
private static void doEqualNumberOfParameters(IInvocationContext context, ASTNode invocationNode, IProblemLocation problem, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodBinding, Collection<ICommandAccess> proposals) throws CoreException {
ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
int[] indexOfDiff = new int[paramTypes.length];
int nDiffs = 0;
for (int n = 0; n < argTypes.length; n++) {
if (!canAssign(argTypes[n], paramTypes[n])) {
indexOfDiff[nDiffs++] = n;
}
}
ITypeBinding declaringTypeDecl = methodBinding.getDeclaringClass().getTypeDeclaration();
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode nameNode = problem.getCoveringNode(astRoot);
if (nameNode == null) {
return;
}
if (nDiffs == 0) {
if (nameNode.getParent() instanceof MethodInvocation) {
MethodInvocation inv = (MethodInvocation) nameNode.getParent();
if (inv.getExpression() == null) {
addQualifierToOuterProposal(context, inv, methodBinding, proposals);
}
}
return;
}
if (nDiffs == 1) {
// one argument mismatching: try to fix
int idx = indexOfDiff[0];
Expression nodeToCast = arguments.get(idx);
ITypeBinding castType = paramTypes[idx];
castType = Bindings.normalizeTypeBinding(castType);
if (castType.isWildcardType()) {
castType = ASTResolving.normalizeWildcardType(castType, false, nodeToCast.getAST());
}
if (castType != null) {
ITypeBinding binding = nodeToCast.resolveTypeBinding();
ITypeBinding castFixType = null;
if (binding == null || castType.isCastCompatible(binding)) {
castFixType = castType;
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
ITypeBinding boxUnboxedTypeBinding = TypeMismatchSubProcessor.boxUnboxPrimitives(castType, binding, nodeToCast.getAST());
if (boxUnboxedTypeBinding != castType && boxUnboxedTypeBinding.isCastCompatible(binding)) {
castFixType = boxUnboxedTypeBinding;
}
}
if (castFixType != null) {
ASTRewriteCorrectionProposal proposal = TypeMismatchSubProcessor.createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CAST_ARGUMENT_1);
String castTypeName = BindingLabelProvider.getBindingLabel(castFixType, JavaElementLabels.ALL_DEFAULT);
String[] arg = new String[] { getArgumentName(arguments, idx), castTypeName };
proposal.setDisplayName(Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addargumentcast_description, arg));
proposals.add(proposal);
}
TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, nodeToCast, castType, false, IProposalRelevance.CAST_ARGUMENT_2, proposals);
}
}
if (nDiffs == 2) {
// try to swap
int idx1 = indexOfDiff[0];
int idx2 = indexOfDiff[1];
boolean canSwap = canAssign(argTypes[idx1], paramTypes[idx2]) && canAssign(argTypes[idx2], paramTypes[idx1]);
if (canSwap) {
Expression arg1 = arguments.get(idx1);
Expression arg2 = arguments.get(idx2);
ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
rewrite.replace(arg1, rewrite.createCopyTarget(arg2), null);
rewrite.replace(arg2, rewrite.createCopyTarget(arg1), null);
{
String[] arg = new String[] { getArgumentName(arguments, idx1), getArgumentName(arguments, idx2) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swaparguments_description, arg);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SWAP_ARGUMENTS, image);
proposals.add(proposal);
}
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
if (targetCU != null) {
ChangeDescription[] changeDesc = new ChangeDescription[paramTypes.length];
for (int i = 0; i < nDiffs; i++) {
changeDesc[idx1] = new SwapDescription(idx2);
}
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
ITypeBinding[] swappedTypes = new ITypeBinding[] { declParamTypes[idx1], declParamTypes[idx2] };
String[] args = new String[] { ASTResolving.getMethodSignature(methodDecl), getTypeNames(swappedTypes) };
String label;
if (methodDecl.isConstructor()) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_constr_description, args);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_description, args);
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SWAP_PARAMETERS, image);
proposals.add(proposal);
}
}
return;
}
}
if (declaringTypeDecl.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
if (targetCU != null) {
ChangeDescription[] changeDesc = createSignatureChangeDescription(indexOfDiff, nDiffs, paramTypes, arguments, argTypes);
if (changeDesc != null) {
IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
ITypeBinding[] newParamTypes = new ITypeBinding[changeDesc.length];
for (int i = 0; i < newParamTypes.length; i++) {
newParamTypes[i] = changeDesc[i] == null ? declParamTypes[i] : ((EditDescription) changeDesc[i]).type;
}
boolean isVarArgs = methodDecl.isVarargs() && newParamTypes.length > 0 && newParamTypes[newParamTypes.length - 1].isArray();
String[] args = new String[] { ASTResolving.getMethodSignature(methodDecl), ASTResolving.getMethodSignature(methodDecl.getName(), newParamTypes, isVarArgs) };
String label;
if (methodDecl.isConstructor()) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_constr_description, args);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_description, args);
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SIGNATURE, image);
proposals.add(proposal);
}
}
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class UnresolvedElementsSubProcessor method addNewMethodProposals.
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Collection<ICommandAccess> proposals) throws JavaModelException {
ITypeBinding nodeParentType = Bindings.getBindingOfParentType(invocationNode);
ITypeBinding binding = null;
if (sender != null) {
binding = sender.resolveTypeBinding();
} else {
binding = nodeParentType;
if (isSuperInvocation && binding != null) {
binding = binding.getSuperclass();
}
}
if (binding != null && binding.isFromSource()) {
ITypeBinding senderDeclBinding = binding.getTypeDeclaration();
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
if (targetCU != null) {
String label;
Image image;
ITypeBinding[] parameterTypes = getParameterTypes(arguments);
if (parameterTypes != null) {
String sig = ASTResolving.getMethodSignature(methodName, parameterTypes, false);
if (ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
if (nodeParentType == senderDeclBinding) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig);
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE);
} else {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) });
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
}
proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
}
if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) {
// no covering method
ASTNode anonymDecl = astRoot.findDeclaringNode(senderDeclBinding);
if (anonymDecl != null) {
senderDeclBinding = Bindings.getBindingOfParentType(anonymDecl.getParent());
if (!senderDeclBinding.isAnonymous() && ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
String[] args = new String[] { sig, ASTResolving.getTypeSignature(senderDeclBinding) };
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args);
image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PROTECTED);
proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
}
}
}
}
}
}
}
use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.
the class UnresolvedElementsSubProcessor method addSimilarTypeProposals.
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));
}
}
}
}
Aggregations