use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationTransformationsTest method replacingFieldWithAnotherField.
@Test
public void replacingFieldWithAnotherField() throws IOException {
ClassOrInterfaceDeclaration cid = consider("public class A {float f;}");
cid.getMembers().set(0, new FieldDeclaration(EnumSet.noneOf(Modifier.class), new VariableDeclarator(PrimitiveType.intType(), "bar")));
assertTransformedToString("public class A {int bar;}", cid);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class TransformationsTest method example8.
@Test
public void example8() throws IOException {
considerExample("Example8_original");
FieldDeclaration fd = (FieldDeclaration) cu.getClassByName("A").get().getMember(0).asFieldDeclaration();
fd.addVariable(new VariableDeclarator(PrimitiveType.intType(), "b"));
assertTransformed("Example8", cu);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class TransformationsTest method example9.
@Test
public void example9() throws IOException {
considerExample("Example9_original");
FieldDeclaration fd = (FieldDeclaration) cu.getClassByName("A").get().getMember(0).asFieldDeclaration();
fd.addVariable(new VariableDeclarator(new ArrayType(PrimitiveType.intType()), "b"));
assertTransformed("Example9", cu);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class Issue241 method testSolveStaticallyImportedMemberType.
@Test
public void testSolveStaticallyImportedMemberType() {
File src = adaptPath(new File("src/test/resources"));
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
CompilationUnit cu = parseSample("Issue241");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
VariableDeclarator v = Navigator.demandVariableDeclaration(cls, "foo").get();
Type t = v.getType();
ResolvedType t2 = javaParserFacade.convert(t, t);
String typeName = t2.asReferenceType().getQualifiedName();
assertEquals("issue241.TypeWithMemberType.MemberInterface", typeName);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class NodeWithMembers method addField.
/**
* Add a field to this
*
* @param type the type of the field
* @param name the name of the field
* @param modifiers the modifiers like {@link Modifier#PUBLIC}
* @return the {@link FieldDeclaration} created
*/
default FieldDeclaration addField(Type<?> type, String name, Modifier... modifiers) {
FieldDeclaration fieldDeclaration = new FieldDeclaration();
fieldDeclaration.setParentNode((Node) this);
VariableDeclarator variable = new VariableDeclarator(new VariableDeclaratorId(name));
fieldDeclaration.getVariables().add(variable);
variable.setParentNode(fieldDeclaration);
fieldDeclaration.setModifiers(Arrays.stream(modifiers).collect(toCollection(() -> EnumSet.noneOf(Modifier.class))));
variable.setType(type);
getMembers().add(fieldDeclaration);
return fieldDeclaration;
}
Aggregations