use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class ASTHelper method createFieldDeclaration.
/**
* Creates a {@link FieldDeclaration}.
*
* @param modifiers
* modifiers
* @param type
* type
* @param variable
* variable declarator
* @return instance of {@link FieldDeclaration}
*/
public static FieldDeclaration createFieldDeclaration(int modifiers, Type type, VariableDeclarator variable) {
List<VariableDeclarator> variables = new ArrayList<VariableDeclarator>();
variables.add(variable);
FieldDeclaration ret = new FieldDeclaration(modifiers, type, variables);
return ret;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class LambdaExprContext method solveSymbolAsValue.
@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
for (Parameter parameter : wrappedNode.getParameters()) {
SymbolDeclarator sb = JavaParserFactory.getSymbolDeclarator(parameter, typeSolver);
int index = 0;
for (ResolvedValueDeclaration decl : sb.getSymbolDeclarations()) {
if (decl.getName().equals(name)) {
if (requireParentNode(wrappedNode) instanceof MethodCallExpr) {
MethodCallExpr methodCallExpr = (MethodCallExpr) requireParentNode(wrappedNode);
MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(methodCallExpr);
int i = pos(methodCallExpr, wrappedNode);
ResolvedType lambdaType = methodUsage.getParamTypes().get(i);
// Get the functional method in order for us to resolve it's type arguments properly
Optional<MethodUsage> functionalMethodOpt = FunctionalInterfaceLogic.getFunctionalMethod(lambdaType);
if (functionalMethodOpt.isPresent()) {
MethodUsage functionalMethod = functionalMethodOpt.get();
InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
// Resolve each type variable of the lambda, and use this later to infer the type of each
// implicit parameter
inferenceContext.addPair(lambdaType, new ReferenceTypeImpl(lambdaType.asReferenceType().getTypeDeclaration(), typeSolver));
// Find the position of this lambda argument
boolean found = false;
int lambdaParamIndex;
for (lambdaParamIndex = 0; lambdaParamIndex < wrappedNode.getParameters().size(); lambdaParamIndex++) {
if (wrappedNode.getParameter(lambdaParamIndex).getName().getIdentifier().equals(name)) {
found = true;
break;
}
}
if (!found) {
return Optional.empty();
}
// Now resolve the argument type using the inference context
ResolvedType argType = inferenceContext.resolve(inferenceContext.addSingle(functionalMethod.getParamType(lambdaParamIndex)));
ResolvedLambdaConstraintType conType;
if (argType.isWildcard()) {
conType = ResolvedLambdaConstraintType.bound(argType.asWildcard().getBoundedType());
} else {
conType = ResolvedLambdaConstraintType.bound(argType);
}
Value value = new Value(conType, name);
return Optional.of(value);
} else {
return Optional.empty();
}
} else if (requireParentNode(wrappedNode) instanceof VariableDeclarator) {
VariableDeclarator variableDeclarator = (VariableDeclarator) requireParentNode(wrappedNode);
ResolvedType t = JavaParserFacade.get(typeSolver).convertToUsageVariableType(variableDeclarator);
Optional<MethodUsage> functionalMethod = FunctionalInterfaceLogic.getFunctionalMethod(t);
if (functionalMethod.isPresent()) {
ResolvedType lambdaType = functionalMethod.get().getParamType(index);
// Replace parameter from declarator
Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
if (lambdaType.isReferenceType()) {
for (com.github.javaparser.utils.Pair<ResolvedTypeParameterDeclaration, ResolvedType> entry : lambdaType.asReferenceType().getTypeParametersMap()) {
if (entry.b.isTypeVariable() && entry.b.asTypeParameter().declaredOnType()) {
ResolvedType ot = t.asReferenceType().typeParametersMap().getValue(entry.a);
lambdaType = lambdaType.replaceTypeVariables(entry.a, ot, inferredTypes);
}
}
} else if (lambdaType.isTypeVariable() && lambdaType.asTypeParameter().declaredOnType()) {
lambdaType = t.asReferenceType().typeParametersMap().getValue(lambdaType.asTypeParameter());
}
Value value = new Value(lambdaType, name);
return Optional.of(value);
} else {
throw new UnsupportedOperationException();
}
} else {
throw new UnsupportedOperationException();
}
}
index++;
}
}
// if nothing is found we should ask the parent context
return getParent().solveSymbolAsValue(name, typeSolver);
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class SourceFileInfoExtractor method solve.
private void solve(Node node) {
if (node instanceof ClassOrInterfaceDeclaration) {
solveTypeDecl((ClassOrInterfaceDeclaration) node);
} else if (node instanceof Expression) {
if ((requireParentNode(node) instanceof ImportDeclaration) || (requireParentNode(node) instanceof Expression) || (requireParentNode(node) instanceof MethodDeclaration) || (requireParentNode(node) instanceof PackageDeclaration)) {
// skip
} else if ((requireParentNode(node) instanceof Statement) || (requireParentNode(node) instanceof VariableDeclarator)) {
try {
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node);
out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
ok++;
} catch (UnsupportedOperationException upe) {
unsupported++;
err.println(upe.getMessage());
throw upe;
} catch (RuntimeException re) {
ko++;
err.println(re.getMessage());
throw re;
}
}
}
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class JavaParserSymbolDeclaration method getType.
@Override
public ResolvedType getType() {
if (wrappedNode instanceof Parameter) {
Parameter parameter = (Parameter) wrappedNode;
if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
int pos = getParamPos(parameter);
ResolvedType lambdaType = JavaParserFacade.get(typeSolver).getType(requireParentNode(wrappedNode));
// MethodDeclaration methodCalled = JavaParserFacade.get(typeSolver).solve()
throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
} else {
final ResolvedType rawType;
if (parameter.getType() instanceof com.github.javaparser.ast.type.PrimitiveType) {
rawType = ResolvedPrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) parameter.getType()).getType().name());
} else {
rawType = JavaParserFacade.get(typeSolver).convertToUsage(parameter.getType(), wrappedNode);
}
if (parameter.isVarArgs()) {
return new ResolvedArrayType(rawType);
}
return rawType;
}
} else if (wrappedNode instanceof VariableDeclarator) {
VariableDeclarator variableDeclarator = (VariableDeclarator) wrappedNode;
if (requireParentNode(wrappedNode) instanceof VariableDeclarationExpr) {
return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
} else if (requireParentNode(wrappedNode) instanceof FieldDeclaration) {
return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
}
}
throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class NodeTest method findCompilationUnit.
@Test
public void findCompilationUnit() {
CompilationUnit cu = parse("class X{int x;}");
VariableDeclarator x = cu.getClassByName("X").get().getMember(0).asFieldDeclaration().getVariables().get(0);
assertEquals(cu, x.findCompilationUnit().get());
}
Aggregations