use of org.eclipse.jdt.core.dom.VariableDeclaration 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.VariableDeclaration in project che by eclipse.
the class NullAnnotationsFix method isComplainingAboutArgument.
/* recognizes any simple name referring to a parameter binding */
public static boolean isComplainingAboutArgument(ASTNode selectedNode) {
if (!(selectedNode instanceof SimpleName))
return false;
SimpleName nameNode = (SimpleName) selectedNode;
IBinding binding = nameNode.resolveBinding();
if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
return true;
VariableDeclaration argDecl = (VariableDeclaration) ASTNodes.getParent(selectedNode, VariableDeclaration.class);
if (argDecl != null)
binding = argDecl.resolveBinding();
if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
return true;
return false;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class InlineTempRefactoring method checkInitialConditions.
/*
* @see Refactoring#checkActivation(IProgressMonitor)
*/
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
try {
//$NON-NLS-1$
pm.beginTask("", 1);
RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext());
if (result.hasFatalError())
return result;
VariableDeclaration declaration = getVariableDeclaration();
result.merge(checkSelection(declaration));
if (result.hasFatalError())
return result;
result.merge(checkInitializer(declaration));
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class InlineTempRefactoring method getModifiedInitializerSource.
private Expression getModifiedInitializerSource(CompilationUnitRewrite rewrite, SimpleName reference) throws JavaModelException {
VariableDeclaration varDecl = getVariableDeclaration();
Expression initializer = varDecl.getInitializer();
ASTNode referenceContext = reference.getParent();
if (Invocations.isResolvedTypeInferredFromExpectedType(initializer)) {
if (!(referenceContext instanceof VariableDeclarationFragment || referenceContext instanceof SingleVariableDeclaration || referenceContext instanceof Assignment)) {
ITypeBinding[] typeArguments = Invocations.getInferredTypeArguments(initializer);
if (typeArguments != null) {
String newSource = createParameterizedInvocation(initializer, typeArguments, rewrite);
return (Expression) rewrite.getASTRewrite().createStringPlaceholder(newSource, initializer.getNodeType());
}
}
}
Expression copy = (Expression) rewrite.getASTRewrite().createCopyTarget(initializer);
AST ast = rewrite.getAST();
if (NecessaryParenthesesChecker.needsParentheses(initializer, reference.getParent(), reference.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
ITypeBinding explicitCast = ASTNodes.getExplicitCast(initializer, reference);
if (explicitCast != null) {
CastExpression cast = ast.newCastExpression();
if (NecessaryParenthesesChecker.needsParentheses(copy, cast, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression(copy);
copy = parenthesized;
}
cast.setExpression(copy);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(reference, rewrite.getImportRewrite());
cast.setType(rewrite.getImportRewrite().addImport(explicitCast, ast, context));
copy = cast;
} else if (initializer instanceof ArrayInitializer && ASTNodes.getDimensions(varDecl) > 0) {
ArrayType newType = (ArrayType) ASTNodeFactory.newType(ast, varDecl);
ArrayCreation newArrayCreation = ast.newArrayCreation();
newArrayCreation.setType(newType);
newArrayCreation.setInitializer((ArrayInitializer) copy);
return newArrayCreation;
}
return copy;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class IntroduceFactoryRefactoring method getCtorCallAt.
/**
* Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
* node that this search hit identified. Necessary because the <code>SearchEngine</code>
* doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
* @param start
* @param length
* @param unitAST
* @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
* @throws CoreException
*/
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
ICompilationUnit unitHandle = ASTCreator.getCu(unitAST);
ASTNode node = NodeFinder.perform(unitAST, start, length);
if (node == null)
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null));
if (node instanceof ClassInstanceCreation) {
if (((ClassInstanceCreation) node).getAnonymousClassDeclaration() != null) {
// Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
fConstructorVisibility = Modifier.PROTECTED;
return null;
}
return node;
} else if (node instanceof VariableDeclaration) {
Expression init = ((VariableDeclaration) node).getInitializer();
if (init instanceof ClassInstanceCreation) {
return init;
} else if (init != null)
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null));
} else if (node instanceof ConstructorInvocation) {
// to another flavor on the same class.
return null;
} else if (node instanceof SuperConstructorInvocation) {
// This is a call we can bypass; it's from one constructor flavor
// to another flavor on the same class.
fConstructorVisibility = Modifier.PROTECTED;
return null;
} else if (node instanceof ExpressionStatement) {
Expression expr = ((ExpressionStatement) node).getExpression();
if (expr instanceof ClassInstanceCreation)
return expr;
else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
} else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
// We seem to have been given a hit for an implicit call to the base-class constructor.
// Do nothing with this (implicit) call, but have to make sure we make the derived class
// doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
fConstructorVisibility = Modifier.PROTECTED;
return null;
} else if (node instanceof MethodRef) {
return node;
} else
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
null));
}
Aggregations