use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class ASTHelper method createVariableDeclarationExpr.
/**
* Creates a {@link VariableDeclarationExpr}.
*
* @param type
* type
* @param name
* name
* @return instance of {@link VariableDeclarationExpr}
*/
public static VariableDeclarationExpr createVariableDeclarationExpr(Type type, String name) {
List<VariableDeclarator> vars = new ArrayList<VariableDeclarator>();
vars.add(new VariableDeclarator(new VariableDeclaratorId(name)));
return new VariableDeclarationExpr(type, vars);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class VarValidator method accept.
@Override
public void accept(VarType node, ProblemReporter reporter) {
// All allowed locations are within a VariableDeclaration inside a VariableDeclarationExpr inside something else.
Optional<VariableDeclarator> variableDeclarator = node.findParent(VariableDeclarator.class);
if (!variableDeclarator.isPresent()) {
// Java 11's var in lambda's
if (varAllowedInLambdaParameters) {
boolean valid = node.findParent(Parameter.class).flatMap(Node::getParentNode).map((Node p) -> p instanceof LambdaExpr).orElse(false);
if (valid) {
return;
}
}
reportIllegalPosition(node, reporter);
return;
}
variableDeclarator.ifPresent(vd -> {
Optional<Node> variableDeclarationExpr = vd.getParentNode();
if (!variableDeclarationExpr.isPresent()) {
reportIllegalPosition(node, reporter);
return;
}
variableDeclarationExpr.ifPresent(vdeNode -> {
if (!(vdeNode instanceof VariableDeclarationExpr)) {
reportIllegalPosition(node, reporter);
return;
}
VariableDeclarationExpr vde = (VariableDeclarationExpr) vdeNode;
if (vde.getVariables().size() > 1) {
reporter.report(vde, "\"var\" only takes a single variable.");
}
Optional<Node> container = vdeNode.getParentNode();
if (!container.isPresent()) {
reportIllegalPosition(node, reporter);
return;
}
container.ifPresent(c -> {
boolean positionIsFine = c instanceof ForStmt || c instanceof ForeachStmt || c instanceof ExpressionStmt;
if (!positionIsFine) {
reportIllegalPosition(node, reporter);
}
// A local variable declaration ends up inside an ExpressionStmt.
if (c instanceof ExpressionStmt) {
if (!vd.getInitializer().isPresent()) {
reporter.report(node, "\"var\" needs an initializer.");
}
vd.getInitializer().ifPresent(initializer -> {
if (initializer instanceof NullLiteralExpr) {
reporter.report(node, "\"var\" cannot infer type from just null.");
}
if (initializer instanceof ArrayCreationExpr) {
reporter.report(node, "\"var\" cannot infer array types.");
}
});
}
});
});
});
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class NodeWithVariables method getCommonType.
/**
* Returns the type that is shared between all variables.
* This is a shortcut for when you are certain that all variables share one type.
* What makes this difficult is arrays, and being able to set the type.
* <br/>For <code>int a;</code> this is int.
* <br/>For <code>int a,b,c,d;</code> this is also int.
* <br/>For <code>int a,b[],c;</code> this is an assertion error since b is an int[], not an int.
* <br/>For <code>int a,b;</code>, then doing setType(String) on b, this is an assertion error. It is also a situation that you don't really want.
*/
default Type getCommonType() {
NodeList<VariableDeclarator> variables = getVariables();
if (variables.isEmpty()) {
throw new AssertionError("There is no common type since there are no variables.");
}
Type type = variables.get(0).getType();
for (int i = 1; i < variables.size(); i++) {
if (!variables.get(i).getType().equals(type)) {
throw new AssertionError("The variables do not have a common type.");
}
}
return type;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class GenericsResolutionTest method resolveFieldWithGenericTypeToString.
@Test
public void resolveFieldWithGenericTypeToString() {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
VariableDeclarator fieldS = Navigator.demandField(clazz, "s");
SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("s", fieldS);
assertEquals(true, symbolReference.isPresent());
assertEquals("s", symbolReference.get().getName());
ResolvedType type = symbolReference.get().getType();
assertEquals(1, type.asReferenceType().typeParametersValues().size());
assertEquals("java.lang.String", type.asReferenceType().typeParametersValues().get(0).describe());
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class GenericsResolutionTest method resolveUsageOfGenericFieldIntermediateCase.
// PRIMA UN TEST CHE DICA CHE IL TIPO DEL CAMPO AS e' LIST<A> NON LIST<E>
@Test
public void resolveUsageOfGenericFieldIntermediateCase() {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
VariableDeclarator field = Navigator.demandField(clazz, "as");
ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(field);
assertEquals(false, type.isTypeVariable());
assertEquals("java.util.List<A>", type.describe());
assertEquals(1, type.asReferenceType().typeParametersValues().size());
assertEquals(true, type.asReferenceType().typeParametersValues().get(0).isTypeVariable());
assertEquals("A", type.asReferenceType().typeParametersValues().get(0).describe());
}
Aggregations