本文整理汇总了Java中com.github.javaparser.ast.Node类的典型用法代码示例。如果您正苦于以下问题:Java Node类的具体用法?Java Node怎么用?Java Node使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Node类属于com.github.javaparser.ast包,在下文中一共展示了Node类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: printOrphanCommentsBeforeThisChildNode
import com.github.javaparser.ast.Node; //导入依赖的package包/类
private void printOrphanCommentsBeforeThisChildNode(final Node node){
if (node instanceof Comment) return;
Node parent = node.getParentNode();
if (parent==null) return;
List<Node> everything = new LinkedList<Node>();
everything.addAll(parent.getChildrenNodes());
sortByBeginPosition(everything);
int positionOfTheChild = -1;
for (int i=0;i<everything.size();i++){
if (everything.get(i)==node) positionOfTheChild=i;
}
if (positionOfTheChild==-1) throw new RuntimeException("My index not found!!! "+node);
int positionOfPreviousChild = -1;
for (int i=positionOfTheChild-1;i>=0 && positionOfPreviousChild==-1;i--){
if (!(everything.get(i) instanceof Comment)) positionOfPreviousChild = i;
}
for (int i=positionOfPreviousChild+1;i<positionOfTheChild;i++){
Node nodeToPrint = everything.get(i);
if (!(nodeToPrint instanceof Comment)) throw new RuntimeException("Expected comment, instead "+nodeToPrint.getClass()+". Position of previous child: "+positionOfPreviousChild+", position of child "+positionOfTheChild);
nodeToPrint.accept(this,null);
}
}
示例2: brewJava
import com.github.javaparser.ast.Node; //导入依赖的package包/类
public static void brewJava(File rFile, File outputDir, String packageName, String className)
throws Exception {
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
TypeSpec.Builder result =
TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
for (Node node : resourceClass.getChildrenNodes()) {
if (node instanceof TypeDeclaration) {
addResourceType(Arrays.asList(SUPPORTED_TYPES), result, (TypeDeclaration) node);
}
}
JavaFile finalR = JavaFile.builder(packageName, result.build())
.addFileComment("Generated code from Butter Knife gradle plugin. Do not modify!")
.build();
finalR.writeTo(outputDir);
}
示例3: printOrphanCommentsEnding
import com.github.javaparser.ast.Node; //导入依赖的package包/类
private void printOrphanCommentsEnding(final Node node) {
if (configuration.isIgnoreComments()) return;
List<Node> everything = new LinkedList<>();
everything.addAll(node.getChildNodes());
sortByBeginPosition(everything);
if (everything.isEmpty()) {
return;
}
int commentsAtEnd = 0;
boolean findingComments = true;
while (findingComments && commentsAtEnd < everything.size()) {
Node last = everything.get(everything.size() - 1 - commentsAtEnd);
findingComments = (last instanceof Comment);
if (findingComments) {
commentsAtEnd++;
}
}
for (int i = 0; i < commentsAtEnd; i++) {
everything.get(everything.size() - commentsAtEnd + i).accept(this, null);
}
}
示例4: visit
import com.github.javaparser.ast.Node; //导入依赖的package包/类
@Override
public void visit(final NormalAnnotationExpr expr, final Void arg) {
final String fqcn = expr.getName().toString();
if (fqcn.equals(Description.class.getCanonicalName())) {
final Node parent = expr.getParentNode();
final String value = expr.getPairs().get(0).toString();
if (parent instanceof ClassOrInterfaceDeclaration) {
descriptionAnotValue = value;
} else if (parent instanceof MethodDeclaration) {
methodDescriptions.put(((MethodDeclaration) parent).getName(), value);
}
} else if (fqcn.equals(ServiceInterfaceAnnotation.class.getCanonicalName())) {
String text1 = expr.getPairs().get(0).toString();
String text2 = expr.getPairs().get(1).toString();
if (text1.contains("value")) {
sieAnnotValue = text1;
sieAnnotOsgiRegistrationType = text2;
} else {
sieAnnotValue = text2;
sieAnnotOsgiRegistrationType = text1;
}
}
super.visit(expr, arg);
}
示例5: createComponent
import com.github.javaparser.ast.Node; //导入依赖的package包/类
/**
* Creates a new component based on the given ParseRuleContext.
*/
private Component createComponent(Node node, ComponentType componentType) {
final Component newCmp = new Component();
newCmp.setPackageName(currentPkg);
newCmp.setComponentType(componentType);
if (componentType.isVariableComponent()) {
newCmp.setLine(node.getRange().get().end.line);
} else {
newCmp.setLine(node.getBegin().get().line);
}
if (node.getComment().isPresent()) {
newCmp.setComment(node.getComment().toString());
}
newCmp.setSourceFilePath(file.name());
return newCmp;
}
示例6: visit
import com.github.javaparser.ast.Node; //导入依赖的package包/类
@Override
public FieldDeclaration visit(FieldDeclaration fd, Void arg) {
super.visit(fd, arg);
boolean []addAnno = new boolean[]{false};
fd.getAnnotations().forEach( annotation -> {
try {
List<Node> annos = annotation.getChildNodes();
if(annos.size() == 2 && "JsonProperty".equalsIgnoreCase(annos.get(0).toString())){
if(this.fields.contains(annos.get(1).toString().replaceAll("\"", ""))){
//we found the field in the pojo that matches the field we need to inject into
//add fully qualified annotation ex. javax.validation.constraints.Null
addAnno[0] = true;
}
}
} catch (java.util.NoSuchElementException e) {
System.out.println("");
}
});
if(addAnno[0]){
fd.addAnnotation(annotationName);
}
return fd;
}
示例7: visit
import com.github.javaparser.ast.Node; //导入依赖的package包/类
@Override public Node visit(final AnnotationMemberDeclaration n, final A arg) {
if (n.getJavaDoc() != null) {
n.setJavaDoc((JavadocComment) n.getJavaDoc().accept(this, arg));
}
final List<AnnotationExpr> annotations = n.getAnnotations();
if (annotations != null) {
for (int i = 0; i < annotations.size(); i++) {
annotations.set(i, (AnnotationExpr) annotations.get(i).accept(this, arg));
}
removeNulls(annotations);
}
n.setType((Type) n.getType().accept(this, arg));
if (n.getDefaultValue() != null) {
n.setDefaultValue((Expression) n.getDefaultValue().accept(this, arg));
}
return n;
}
示例8: isIntrospectionRelevantNode
import com.github.javaparser.ast.Node; //导入依赖的package包/类
private boolean isIntrospectionRelevantNode(Node node) {
if (node instanceof Type && node.getParentNode() instanceof MethodDeclaration) {
return false;
}
if (node instanceof Parameter && node.getParentNode() instanceof MethodDeclaration) {
return false;
}
if (!(node instanceof AnnotationExpr) && node.getParentNode() instanceof BaseParameter) {
return false;
}
if (node instanceof NameExpr && node.getParentNode() instanceof AnnotationExpr) {
return false;
}
if (node.getParentNode() instanceof FieldAccessExpr) {
return false;
}
return true;
}
示例9: visit
import com.github.javaparser.ast.Node; //导入依赖的package包/类
protected Boolean visit(final BaseParameter n1, final Node arg) {
final BaseParameter n2 = (BaseParameter) arg;
if (n1.getModifiers() != n2.getModifiers()) {
return Boolean.FALSE;
}
if (!nodeEquals(n1.getId(), n2.getId())) {
return Boolean.FALSE;
}
if (!nodesEquals(n1.getAnnotations(), n2.getAnnotations())) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}
示例10: insertCommentsInCu
import com.github.javaparser.ast.Node; //导入依赖的package包/类
/**
* Comments are attributed to the thing the comment and are removed from
* allComments.
*/
private static void insertCommentsInCu(CompilationUnit cu, CommentsCollection commentsCollection){
if (commentsCollection.size()==0) return;
// I should sort all the direct children and the comments, if a comment is the first thing then it
// a comment to the CompilationUnit
// FIXME if there is no package it could be also a comment to the following class...
// so I could use some heuristics in these cases to distinguish the two cases
List<Comment> comments = commentsCollection.getAll();
PositionUtils.sortByBeginPosition(comments);
List<Node> children = cu.getChildrenNodes();
PositionUtils.sortByBeginPosition(children);
if (cu.getPackage()!=null && (children.size()==0 || PositionUtils.areInOrder(comments.get(0), children.get(0)))){
cu.setComment(comments.get(0));
comments.remove(0);
}
insertCommentsInNode(cu,comments);
}
示例11: getSymbolDeclarator
import com.github.javaparser.ast.Node; //导入依赖的package包/类
public static SymbolDeclarator getSymbolDeclarator(Node node, TypeSolver typeSolver) {
if (node instanceof FieldDeclaration) {
return new FieldSymbolDeclarator((FieldDeclaration) node, typeSolver);
} else if (node instanceof Parameter) {
return new ParameterSymbolDeclarator((Parameter) node, typeSolver);
} else if (node instanceof ExpressionStmt) {
ExpressionStmt expressionStmt = (ExpressionStmt) node;
if (expressionStmt.getExpression() instanceof VariableDeclarationExpr) {
return new VariableSymbolDeclarator((VariableDeclarationExpr) (expressionStmt.getExpression()), typeSolver);
} else {
return new NoSymbolDeclarator<ExpressionStmt>(expressionStmt, typeSolver);
}
} else if (node instanceof IfStmt) {
return new NoSymbolDeclarator<IfStmt>((IfStmt) node, typeSolver);
} else if (node instanceof ForeachStmt) {
ForeachStmt foreachStmt = (ForeachStmt) node;
return new VariableSymbolDeclarator((VariableDeclarationExpr) (foreachStmt.getVariable()), typeSolver);
} else {
return new NoSymbolDeclarator<Node>(node, typeSolver);
}
}
示例12: printOrphanCommentsEnding
import com.github.javaparser.ast.Node; //导入依赖的package包/类
private void printOrphanCommentsEnding(final Node node){
List<Node> everything = new LinkedList<Node>();
everything.addAll(node.getChildrenNodes());
sortByBeginPosition(everything);
if (everything.size()==0) return;
int commentsAtEnd = 0;
boolean findingComments = true;
while (findingComments&&commentsAtEnd<everything.size()){
Node last = everything.get(everything.size()-1-commentsAtEnd);
findingComments = (last instanceof Comment);
if (findingComments) commentsAtEnd++;
}
for (int i=0;i<commentsAtEnd;i++){
everything.get(everything.size()-commentsAtEnd+i).accept(this,null);
}
}
示例13: visit
import com.github.javaparser.ast.Node; //导入依赖的package包/类
@Override
public Node visit(MethodDeclaration _n, Object _arg) {
JavadocComment javaDoc = cloneNodes(_n.getJavaDoc(), _arg);
List<AnnotationExpr> annotations = visit(_n.getAnnotations(), _arg);
List<TypeParameter> typeParameters = visit(_n.getTypeParameters(), _arg);
Type type_ = cloneNodes(_n.getType(), _arg);
List<Parameter> parameters = visit(_n.getParameters(), _arg);
List<ReferenceType> throws_ = visit(_n.getThrows(), _arg);
BlockStmt block = cloneNodes(_n.getBody(), _arg);
Comment comment = cloneNodes(_n.getComment(), _arg);
MethodDeclaration r = new MethodDeclaration(
_n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(),
_n.getModifiers(), annotations, typeParameters, type_, _n.getName(), parameters, _n.getArrayCount(), throws_, block
);
r.setComment(comment);
return r;
}
示例14: nodeContains
import com.github.javaparser.ast.Node; //导入依赖的package包/类
public static boolean nodeContains(Node container, Node contained, boolean ignoringAnnotations){
if (!ignoringAnnotations || PositionUtils.getLastAnnotation(container)==null){
return container.contains(contained);
}
if (!container.contains(contained)){
return false;
}
// if the node is contained, but it comes immediately after the annotations,
// let's not consider it contained
if (container instanceof AnnotableNode){
int bl = beginLineWithoutConsideringAnnotation(container);
int bc = beginColumnWithoutConsideringAnnotation(container);
if (bl>contained.getBeginLine()) return false;
if (bl==contained.getBeginLine() && bc>contained.getBeginColumn()) return false;
if (container.getEndLine()<contained.getEndLine()) return false;
if (container.getEndLine()==contained.getEndLine() && container.getEndColumn()<contained.getEndColumn()) return false;
return true;
}
return true;
}
示例15: visit
import com.github.javaparser.ast.Node; //导入依赖的package包/类
@Override public Boolean visit(final IfStmt n1, final Node arg) {
final IfStmt n2 = (IfStmt) arg;
if (!nodeEquals(n1.getCondition(), n2.getCondition())) {
return Boolean.FALSE;
}
if (!nodeEquals(n1.getThenStmt(), n2.getThenStmt())) {
return Boolean.FALSE;
}
if (!nodeEquals(n1.getElseStmt(), n2.getElseStmt())) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}