use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class LexicalDifferenceCalculator method calculatedSyntaxModelAfterListAddition.
// Visible for testing
CalculatedSyntaxModel calculatedSyntaxModelAfterListAddition(Node container, ObservableProperty observableProperty, int index, Node nodeAdded) {
CsmElement csm = ConcreteSyntaxModel.forClass(container.getClass());
Object rawValue = observableProperty.getRawValue(container);
if (!(rawValue instanceof NodeList)) {
throw new IllegalStateException("Expected NodeList, found " + rawValue.getClass().getCanonicalName());
}
NodeList nodeList = (NodeList) rawValue;
return calculatedSyntaxModelAfterListAddition(csm, observableProperty, nodeList, index, nodeAdded);
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class YamlPrinter method output.
public void output(Node node, String name, int level, StringBuilder builder) {
assertNotNull(node);
NodeMetaModel metaModel = node.getMetaModel();
List<PropertyMetaModel> allPropertyMetaModels = metaModel.getAllPropertyMetaModels();
List<PropertyMetaModel> attributes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isAttribute).filter(PropertyMetaModel::isSingular).collect(toList());
List<PropertyMetaModel> subNodes = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNode).filter(PropertyMetaModel::isSingular).collect(toList());
List<PropertyMetaModel> subLists = allPropertyMetaModels.stream().filter(PropertyMetaModel::isNodeList).collect(toList());
if (outputNodeType)
builder.append(System.lineSeparator() + indent(level) + name + "(Type=" + metaModel.getTypeName() + "): ");
else
builder.append(System.lineSeparator() + indent(level) + name + ": ");
level++;
for (PropertyMetaModel a : attributes) {
builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \"" + a.getValue(node).toString() + "\"");
}
for (PropertyMetaModel sn : subNodes) {
Node nd = (Node) sn.getValue(node);
if (nd != null)
output(nd, sn.getName(), level, builder);
}
for (PropertyMetaModel sl : subLists) {
NodeList<? extends Node> nl = (NodeList<? extends Node>) sl.getValue(node);
if (nl != null && nl.isNonEmpty()) {
builder.append(System.lineSeparator() + indent(level) + sl.getName() + ": ");
String slName = sl.getName();
slName = slName.endsWith("s") ? slName.substring(0, sl.getName().length() - 1) : slName;
for (Node nd : nl) output(nd, "- " + slName, level + 1, builder);
}
}
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class AnonymousClassDeclarationContext method solveType.
@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
List<com.github.javaparser.ast.body.TypeDeclaration> typeDeclarations = myDeclaration.findMembersOfKind(com.github.javaparser.ast.body.TypeDeclaration.class);
Optional<SymbolReference<ResolvedTypeDeclaration>> exactMatch = typeDeclarations.stream().filter(internalType -> internalType.getName().getId().equals(name)).findFirst().map(internalType -> SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType)));
if (exactMatch.isPresent()) {
return exactMatch.get();
}
Optional<SymbolReference<ResolvedTypeDeclaration>> recursiveMatch = typeDeclarations.stream().filter(internalType -> name.startsWith(String.format("%s.", internalType.getName()))).findFirst().map(internalType -> JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(internalType.getName().getId().length() + 1), typeSolver));
if (recursiveMatch.isPresent()) {
return recursiveMatch.get();
}
Optional<SymbolReference<ResolvedTypeDeclaration>> typeArgumentsMatch = wrappedNode.getTypeArguments().map(nodes -> ((NodeWithTypeArguments<?>) nodes).getTypeArguments().orElse(new NodeList<>())).orElse(new NodeList<>()).stream().filter(type -> type.toString().equals(name)).findFirst().map(matchingType -> SymbolReference.solved(new JavaParserTypeParameter(new TypeParameter(matchingType.toString()), typeSolver)));
if (typeArgumentsMatch.isPresent()) {
return typeArgumentsMatch.get();
}
// Look into extended classes and implemented interfaces
for (ResolvedReferenceType ancestor : myDeclaration.getAncestors()) {
// look at names of extended classes and implemented interfaces (this may not be important because they are checked in CompilationUnitContext)
if (ancestor.getTypeDeclaration().getName().equals(name)) {
return SymbolReference.solved(ancestor.getTypeDeclaration());
}
// look into internal types of extended classes and implemented interfaces
try {
for (ResolvedTypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
if (internalTypeDeclaration.getName().equals(name)) {
return SymbolReference.solved(internalTypeDeclaration);
}
}
} catch (UnsupportedOperationException e) {
// just continue using the next ancestor
}
}
return getParent().solveType(name, typeSolver);
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class GeneratedJavaParserBase method generateLambda.
/**
* Workaround for rather complex ambiguity that lambda's create
*/
Expression generateLambda(Expression ret, Statement lambdaBody) {
if (ret instanceof EnclosedExpr) {
Expression inner = ((EnclosedExpr) ret).getInner();
SimpleName id = ((NameExpr) inner).getName();
NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), EnumSet.noneOf(Modifier.class), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, true);
} else if (ret instanceof NameExpr) {
SimpleName id = ((NameExpr) ret).getName();
NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), EnumSet.noneOf(Modifier.class), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, false);
} else if (ret instanceof LambdaExpr) {
((LambdaExpr) ret).setBody(lambdaBody);
propagateRangeGrowthOnRight(ret, lambdaBody);
} else if (ret instanceof CastExpr) {
CastExpr castExpr = (CastExpr) ret;
Expression inner = generateLambda(castExpr.getExpression(), lambdaBody);
castExpr.setExpression(inner);
} else {
addProblem("Failed to parse lambda expression! Please create an issue at https://github.com/javaparser/javaparser/issues");
}
return ret;
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class CloneVisitorTest method cloneJavaDocTest.
@Test
public void cloneJavaDocTest() {
NodeList<BodyDeclaration<?>> bodyDeclarationList = new NodeList<>();
bodyDeclarationList.add(new AnnotationMemberDeclaration().setJavadocComment("javadoc"));
bodyDeclarationList.add(new ConstructorDeclaration().setJavadocComment("javadoc"));
bodyDeclarationList.add(new EnumConstantDeclaration().setJavadocComment("javadoc"));
bodyDeclarationList.add(new FieldDeclaration().setJavadocComment("javadoc"));
bodyDeclarationList.add(new InitializerDeclaration().setJavadocComment("javadoc"));
bodyDeclarationList.add(new MethodDeclaration().setJavadocComment("javadoc"));
NodeList<TypeDeclaration<?>> typeDeclarationList = new NodeList<>();
AnnotationDeclaration annotationDeclaration = new AnnotationDeclaration();
annotationDeclaration.setName("nnotationDeclarationTest");
typeDeclarationList.add(annotationDeclaration.setJavadocComment("javadoc"));
ClassOrInterfaceDeclaration classOrInterfaceDeclaration2 = new ClassOrInterfaceDeclaration();
classOrInterfaceDeclaration2.setName("emptyTypeDeclarationTest");
typeDeclarationList.add(classOrInterfaceDeclaration2.setJavadocComment("javadoc"));
EnumDeclaration enumDeclaration = new EnumDeclaration();
enumDeclaration.setName("enumDeclarationTest");
typeDeclarationList.add(enumDeclaration.setJavadocComment("javadoc"));
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration();
classOrInterfaceDeclaration.setName("classOrInterfaceDeclarationTest");
typeDeclarationList.add(classOrInterfaceDeclaration.setJavadocComment("javadoc"));
ClassOrInterfaceDeclaration classOrInterfaceDeclaration1 = new ClassOrInterfaceDeclaration();
classOrInterfaceDeclaration1.setName("emptyTypeDeclarationTest1");
typeDeclarationList.add(classOrInterfaceDeclaration2.setMembers(bodyDeclarationList));
cu.setTypes(typeDeclarationList);
CompilationUnit cuClone = (CompilationUnit) new CloneVisitor().visit(cu, null);
NodeList<TypeDeclaration<?>> typeDeclarationListClone = cuClone.getTypes();
Iterator<TypeDeclaration<?>> typeItr = typeDeclarationListClone.iterator();
TypeDeclaration<?> typeDeclaration;
while (typeItr.hasNext()) {
typeDeclaration = typeItr.next();
if (typeDeclaration.getMembers() == null) {
assertEquals(typeDeclaration.getComment().get().getContent(), " javadoc");
} else {
Iterator<BodyDeclaration<?>> bodyItr = typeDeclaration.getMembers().iterator();
while (bodyItr.hasNext()) {
BodyDeclaration<?> bodyDeclaration = bodyItr.next();
assertEquals(bodyDeclaration.getComment().get().getContent(), " javadoc");
}
}
}
}
Aggregations