use of org.eclipse.jdt.core.dom.SingleVariableDeclaration 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.SingleVariableDeclaration in project eclipse-cs by checkstyle.
the class FinalParametersQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(SingleVariableDeclaration node) {
if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
if (!Modifier.isFinal(node.getModifiers())) {
Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
node.modifiers().add(finalModifier);
}
}
return true;
}
};
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project whole by wholeplatform.
the class CompilationUnitBuilder method newSingleVariableDeclaration.
public SingleVariableDeclaration newSingleVariableDeclaration(Type pType, SimpleName pName) {
SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
param.setType(pType);
param.setName(pName);
return param;
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project evosuite by EvoSuite.
the class CodeGenerator method createSetFieldMethod.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void createSetFieldMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
// public static void setField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception
// {
// final Class<?> clazz = Class.forName(clazzName);
// final Field f = clazz.getDeclaredField(fieldName);
// f.setAccessible(true);
// f.set(receiver, value);
// }
// -- add necessary import statements
List imports = cu.imports();
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Field" }));
imports.add(id);
// -- create method frame: "public static void setProtectedField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception"
final MethodDeclaration md = ast.newMethodDeclaration();
td.bodyDeclarations().add(md);
md.setName(ast.newSimpleName("setField"));
List modifiers = md.modifiers();
modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
md.thrownExceptions().add(ast.newSimpleName("Exception"));
List parameters = md.parameters();
SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
svd.setName(ast.newSimpleName("clazzName"));
parameters.add(svd);
svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
svd.setName(ast.newSimpleName("fieldName"));
parameters.add(svd);
svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
svd.setName(ast.newSimpleName("receiver"));
parameters.add(svd);
svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
svd.setName(ast.newSimpleName("value"));
parameters.add(svd);
// -- create method body
// final Class<?> clazz = Class.forName(clazzName);
// final Field f = clazz.getDeclaredField(fieldName);
// f.setAccessible(true);
// f.set(receiver, value);
final Block methodBlock = ast.newBlock();
md.setBody(methodBlock);
final List methodStmts = methodBlock.statements();
// final Class clazz = Class.forName(clazzName);
MethodInvocation init = ast.newMethodInvocation();
init.setName(ast.newSimpleName("forName"));
init.setExpression(ast.newSimpleName("Class"));
init.arguments().add(ast.newSimpleName("clazzName"));
VariableDeclarationFragment varDeclFrag = ast.newVariableDeclarationFragment();
varDeclFrag.setName(ast.newSimpleName("clazz"));
varDeclFrag.setInitializer(init);
VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
methodStmts.add(varDeclStmt);
// final Field f = clazz.getDeclaredField(fieldName);
init = ast.newMethodInvocation();
init.setName(ast.newSimpleName("getDeclaredField"));
init.setExpression(ast.newSimpleName("clazz"));
init.arguments().add(ast.newSimpleName("fieldName"));
varDeclFrag = ast.newVariableDeclarationFragment();
varDeclFrag.setName(ast.newSimpleName("f"));
varDeclFrag.setInitializer(init);
varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Field")));
methodStmts.add(varDeclStmt);
// f.setAccessible(true);
MethodInvocation minv = ast.newMethodInvocation();
minv.setName(ast.newSimpleName("setAccessible"));
minv.setExpression(ast.newSimpleName("f"));
minv.arguments().add(ast.newBooleanLiteral(true));
methodStmts.add(ast.newExpressionStatement(minv));
// f.set(receiver, value);
minv = ast.newMethodInvocation();
minv.setName(ast.newSimpleName("set"));
minv.setExpression(ast.newSimpleName("f"));
minv.arguments().add(ast.newSimpleName("receiver"));
minv.arguments().add(ast.newSimpleName("value"));
methodStmts.add(ast.newExpressionStatement(minv));
}
use of org.eclipse.jdt.core.dom.SingleVariableDeclaration in project evosuite by EvoSuite.
the class CodeGenerator method createGetFieldMethod.
@SuppressWarnings({ "rawtypes", "unchecked" })
private void createGetFieldMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
// public static void setField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception
// {
// final Class<?> clazz = Class.forName(clazzName);
// final Field f = clazz.getDeclaredField(fieldName);
// f.setAccessible(true);
// f.set(receiver, value);
// }
// -- add necessary import statements
List imports = cu.imports();
ImportDeclaration id = ast.newImportDeclaration();
id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Field" }));
imports.add(id);
// -- create method frame: "public static Object setProtectedField(final String clazzName, final String fieldName, final Object receiver) throws Exception"
final MethodDeclaration md = ast.newMethodDeclaration();
td.bodyDeclarations().add(md);
md.setName(ast.newSimpleName("getField"));
List modifiers = md.modifiers();
modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
md.thrownExceptions().add(ast.newSimpleName("Exception"));
List parameters = md.parameters();
SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
svd.setName(ast.newSimpleName("clazzName"));
parameters.add(svd);
svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
svd.setName(ast.newSimpleName("fieldName"));
parameters.add(svd);
svd = ast.newSingleVariableDeclaration();
svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
svd.setName(ast.newSimpleName("receiver"));
parameters.add(svd);
md.setReturnType2(ast.newSimpleType(ast.newSimpleName("Object")));
// -- create method body
// final Class<?> clazz = Class.forName(clazzName);
// final Field f = clazz.getDeclaredField(fieldName);
// f.setAccessible(true);
// return f.get(receiver);
final Block methodBlock = ast.newBlock();
md.setBody(methodBlock);
final List methodStmts = methodBlock.statements();
// final Class clazz = Class.forName(clazzName);
MethodInvocation init = ast.newMethodInvocation();
init.setName(ast.newSimpleName("forName"));
init.setExpression(ast.newSimpleName("Class"));
init.arguments().add(ast.newSimpleName("clazzName"));
VariableDeclarationFragment varDeclFrag = ast.newVariableDeclarationFragment();
varDeclFrag.setName(ast.newSimpleName("clazz"));
varDeclFrag.setInitializer(init);
VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
methodStmts.add(varDeclStmt);
// final Field f = clazz.getDeclaredField(fieldName);
init = ast.newMethodInvocation();
init.setName(ast.newSimpleName("getDeclaredField"));
init.setExpression(ast.newSimpleName("clazz"));
init.arguments().add(ast.newSimpleName("fieldName"));
varDeclFrag = ast.newVariableDeclarationFragment();
varDeclFrag.setName(ast.newSimpleName("f"));
varDeclFrag.setInitializer(init);
varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Field")));
methodStmts.add(varDeclStmt);
// f.setAccessible(true);
MethodInvocation minv = ast.newMethodInvocation();
minv.setName(ast.newSimpleName("setAccessible"));
minv.setExpression(ast.newSimpleName("f"));
minv.arguments().add(ast.newBooleanLiteral(true));
methodStmts.add(ast.newExpressionStatement(minv));
// return f.get(receiver);
minv = ast.newMethodInvocation();
minv.setName(ast.newSimpleName("get"));
minv.setExpression(ast.newSimpleName("f"));
minv.arguments().add(ast.newSimpleName("receiver"));
final ReturnStatement returnStmt = ast.newReturnStatement();
returnStmt.setExpression(minv);
methodStmts.add(returnStmt);
}
Aggregations