use of org.eclipse.jdt.core.dom.VariableDeclaration in project che by eclipse.
the class SurroundWithTryCatchRefactoring method getSpecialVariableDeclarationStatements.
private List<ASTNode> getSpecialVariableDeclarationStatements() {
List<ASTNode> result = new ArrayList<ASTNode>(3);
VariableDeclaration[] locals = fAnalyzer.getAffectedLocals();
for (int i = 0; i < locals.length; i++) {
ASTNode parent = locals[i].getParent();
if (parent instanceof VariableDeclarationStatement && !result.contains(parent))
result.add(parent);
}
return result;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method replaceClass.
private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactor) {
final ASTBuilder b = ctx.getASTBuilder();
final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
if (substituteType != null) {
ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
originalInstanceCreation.setType(substituteType);
}
for (final MethodInvocation methodCall : methodCallsToRefactor) {
final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
refactorMethod(b, methodCall, copyOfMethodCall);
ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
}
for (final VariableDeclaration variableDecl : variableDecls) {
final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl.getParent();
final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(), (ASTNode) oldDeclareStmt.fragments().get(0));
if (substituteVarType != null) {
ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
}
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method visit.
@Override
public boolean visit(Block node) {
final ObjectInstantiationVisitor classCreationVisitor = new ObjectInstantiationVisitor(node);
node.accept(classCreationVisitor);
for (final ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
final List<VariableDeclaration> varDecls = new ArrayList<VariableDeclaration>();
final List<MethodInvocation> methodCallsToRefactor = new ArrayList<MethodInvocation>();
if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, instanceCreation.resolveTypeBinding(), varDecls, methodCallsToRefactor) && canCodeBeRefactored()) {
replaceClass(instanceCreation, varDecls, methodCallsToRefactor);
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project fabric8 by jboss-fuse.
the class AddSwaggerAnnotationMojo method addSwaggerApiAnnotation.
private void addSwaggerApiAnnotation(CompilationUnit unit, AnnotationVisitor visitor, File file, Document document) throws JavaModelException, IllegalArgumentException, MalformedTreeException, BadLocationException, IOException {
AST ast = unit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
ListRewrite listRewrite = rewriter.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY);
NormalAnnotation normalAnnotation = rewriter.getAST().newNormalAnnotation();
Name name = ast.newName("com.wordnik.swagger.annotations.Api");
normalAnnotation.setTypeName(name);
MemberValuePair memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
StringLiteral stringLiteral = ast.newStringLiteral();
String rootPath = visitor.getRootPath();
rootPath = rootPath.substring(1, rootPath.length() - 1);
if (rootPath.endsWith("/")) {
rootPath = rootPath.substring(0, rootPath.length() - 1);
}
stringLiteral.setLiteralValue(rootPath);
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("description"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue("Operations about " + visitor.getRestServiceClass());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
listRewrite.insertAt(normalAnnotation, 0, null);
for (MethodDeclaration method : visitor.getRestMethod()) {
listRewrite = rewriter.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
normalAnnotation = rewriter.getAST().newNormalAnnotation();
name = ast.newName("com.wordnik.swagger.annotations.ApiOperation");
normalAnnotation.setTypeName(name);
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(method.getName().toString());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
Javadoc doc = method.getJavadoc();
String comment = null;
if (doc != null) {
comment = method.getJavadoc().toString();
}
if (comment != null && comment.length() > 0) {
// add notes from method java doc
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("notes"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(comment);
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
}
listRewrite.insertAt(normalAnnotation, 0, null);
listRewrite = rewriter.getListRewrite((ASTNode) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0), SingleVariableDeclaration.MODIFIERS2_PROPERTY);
normalAnnotation = rewriter.getAST().newNormalAnnotation();
name = ast.newName("com.wordnik.swagger.annotations.ApiParam");
normalAnnotation.setTypeName(name);
((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName();
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName().toString());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
listRewrite.insertAt(normalAnnotation, 0, null);
}
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
FileUtils.writeStringToFile(file, document.get());
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method convertMethodRefernceToLambda.
/**
* Converts and replaces the given method reference with corresponding lambda
* expression in the given ASTRewrite.
*
* @param methodReference
* the method reference to convert
* @param functionalMethod
* the non-generic functional interface method to be implemented by
* the lambda expression
* @param astRoot
* the AST root
* @param rewrite
* the ASTRewrite
* @param linkedProposalModel
* to create linked proposals for lambda's parameters or
* <code>null</code> if linked proposals are not required
* @param createBlockBody
* <code>true</code> if lambda expression's body should be a block
*
* @return lambda expression used to replace the method reference in the given
* ASTRewrite
* @throws JavaModelException
* if an exception occurs while accessing the Java element
* corresponding to the <code>functionalMethod</code>
*/
public static LambdaExpression convertMethodRefernceToLambda(MethodReference methodReference, IMethodBinding functionalMethod, CompilationUnit astRoot, ASTRewrite rewrite, LinkedProposalModel linkedProposalModel, boolean createBlockBody) throws JavaModelException {
AST ast = astRoot.getAST();
LambdaExpression lambda = ast.newLambdaExpression();
String[] lambdaParamNames = getUniqueParameterNames(methodReference, functionalMethod);
List<VariableDeclaration> lambdaParameters = lambda.parameters();
for (int i = 0; i < lambdaParamNames.length; i++) {
String paramName = lambdaParamNames[i];
VariableDeclarationFragment lambdaParameter = ast.newVariableDeclarationFragment();
SimpleName name = ast.newSimpleName(paramName);
lambdaParameter.setName(name);
lambdaParameters.add(lambdaParameter);
if (linkedProposalModel != null) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), true).addPosition(rewrite.track(name), i == 0);
}
}
int noOfLambdaParameters = lambdaParamNames.length;
lambda.setParentheses(noOfLambdaParameters != 1);
ITypeBinding returnTypeBinding = functionalMethod.getReturnType();
// too often null, see bug 440000, bug 440344, bug 333665
IMethodBinding referredMethodBinding = methodReference.resolveMethodBinding();
if (methodReference instanceof CreationReference) {
CreationReference creationRef = (CreationReference) methodReference;
Type type = creationRef.getType();
if (type instanceof ArrayType) {
ArrayCreation arrayCreation = ast.newArrayCreation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(arrayCreation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(arrayCreation);
}
ArrayType arrayType = (ArrayType) type;
Type copiedElementType = (Type) rewrite.createCopyTarget(arrayType.getElementType());
arrayCreation.setType(ast.newArrayType(copiedElementType, arrayType.getDimensions()));
SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
arrayCreation.dimensions().add(name);
if (linkedProposalModel != null) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
} else {
ClassInstanceCreation cic = ast.newClassInstanceCreation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(cic, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(cic);
}
ITypeBinding typeBinding = type.resolveBinding();
if (!(type instanceof ParameterizedType) && typeBinding != null && typeBinding.getTypeDeclaration().isGenericType()) {
cic.setType(ast.newParameterizedType((Type) rewrite.createCopyTarget(type)));
} else {
cic.setType((Type) rewrite.createCopyTarget(type));
}
List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
cic.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
cic.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
}
} else if (referredMethodBinding != null && Modifier.isStatic(referredMethodBinding.getModifiers())) {
MethodInvocation methodInvocation = ast.newMethodInvocation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(methodInvocation);
}
Expression expr = null;
boolean hasConflict = hasConflict(methodReference.getStartPosition(), referredMethodBinding, ScopeAnalyzer.METHODS | ScopeAnalyzer.CHECK_VISIBILITY, astRoot);
if (hasConflict || !Bindings.isSuperType(referredMethodBinding.getDeclaringClass(), ASTNodes.getEnclosingType(methodReference)) || methodReference.typeArguments().size() != 0) {
if (methodReference instanceof ExpressionMethodReference) {
ExpressionMethodReference expressionMethodReference = (ExpressionMethodReference) methodReference;
expr = (Expression) rewrite.createCopyTarget(expressionMethodReference.getExpression());
} else if (methodReference instanceof TypeMethodReference) {
Type type = ((TypeMethodReference) methodReference).getType();
ITypeBinding typeBinding = type.resolveBinding();
if (typeBinding != null) {
ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(astRoot, true);
expr = ast.newName(importRewrite.addImport(typeBinding));
}
}
}
methodInvocation.setExpression(expr);
SimpleName methodName = getMethodInvocationName(methodReference);
methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
methodInvocation.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
} else if (methodReference instanceof SuperMethodReference) {
SuperMethodInvocation superMethodInvocation = ast.newSuperMethodInvocation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(superMethodInvocation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(superMethodInvocation);
}
Name superQualifier = ((SuperMethodReference) methodReference).getQualifier();
if (superQualifier != null) {
superMethodInvocation.setQualifier((Name) rewrite.createCopyTarget(superQualifier));
}
SimpleName methodName = getMethodInvocationName(methodReference);
superMethodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
List<SimpleName> invocationArgs = getInvocationArguments(ast, 0, noOfLambdaParameters, lambdaParamNames);
superMethodInvocation.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
superMethodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
} else {
MethodInvocation methodInvocation = ast.newMethodInvocation();
if (createBlockBody) {
Block blockBody = getBlockBodyForLambda(methodInvocation, returnTypeBinding, ast);
lambda.setBody(blockBody);
} else {
lambda.setBody(methodInvocation);
}
boolean isTypeReference = isTypeReferenceToInstanceMethod(methodReference);
if (isTypeReference) {
SimpleName name = ast.newSimpleName(lambdaParamNames[0]);
methodInvocation.setExpression(name);
if (linkedProposalModel != null) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
} else {
Expression expr = ((ExpressionMethodReference) methodReference).getExpression();
if (!(expr instanceof ThisExpression && methodReference.typeArguments().size() == 0)) {
methodInvocation.setExpression((Expression) rewrite.createCopyTarget(expr));
}
}
SimpleName methodName = getMethodInvocationName(methodReference);
methodInvocation.setName((SimpleName) rewrite.createCopyTarget(methodName));
List<SimpleName> invocationArgs = getInvocationArguments(ast, isTypeReference ? 1 : 0, noOfLambdaParameters, lambdaParamNames);
methodInvocation.arguments().addAll(invocationArgs);
if (linkedProposalModel != null) {
for (SimpleName name : invocationArgs) {
linkedProposalModel.getPositionGroup(name.getIdentifier(), false).addPosition(rewrite.track(name), LinkedPositionGroup.NO_STOP);
}
}
methodInvocation.typeArguments().addAll(getCopiedTypeArguments(rewrite, methodReference.typeArguments()));
}
rewrite.replace(methodReference, lambda, null);
return lambda;
}
Aggregations