use of org.eclipse.jdt.core.dom.VariableDeclaration in project flux by eclipse.
the class QuickAssistProcessor method getArrayInitializerToArrayCreation.
private static boolean getArrayInitializerToArrayCreation(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof ArrayInitializer)) {
return false;
}
ArrayInitializer initializer = (ArrayInitializer) node;
ASTNode parent = initializer.getParent();
while (parent instanceof ArrayInitializer) {
initializer = (ArrayInitializer) parent;
parent = parent.getParent();
}
ITypeBinding typeBinding = initializer.resolveTypeBinding();
if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
return false;
}
if (resultingCollections == null) {
return true;
}
AST ast = node.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
String typeName = imports.addImport(typeBinding, importRewriteContext);
ArrayCreation creation = ast.newArrayCreation();
creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));
rewrite.replace(initializer, creation, null);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project flux by eclipse.
the class ASTNodes method isExplicitlyTypedLambda.
private static boolean isExplicitlyTypedLambda(Expression expression) {
if (!(expression instanceof LambdaExpression))
return false;
LambdaExpression lambda = (LambdaExpression) expression;
List<VariableDeclaration> parameters = lambda.parameters();
if (parameters.isEmpty())
return true;
return parameters.get(0) instanceof SingleVariableDeclaration;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method maybeRefactorLambdaExpression.
private boolean maybeRefactorLambdaExpression(final LambdaExpression visited, final Set<String> classesToUseWithImport) {
ITypeBinding targetType = ASTNodes.getTargetType(visited);
if (ASTNodes.hasType(targetType, Comparator.class.getCanonicalName()) && targetType.getTypeArguments() != null && targetType.getTypeArguments().length == 1 && visited.parameters() != null && visited.parameters().size() == 2) {
VariableDeclaration object1 = (VariableDeclaration) visited.parameters().get(0);
VariableDeclaration object2 = (VariableDeclaration) visited.parameters().get(1);
if (visited.getBody() instanceof Statement) {
return maybeRefactorBody(visited, targetType.getTypeArguments()[0], classesToUseWithImport, object1, object2, ASTNodes.asList((Statement) visited.getBody()));
}
if (visited.getBody() instanceof Expression) {
SimpleName name1 = object1.getName();
SimpleName name2 = object2.getName();
return maybeRefactorExpression(visited, targetType.getTypeArguments()[0], classesToUseWithImport, name1, name2, (Expression) visited.getBody());
}
}
return true;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteCleanUp method maybeRefactorBlock.
/**
* Maybe refactor the block.
*
* @param node The node
* @param classesToUseWithImport The classes to use with import
* @param importsToAdd The imports to add
* @return True to visit subtree
*/
protected boolean maybeRefactorBlock(final Block node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ObjectInstantiationVisitor classCreationVisitor = new ObjectInstantiationVisitor(node);
node.accept(classCreationVisitor);
for (ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
List<VariableDeclaration> varDecls = new ArrayList<>();
List<MethodInvocation> methodCallsToRefactor = new ArrayList<>();
if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, instanceCreation.resolveTypeBinding(), varDecls, methodCallsToRefactor) && canCodeBeRefactored()) {
replaceClass(instanceCreation, varDecls, methodCallsToRefactor, classesToUseWithImport, importsToAdd);
return false;
}
}
return true;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class InlineTempRefactoring method removeTemp.
private void removeTemp(CompilationUnitRewrite cuRewrite) {
VariableDeclaration variableDeclaration = getVariableDeclaration();
TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_remove_edit_name);
ASTNode parent = variableDeclaration.getParent();
ASTRewrite rewrite = cuRewrite.getASTRewrite();
TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
if (parent instanceof VariableDeclarationStatement && ((VariableDeclarationStatement) parent).fragments().size() == 1) {
sourceRangeComputer.addTightSourceNode(parent);
rewrite.remove(parent, groupDesc);
} else {
sourceRangeComputer.addTightSourceNode(variableDeclaration);
rewrite.remove(variableDeclaration, groupDesc);
}
}
Aggregations