use of org.eclipse.jdt.core.dom.VariableDeclaration in project AutoRefactor by JnRouvignac.
the class AbstractClassSubstituteRefactoring method canVarOccurrenceBeRefactored0.
private boolean canVarOccurrenceBeRefactored0(final Block node, final List<VariableDeclaration> varDecls, final List<MethodInvocation> methodCallsToRefactor, final List<VariableDeclaration> otherVarDecls) {
for (final VariableDeclaration varDecl : varDecls) {
final VarOccurrenceVisitor varOccurrenceVisitor = new VarOccurrenceVisitor(varDecl);
final Statement parent = getAncestorOrNull(varDecl, Statement.class);
Statement nextSibling = getNextSibling(parent);
while (nextSibling != null) {
varOccurrenceVisitor.visitNode(nextSibling);
nextSibling = getNextSibling(nextSibling);
}
if (varOccurrenceVisitor.isUsedInAnnonymousClass()) {
return false;
}
for (final SimpleName varOccurrence : varOccurrenceVisitor.getVarOccurrences()) {
final List<VariableDeclaration> subVarDecls = new ArrayList<VariableDeclaration>();
if (!canBeRefactored(node, varOccurrence, varOccurrence.resolveTypeBinding(), subVarDecls, methodCallsToRefactor)) {
return false;
}
otherVarDecls.addAll(subVarDecls);
}
}
return true;
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.
public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) throws CoreException {
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration == null) {
return;
}
ICompilationUnit cu = context.getCompilationUnit();
ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
if (binding == null) {
return;
}
if (declaration instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declaration;
IMethodBinding methodBinding = methodDecl.resolveBinding();
IMethodBinding overridden = null;
if (methodBinding != null) {
overridden = Bindings.findOverriddenMethod(methodBinding, true);
}
String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
}
} else if (declaration instanceof AbstractTypeDeclaration) {
String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
String[] typeParamNames;
if (declaration instanceof TypeDeclaration) {
List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
typeParamNames = new String[typeParams.size()];
for (int i = 0; i < typeParamNames.length; i++) {
typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
}
} else {
typeParamNames = new String[0];
}
String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
}
} else if (declaration instanceof FieldDeclaration) {
// $NON-NLS-1$
String comment = "/**\n *\n */\n";
List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
if (fragments != null && fragments.size() > 0) {
VariableDeclaration decl = fragments.get(0);
String fieldName = decl.getName().getIdentifier();
String typeName = binding.getName();
comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
}
if (comment != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
}
} else if (declaration instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
String id = enumDecl.getName().getIdentifier();
String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse.jdt.ls by eclipse.
the class ExtractMethodAnalyzer method initReturnType.
private void initReturnType(ImportRewrite rewriter) {
AST ast = fEnclosingBodyDeclaration.getAST();
fReturnType = null;
fReturnTypeBinding = null;
switch(fReturnKind) {
case ACCESS_TO_LOCAL:
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
if (declaration.resolveBinding() != null) {
fReturnTypeBinding = declaration.resolveBinding().getType();
}
break;
case EXPRESSION:
Expression expression = (Expression) getFirstSelectedNode();
if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
} else {
fExpressionBinding = expression.resolveTypeBinding();
}
if (fExpressionBinding != null) {
if (fExpressionBinding.isNullType()) {
getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
} else {
ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
if (normalizedBinding != null) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
fReturnType = rewriter.addImport(normalizedBinding, ast, context, TypeLocation.RETURN_TYPE);
fReturnTypeBinding = normalizedBinding;
}
}
} else {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
// $NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
}
break;
case RETURN_STATEMENT_VALUE:
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
}
break;
default:
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
// $NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
if (fReturnType == null) {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
// $NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project eclipse-cs by checkstyle.
the class ArrayTypeStyleQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@Override
public boolean visit(VariableDeclarationStatement node) {
if (containsPosition(node, markerStartOffset)) {
if (isCStyle(node.fragments())) {
int dimensions = 0;
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
if (decl.getExtraDimensions() > dimensions) {
dimensions = decl.getExtraDimensions();
}
decl.setExtraDimensions(0);
}
// wrap current type into ArrayType
ArrayType arrayType = createArrayType(node.getType(), dimensions);
node.setType(arrayType);
} else if (isJavaStyle(node.getType())) {
int dimensions = ((ArrayType) node.getType()).getDimensions();
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
decl.setExtraDimensions(dimensions);
}
Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
node.setType(elementType);
}
}
return true;
}
@Override
public boolean visit(SingleVariableDeclaration node) {
if (containsPosition(node, markerStartOffset)) {
if (isCStyle(node)) {
// wrap the existing type into an array type
node.setType(createArrayType(node.getType(), node.getExtraDimensions()));
node.setExtraDimensions(0);
} else if (isJavaStyle(node.getType())) {
ArrayType arrayType = (ArrayType) node.getType();
Type elementType = (Type) ASTNode.copySubtree(node.getAST(), arrayType.getElementType());
node.setType(elementType);
node.setExtraDimensions(arrayType.getDimensions());
}
}
return true;
}
@Override
public boolean visit(FieldDeclaration node) {
if (containsPosition(node, markerStartOffset)) {
if (isCStyle(node.fragments())) {
int dimensions = 0;
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
if (decl.getExtraDimensions() > dimensions) {
dimensions = decl.getExtraDimensions();
}
decl.setExtraDimensions(0);
}
// wrap current type into ArrayType
ArrayType arrayType = createArrayType(node.getType(), dimensions);
node.setType(arrayType);
} else if (isJavaStyle(node.getType())) {
int dimensions = ((ArrayType) node.getType()).getDimensions();
List<?> fragments = node.fragments();
for (int i = 0, size = fragments.size(); i < size; i++) {
VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
decl.setExtraDimensions(dimensions);
}
Type elementType = (Type) ASTNode.copySubtree(node.getAST(), ((ArrayType) node.getType()).getElementType());
node.setType(elementType);
}
}
return true;
}
private boolean isJavaStyle(Type type) {
return type instanceof ArrayType;
}
private boolean isCStyle(VariableDeclaration decl) {
return decl.getExtraDimensions() > 0;
}
private boolean isCStyle(List<?> fragments) {
Iterator<?> it = fragments.iterator();
while (it.hasNext()) {
VariableDeclaration decl = (VariableDeclaration) it.next();
if (isCStyle(decl)) {
return true;
}
}
return false;
}
private ArrayType createArrayType(Type componentType, int dimensions) {
Type type = (Type) ASTNode.copySubtree(componentType.getAST(), componentType);
ArrayType arrayType = componentType.getAST().newArrayType(type, dimensions);
return arrayType;
}
};
}
use of org.eclipse.jdt.core.dom.VariableDeclaration in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveVariableReference.
/**
* <p>
* retrieveVariableReference
* </p>
*
* @param argument
* a {@link java.lang.Object} object.
* @param varType
* a {@link java.lang.Class} object.
* @return a {@link org.evosuite.testcase.VariableReference} object.
*/
protected VariableReference retrieveVariableReference(Object argument, Class<?> varType) {
if (argument instanceof ClassInstanceCreation) {
return retrieveVariableReference((ClassInstanceCreation) argument, varType);
}
if (argument instanceof VariableDeclarationFragment) {
return retrieveVariableReference((VariableDeclarationFragment) argument);
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
lineNumber = testReader.getLineNumber(simpleName.getStartPosition());
return retrieveVariableReference(simpleName.resolveBinding(), varType);
}
if (argument instanceof IVariableBinding) {
return retrieveVariableReference((IVariableBinding) argument, varType);
}
if (argument instanceof PrefixExpression) {
return retrieveVariableReference((PrefixExpression) argument);
}
if (argument instanceof InfixExpression) {
return retrieveVariableReference((InfixExpression) argument, varType);
}
if (argument instanceof ExpressionStatement) {
ExpressionStatement exprStmt = (ExpressionStatement) argument;
Expression expression = exprStmt.getExpression();
return retrieveVariableReference(expression, varType);
}
if (argument instanceof NullLiteral) {
return retrieveVariableReference((NullLiteral) argument, varType);
}
if (argument instanceof StringLiteral) {
return retrieveVariableReference((StringLiteral) argument);
}
if (argument instanceof NumberLiteral) {
return retrieveVariableReference((NumberLiteral) argument);
}
if (argument instanceof CharacterLiteral) {
return retrieveVariableReference((CharacterLiteral) argument);
}
if (argument instanceof BooleanLiteral) {
return retrieveVariableReference((BooleanLiteral) argument);
}
if (argument instanceof ITypeBinding) {
if (varType != null) {
return new ValidVariableReference(testCase.getReference(), varType);
}
return new ValidVariableReference(testCase.getReference(), retrieveTypeClass(argument));
}
if (argument instanceof QualifiedName) {
return retrieveVariableReference((QualifiedName) argument);
}
if (argument instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) argument;
VariableReference result = retrieveResultReference(methodInvocation);
nestedCallResults.push(result);
return result;
}
if (argument instanceof ArrayCreation) {
return retrieveVariableReference((ArrayCreation) argument);
}
if (argument instanceof VariableDeclaration) {
return retrieveVariableReference((VariableDeclaration) argument);
}
if (argument instanceof ArrayAccess) {
// argument).getArray(), null);
return retrieveVariableReference((ArrayAccess) argument);
}
if (argument instanceof Assignment) {
return retrieveVariableReference(((Assignment) argument).getLeftHandSide(), null);
}
if (argument instanceof CastExpression) {
CastExpression castExpression = (CastExpression) argument;
VariableReference result = retrieveVariableReference(castExpression.getExpression(), null);
Class<?> castClass = retrieveTypeClass(castExpression.resolveTypeBinding());
assert castClass.isAssignableFrom(toClass(result.getType()));
result.setType(castClass);
return result;
}
throw new UnsupportedOperationException("Argument type " + argument.getClass() + " not implemented!");
}
Aggregations