int x=2;
+ * @return VariableDeclarationExpr representing the type
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static VariableDeclarationExpr parseVariableDeclarationExpr(String declaration) {
+ return simplifiedParse(VARIABLE_DECLARATION_EXPR, provider(declaration));
+ }
+
+ /**
+ * Parses the content of a JavadocComment and returns a {@link com.github.javaparser.javadoc.Javadoc} that
+ * represents it.
+ *
+ * @param content a variable declaration like content of my javadoc\n * second line\n * third line
+ * @return Javadoc representing the content of the comment
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static Javadoc parseJavadoc(String content) {
+ return JavadocParser.parse(content);
+ }
+
+ /**
+ * Parses the this(...) and super(...) statements that may occur at the start of a constructor.
+ *
+ * @param statement a statement like super("hello");
+ * @return the AST for the statement.
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static ExplicitConstructorInvocationStmt parseExplicitConstructorInvocationStmt(String statement) {
+ return simplifiedParse(EXPLICIT_CONSTRUCTOR_INVOCATION_STMT, provider(statement));
+ }
+
+ /**
+ * Parses a qualified name (one that can have "."s in it) and returns it as a Name.
+ *
+ * @param qualifiedName a name like "com.laamella.parameter_source"
+ * @return the AST for the name
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static Name parseName(String qualifiedName) {
+ return simplifiedParse(NAME, provider(qualifiedName));
+ }
+
+ /**
+ * Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName.
+ *
+ * @param name a name like "parameter_source"
+ * @return the AST for the name
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static SimpleName parseSimpleName(String name) {
+ return simplifiedParse(SIMPLE_NAME, provider(name));
+ }
+
+ /**
+ * Parses a single parameter (a type and a name) and returns it as a Parameter.
+ *
+ * @param parameter a parameter like "int[] x"
+ * @return the AST for the parameter
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static Parameter parseParameter(String parameter) {
+ return simplifiedParse(PARAMETER, provider(parameter));
+ }
+
+
+ /**
+ * Parses a package declaration and returns it as a PackageDeclaration.
+ *
+ * @param packageDeclaration a declaration like "package com.microsoft.java;"
+ * @return the AST for the parameter
+ * @throws ParseProblemException if the source code has parser errors
+ */
+ public static PackageDeclaration parsePackageDeclaration(String packageDeclaration) {
+ return simplifiedParse(PACKAGE_DECLARATION, provider(packageDeclaration));
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/JavaToken.java b/javaparser-core/src/main/java/com/github/javaparser/JavaToken.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad56ff5d71d5ac1a12234dbe808afdcfd737b56b
--- /dev/null
+++ b/javaparser-core/src/main/java/com/github/javaparser/JavaToken.java
@@ -0,0 +1,796 @@
+/*
+ * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
+ * Copyright (C) 2011, 2013-2016 The JavaParser Team.
+ *
+ * This file is part of JavaParser.
+ *
+ * JavaParser can be used either under the terms of
+ * a) the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * b) the terms of the Apache License
+ *
+ * You should have received a copy of both licenses in LICENCE.LGPL and
+ * LICENCE.APACHE. Please refer to those files for details.
+ *
+ * JavaParser is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ */
+package com.github.javaparser;
+
+import java.util.List;
+import java.util.Optional;
+import static com.github.javaparser.utils.CodeGenerationUtils.f;
+import static com.github.javaparser.utils.Utils.EOL;
+import static com.github.javaparser.utils.Utils.assertNotNull;
+import javax.annotation.Generated;
+
+/**
+ * A token from a parsed source file.
+ * (Awkwardly named "Java"Token since JavaCC already generates an internal class Token.)
+ * It is a node in a double linked list called token list.
+ */
+public class JavaToken {
+
+ public static final JavaToken INVALID = new JavaToken();
+
+ private Range range;
+
+ private int kind;
+
+ private String text;
+
+ private JavaToken previousToken = null;
+
+ private JavaToken nextToken = null;
+
+ private JavaToken() {
+ this(null, 0, "INVALID", null, null);
+ }
+
+ public JavaToken(int kind, String text) {
+ this(null, kind, text, null, null);
+ }
+
+ JavaToken(Token token, Listi'th encountered parsing problem. May throw IndexOutOfBoundsException.
+ */
+ public Problem getProblem(int i) {
+ return getProblems().get(i);
+ }
+
+ /**
+ * @return the complete list of tokens that were parsed, or empty if parsing failed completely.
+ * @deprecated lists of tokens are now kept in every node.
+ * Calling this method is comparable to calling getResult().get().getTokenRange().get()
+ */
+ @Deprecated
+ public Optionalnew int[1][2]; there are two ArrayCreationLevel objects,
+ * the first one contains the expression "1",
+ * the second the expression "2".
+ */
+public final class ArrayCreationLevel extends Node implements NodeWithAnnotations+ * This class represents the entire compilation unit. Each java file denotes a + * compilation unit. + *
+ * A compilation unit start with an optional package declaration, + * followed by zero or more import declarations, + * followed by zero or more type declarations. + * + * @author Julio Vilmar Gesser + * @see PackageDeclaration + * @see ImportDeclaration + * @see TypeDeclaration + * @see Storage + */ +public final class CompilationUnit extends Node { + + @OptionalProperty + private PackageDeclaration packageDeclaration; + + private NodeListnull if there is no import.
+ *
+ * @return the list of imports or none if there is no import
+ */
+ @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
+ public NodeListOptional.none() is returned.
+ *
+ * @return the package declaration or none
+ */
+ @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
+ public Optionalnone is returned.
+ *
+ * @return the list of types or none null if there is no type
+ * @see AnnotationDeclaration
+ * @see ClassOrInterfaceDeclaration
+ * @see EnumDeclaration
+ */
+ @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
+ public NodeListgetTypes().i is out of bounds, throws IndexOutOfBoundsException.
+ *
+ * @param i the index of the type declaration to retrieve
+ */
+ public TypeDeclaration> getType(int i) {
+ return getTypes().get(i);
+ }
+
+ /**
+ * Sets the list of imports of this compilation unit. The list is initially
+ * null.
+ *
+ * @param imports the list of imports
+ */
+ @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
+ public CompilationUnit setImports(final NodeListnull to default package
+ */
+ @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
+ public CompilationUnit setPackageDeclaration(final PackageDeclaration packageDeclaration) {
+ if (packageDeclaration == this.packageDeclaration) {
+ return (CompilationUnit) this;
+ }
+ notifyPropertyChange(ObservableProperty.PACKAGE_DECLARATION, this.packageDeclaration, packageDeclaration);
+ if (this.packageDeclaration != null)
+ this.packageDeclaration.setParentNode(null);
+ this.packageDeclaration = packageDeclaration;
+ setAsParentNodeOf(packageDeclaration);
+ return this;
+ }
+
+ /**
+ * Sets the list of types declared in this compilation unit.
+ */
+ @Generated("com.github.javaparser.generator.core.node.PropertyGenerator")
+ public CompilationUnit setTypes(final NodeList+ *
+ *
+ * public static final DataKey<Role> ROLE = new DataKey<Role>() { };
+ *
+ *
+ *
+ * This code was taken from the Wicket project.
+ *
+ * @param The name does not include the asterisk or the static keyword. The tree is built by instantiating the required nodes, then adding them to other nodes.
+ * If it is the parser who is building the tree, it will use the largest constructor,
+ * the one with "range" as the first parameter.
+ * If you want to manually instantiate nodes, we suggest to...
+ * The parent node field is managed automatically and can be seen as read only.
+ * Note that there is only one parent,
+ * and trying to use the same node in two places will lead to unexpected behaviour.
+ * It is advised to clone() a node before moving it around.
+ * Each Node can have one associated comment which describes it and
+ * a number of "orphan comments" which it contains but are not specifically
+ * associated to any child.
+ * When the parser creates nodes, it sets their source code position in the "range" field.
+ * When you manually instantiate nodes, their range is not set.
+ * The top left character is position 1, 1.
+ * Note that since this is an abstract syntax tree,
+ * it leaves out a lot of text from the original source file,
+ * like where braces or comma's are exactly.
+ * Therefore there is no position information on everything in the original source file.
+ * It is possible to add observers to the the tree.
+ * Any change in the tree is sent as an event to any observers watching.
+ * The most comfortable way of working with an abstract syntax tree is using visitors.
+ * You can use one of the visitors in the visitor package, or extend one of them.
+ * A visitor can be "run" by calling accept on a node:
+ *
+ * For example, comments at the end of methods (immediately before the parenthesis)
+ * or at the end of CompilationUnit are orphan comments.
+ *
+ * When more than one comment preceeds a statement, the one immediately preceding it
+ * it is associated with the statements, while the others are orphans.
+ *
+ * Changes to this list are not persisted.
+ *
+ * @return all comments that cannot be attributed to a concept
+ */
+ public List
+ * Since everything at CompilationUnit level is removable,
+ * this method will only (silently) fail when the node is in a detached AST fragment.
+ */
+ public void removeForced() {
+ if (!remove()) {
+ getParentNode().ifPresent(Node::remove);
+ }
+ }
+
+ @Override
+ public Node getParentNodeForChildren() {
+ return this;
+ }
+
+ protected void setAsParentNodeOf(NodeList extends Node> list) {
+ if (list != null) {
+ list.setParentNode(getParentNodeForChildren());
+ }
+ }
+
+ public void notifyPropertyChange(ObservableProperty property, P oldValue, P newValue) {
+ this.observers.forEach(o -> o.propertyChange(this, property, oldValue, newValue));
+ }
+
+ @Override
+ public void unregister(AstObserver observer) {
+ this.observers.remove(observer);
+ }
+
+ @Override
+ public void register(AstObserver observer) {
+ this.observers.add(observer);
+ }
+
+ /**
+ * Register a new observer for the given node. Depending on the mode specified also descendants, existing
+ * and new, could be observed. For more details see ObserverRegistrationMode.
+ */
+ public void register(AstObserver observer, ObserverRegistrationMode mode) {
+ if (mode == null) {
+ throw new IllegalArgumentException("Mode should be not null");
+ }
+ switch(mode) {
+ case JUST_THIS_NODE:
+ register(observer);
+ break;
+ case THIS_NODE_AND_EXISTING_DESCENDANTS:
+ registerForSubtree(observer);
+ break;
+ case SELF_PROPAGATING:
+ registerForSubtree(PropagatingAstObserver.transformInPropagatingObserver(observer));
+ break;
+ default:
+ throw new UnsupportedOperationException("This mode is not supported: " + mode);
+ }
+ }
+
+ /**
+ * Register the observer for the current node and all the contained node and nodelists, recursively.
+ */
+ public void registerForSubtree(AstObserver observer) {
+ register(observer);
+ this.getChildNodes().forEach(c -> c.registerForSubtree(observer));
+ for (PropertyMetaModel property : getMetaModel().getAllPropertyMetaModels()) {
+ if (property.isNodeList()) {
+ NodeList> nodeList = (NodeList>) property.getValue(this);
+ if (nodeList != null)
+ nodeList.register(observer);
+ }
+ }
+ }
+
+ @Override
+ public boolean isRegistered(AstObserver observer) {
+ return this.observers.contains(observer);
+ }
+
+ @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
+ public boolean remove(Node node) {
+ if (node == null)
+ return false;
+ if (comment != null) {
+ if (node == comment) {
+ removeComment();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
+ public Node removeComment() {
+ return setComment((Comment) null);
+ }
+
+ @Override
+ @Generated("com.github.javaparser.generator.core.node.CloneGenerator")
+ public Node clone() {
+ return (Node) accept(new CloneVisitor(), null);
+ }
+
+ /**
+ * @return get JavaParser specific node introspection information.
+ */
+ @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
+ public NodeMetaModel getMetaModel() {
+ return JavaParserMetaModel.nodeMetaModel;
+ }
+
+ /**
+ * @return whether this node was successfully parsed or not.
+ * If it was not, only the range and tokenRange fields will be valid.
+ */
+ public Parsedness getParsed() {
+ return parsed;
+ }
+
+ /**
+ * Used by the parser to flag unparsable nodes.
+ */
+ public Node setParsed(Parsedness parsed) {
+ this.parsed = parsed;
+ return this;
+ }
+
+ @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
+ public boolean replace(Node node, Node replacementNode) {
+ if (node == null)
+ return false;
+ if (comment != null) {
+ if (node == comment) {
+ setComment((Comment) replacementNode);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Finds the root node of this AST by finding the topmost parent.
+ */
+ public Node findRootNode() {
+ Node n = this;
+ while (n.getParentNode().isPresent()) {
+ n = n.getParentNode().get();
+ }
+ return n;
+ }
+
+ /**
+ * @return the containing CompilationUnit, or empty if this node is not inside a compilation unit.
+ */
+ public Optionalimport com.github.javaparser.JavaParser;
+ * import com.github.javaparser.*;
+ * import com.github.javaparser.JavaParser.*;
+ * import static com.github.javaparser.JavaParser.*;
+ * import static com.github.javaparser.JavaParser.parse;
+ *
+ * Construction
+ *
+ *
+ * ... and use the various methods on the node to initialize it further, if needed.
+ * Parent/child
+ * Comments
+ * Positions
+ * Observers
+ * Visitors
+ * node.accept(visitor, argument);
+ * where argument is an object of your choice (often simply null.)
+ *
+ * @author Julio Vilmar Gesser
+ */
+public abstract class Node implements Cloneable, HasParentNode
This is the most general walk method. All other walk and findAll methods are based on this.
+ */
+ public void walk(TreeTraversal traversal, Consumer
This is the
+ * most general findFirst method. All other findFirst methods are based on this.
+ */
+ public > nodesStack = new Stack<>();
+
+ private final Stack