diff options
author | Maximilien CRUZ <max@rockpalm.it> | 2016-07-14 17:30:34 +0200 |
---|---|---|
committer | Maximilien CRUZ <max@rockpalm.it> | 2016-07-17 22:04:31 +0200 |
commit | 390fbb37cc7f41787cf2559842beb98e7058b4ae (patch) | |
tree | 80c81fa37bbb44dabaff06375ddd9123c75eb97e /javaparser-core/src/main/java/com | |
parent | d1b21c6bee0519451ba7c39b6f3ade8aa9be109c (diff) | |
download | platform_external_javaparser-390fbb37cc7f41787cf2559842beb98e7058b4ae.tar.gz platform_external_javaparser-390fbb37cc7f41787cf2559842beb98e7058b4ae.tar.bz2 platform_external_javaparser-390fbb37cc7f41787cf2559842beb98e7058b4ae.zip |
builder methods for #363
Diffstat (limited to 'javaparser-core/src/main/java/com')
20 files changed, 1466 insertions, 422 deletions
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ASTHelper.java b/javaparser-core/src/main/java/com/github/javaparser/ASTHelper.java index 88993b171..75700328a 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ASTHelper.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ASTHelper.java @@ -21,6 +21,11 @@ package com.github.javaparser; +import static com.github.javaparser.ast.internal.Utils.isNullOrEmpty; + +import java.util.ArrayList; +import java.util.List; + import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.body.BodyDeclaration; @@ -40,15 +45,10 @@ import com.github.javaparser.ast.stmt.ExpressionStmt; import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.type.PrimitiveType; +import com.github.javaparser.ast.type.PrimitiveType.Primitive; import com.github.javaparser.ast.type.ReferenceType; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.type.VoidType; -import com.github.javaparser.ast.type.PrimitiveType.Primitive; - -import java.util.ArrayList; -import java.util.List; - -import static com.github.javaparser.ast.internal.Utils.*; /** * This class helps to construct new nodes. @@ -202,7 +202,9 @@ public final class ASTHelper { * compilation unit * @param type * type declaration + * @deprecated use {@link CompilationUnit#addClass(String)} kind of methods */ + @Deprecated public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) { List<TypeDeclaration> types = cu.getTypes(); if (isNullOrEmpty(types)) { diff --git a/javaparser-core/src/main/java/com/github/javaparser/ClassUtils.java b/javaparser-core/src/main/java/com/github/javaparser/ClassUtils.java new file mode 100644 index 000000000..fc7807b94 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ClassUtils.java @@ -0,0 +1,69 @@ +package com.github.javaparser;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ClassUtils {
+ /**
+ * Maps primitive {@code Class}es to their corresponding wrapper {@code Class}.
+ */
+ private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<Class<?>, Class<?>>();
+ static {
+ primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
+ primitiveWrapperMap.put(Byte.TYPE, Byte.class);
+ primitiveWrapperMap.put(Character.TYPE, Character.class);
+ primitiveWrapperMap.put(Short.TYPE, Short.class);
+ primitiveWrapperMap.put(Integer.TYPE, Integer.class);
+ primitiveWrapperMap.put(Long.TYPE, Long.class);
+ primitiveWrapperMap.put(Double.TYPE, Double.class);
+ primitiveWrapperMap.put(Float.TYPE, Float.class);
+ primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
+ }
+
+ /**
+ * Maps wrapper {@code Class}es to their corresponding primitive types.
+ */
+ private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<Class<?>, Class<?>>();
+ static {
+ for (final Class<?> primitiveClass : primitiveWrapperMap.keySet()) {
+ final Class<?> wrapperClass = primitiveWrapperMap.get(primitiveClass);
+ if (!primitiveClass.equals(wrapperClass)) {
+ wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
+ }
+ }
+ }
+
+ /**
+ * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte},
+ * {@link Character},
+ * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ *
+ * @param type
+ * The class to query or null.
+ * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte},
+ * {@link Character},
+ * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ */
+ public static boolean isPrimitiveOrWrapper(final Class<?> type) {
+ if (type == null) {
+ return false;
+ }
+ return type.isPrimitive() || isPrimitiveWrapper(type);
+ }
+
+ /**
+ * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
+ * {@link Short},
+ * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ *
+ * @param type
+ * The class to query or null.
+ * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
+ * {@link Short},
+ * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ * @since 3.1
+ */
+ public static boolean isPrimitiveWrapper(final Class<?> type) {
+ return wrapperPrimitiveMap.containsKey(type);
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java b/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java index 5ceee3841..6befa5959 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/CompilationUnit.java @@ -18,28 +18,34 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ - + package com.github.javaparser.ast; -import com.github.javaparser.Position; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.util.List; + +import com.github.javaparser.ASTHelper; import com.github.javaparser.Range; -import com.github.javaparser.ast.body.*; +import com.github.javaparser.ast.body.AnnotationDeclaration; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.EmptyTypeDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.ast.body.ModifierSet; +import com.github.javaparser.ast.body.TypeDeclaration; import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.ensureNotNull; - /** * <p> * This class represents the entire compilation unit. Each java file denotes a * compilation unit. * </p> * The CompilationUnit is constructed following the syntax:<br> + * * <pre> * {@code * CompilationUnit ::= ( }{@link PackageDeclaration}{@code )? @@ -47,6 +53,7 @@ import static com.github.javaparser.ast.internal.Utils.ensureNotNull; * ( }{@link TypeDeclaration}{@code )* * } * </pre> + * * @author Julio Vilmar Gesser */ public final class CompilationUnit extends Node { @@ -70,11 +77,13 @@ public final class CompilationUnit extends Node { * @deprecated prefer using Range objects. */ @Deprecated - public CompilationUnit(int beginLine, int beginColumn, int endLine, int endColumn, PackageDeclaration pakage, List<ImportDeclaration> imports, List<TypeDeclaration> types) { + public CompilationUnit(int beginLine, int beginColumn, int endLine, int endColumn, PackageDeclaration pakage, + List<ImportDeclaration> imports, List<TypeDeclaration> types) { this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), pakage, imports, types); } - - public CompilationUnit(Range range, PackageDeclaration pakage, List<ImportDeclaration> imports, List<TypeDeclaration> types) { + + public CompilationUnit(Range range, PackageDeclaration pakage, List<ImportDeclaration> imports, + List<TypeDeclaration> types) { super(range); setPackage(pakage); setImports(imports); @@ -163,7 +172,7 @@ public final class CompilationUnit extends Node { */ public void setImports(List<ImportDeclaration> imports) { this.imports = imports; - setAsParentNodeOf(this.imports); + setAsParentNodeOf(this.imports); } /** @@ -175,7 +184,7 @@ public final class CompilationUnit extends Node { */ public void setPackage(PackageDeclaration pakage) { this.pakage = pakage; - setAsParentNodeOf(this.pakage); + setAsParentNodeOf(this.pakage); } /** @@ -186,6 +195,180 @@ public final class CompilationUnit extends Node { */ public void setTypes(List<TypeDeclaration> types) { this.types = types; - setAsParentNodeOf(this.types); + setAsParentNodeOf(this.types); + } + + /** + * sets the package declaration of this compilation unit + * + * @param name the name of the package + * @return this, the {@link CompilationUnit} + */ + public CompilationUnit setPackageName(String name) { + setPackage(new PackageDeclaration(ASTHelper.createNameExpr(name))); + return this; + } + + /** + * Add an import to the list of {@link ImportDeclaration} of this compilation unit<br> + * shorthand for {@link #addImport(String, boolean, boolean)} with name,false,false + * + * @param name the import name + * @return this, the {@link CompilationUnit} + */ + public CompilationUnit addImport(String name) { + return addImport(name, false, false); + } + + /** + * Add an import to the list of {@link ImportDeclaration} of this compilation unit<br> + * shorthand for {@link #addImport(String)} with clazz.getName() + * + * @param clazz the class to import + * @return this, the {@link CompilationUnit} + */ + public CompilationUnit addImport(Class<?> clazz) { + return addImport(clazz.getName()); + } + + /** + * Add an import to the list of {@link ImportDeclaration} of this compilation unit<br> + * shorthand for {@link #addImport(String, boolean, boolean)} with name,false,false<br> + * <b>This method check if no import with the same name is already in the list</b> + * + * @param clazz the class to import + * @return this, the {@link CompilationUnit} + */ + public CompilationUnit addImportWithoutDuplicates(Class<?> clazz) { + return addImportWithoutDuplicates(clazz.getName()); + } + + /** + * Add an import to the list of {@link ImportDeclaration} of this compilation unit<br> + * shorthand for {@link #addImport(String, boolean, boolean)} with name,false,false<br> + * <b>This method check if no import with the same name is already in the list</b> + * + * @param name the import name + * @return this, the {@link CompilationUnit} + */ + public CompilationUnit addImportWithoutDuplicates(final String name) { + if (getImports().stream().anyMatch(i -> i.getName().getName().equals(name))) + return this; + else + return addImport(name, false, false); + } + + /** + * Add an import to the list of {@link ImportDeclaration} of this compilation unit + * + * @param name the import name + * @param isStatic + * @param isAsterisk + * @return this, the {@link CompilationUnit} + */ + public CompilationUnit addImport(String name, boolean isStatic, boolean isAsterisk) { + ImportDeclaration importDeclaration = new ImportDeclaration(ASTHelper.createNameExpr(name), isStatic, + isAsterisk); + getImports().add(importDeclaration); + importDeclaration.setParentNode(this); + return this; + } + + /** + * Add a public class to the types of this compilation unit + * + * @param name the class name + * @return the newly created class + */ + public ClassOrInterfaceDeclaration addClass(String name) { + return addClass(name, ModifierSet.PUBLIC); + } + + /** + * Add a class to the types of this compilation unit + * + * @param name the class name + * @param modifiers the modifiers (like ModifierSet.PUBLIC) + * @return the newly created class + */ + public ClassOrInterfaceDeclaration addClass(String name, int modifiers) { + ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration(modifiers, + false, name); + getTypes().add(classOrInterfaceDeclaration); + classOrInterfaceDeclaration.setParentNode(this); + return classOrInterfaceDeclaration; + } + + /** + * Add a public interface class to the types of this compilation unit + * + * @param name the interface name + * @return the newly created class + */ + public ClassOrInterfaceDeclaration addInterface(String name) { + return addInterface(name, ModifierSet.PUBLIC); + } + + /** + * Add an interface to the types of this compilation unit + * + * @param name the interface name + * @param modifiers the modifiers (like ModifierSet.PUBLIC) + * @return the newly created class + */ + public ClassOrInterfaceDeclaration addInterface(String name, int modifiers) { + ClassOrInterfaceDeclaration classOrInterfaceDeclaration = new ClassOrInterfaceDeclaration(modifiers, + true, name); + getTypes().add(classOrInterfaceDeclaration); + classOrInterfaceDeclaration.setParentNode(this); + return classOrInterfaceDeclaration; + } + + /** + * Add a public enum to the types of this compilation unit + * + * @param name the enum name + * @return the newly created class + */ + public EnumDeclaration addEnum(String name) { + return addEnum(name, ModifierSet.PUBLIC); + } + + /** + * Add an enum to the types of this compilation unit + * + * @param name the enum name + * @param modifiers the modifiers (like ModifierSet.PUBLIC) + * @return the newly created class + */ + public EnumDeclaration addEnum(String name, int modifiers) { + EnumDeclaration enumDeclaration = new EnumDeclaration(modifiers, name); + getTypes().add(enumDeclaration); + enumDeclaration.setParentNode(this); + return enumDeclaration; + } + + /** + * Add a public annotation declaration to the types of this compilation unit + * + * @param name the annotation name + * @return the newly created class + */ + public AnnotationDeclaration addAnnotationDeclaration(String name) { + return addAnnotationDeclaration(name, ModifierSet.PUBLIC); + } + + /** + * Add an annotation declaration to the types of this compilation unit + * + * @param name the annotation name + * @param modifiers the modifiers (like ModifierSet.PUBLIC) + * @return the newly created class + */ + public AnnotationDeclaration addAnnotationDeclaration(String name, int modifiers) { + AnnotationDeclaration annotationDeclaration = new AnnotationDeclaration(modifiers, name); + getTypes().add(annotationDeclaration); + annotationDeclaration.setParentNode(this); + return annotationDeclaration; } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java b/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java index b37a0b511..304bc1385 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/Node.java @@ -21,16 +21,22 @@ package com.github.javaparser.ast; -import com.github.javaparser.Position; -import com.github.javaparser.Range; -import com.github.javaparser.ast.comments.Comment; -import com.github.javaparser.ast.visitor.*; +import static com.github.javaparser.Position.pos; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import static com.github.javaparser.Position.pos; +import com.github.javaparser.Position; +import com.github.javaparser.Range; +import com.github.javaparser.ast.comments.BlockComment; +import com.github.javaparser.ast.comments.Comment; +import com.github.javaparser.ast.comments.JavadocComment; +import com.github.javaparser.ast.comments.LineComment; +import com.github.javaparser.ast.visitor.CloneVisitor; +import com.github.javaparser.ast.visitor.DumpVisitor; +import com.github.javaparser.ast.visitor.EqualsVisitor; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; /** * Abstract class for all nodes of the AST. @@ -203,17 +209,17 @@ public abstract class Node implements Cloneable { * Sets the begin position of this node in the source file. */ public void setBegin(Position begin) { - range=range.withBegin(begin); + range = range.withBegin(begin); } /** * Sets the end position of this node in the source file. */ public void setEnd(Position end) { - range=range.withEnd(end); + range = range.withEnd(end); } - /** + /** * @return the range of characters in the source code that this node covers. */ public Range getRange() { @@ -236,8 +242,7 @@ public abstract class Node implements Cloneable { if (comment != null && (this instanceof Comment)) { throw new RuntimeException("A comment can not be commented"); } - if (this.comment != null) - { + if (this.comment != null) { this.comment.setCommentedNode(null); } this.comment = comment; @@ -249,6 +254,33 @@ public abstract class Node implements Cloneable { /** * Use this to store additional information to this node. * + * @param comment to be set + */ + public final void setJavaDocComment(String comment) { + setComment(new JavadocComment(comment)); + } + + /** + * Use this to store additional information to this node. + * + * @param comment to be set + */ + public final void setLineComment(String comment) { + setComment(new LineComment(comment)); + } + + /** + * Use this to store additional information to this node. + * + * @param comment to be set + */ + public final void setBlockComment(String comment) { + setComment(new BlockComment(comment)); + } + + /** + * Use this to store additional information to this node. + * * @param data to be set */ public final void setData(final Object data) { @@ -319,6 +351,17 @@ public abstract class Node implements Cloneable { return parentNode; } + @SuppressWarnings("unchecked") + public <T> T getParentNodeOfType(Class<T> classType) { + Node parent = parentNode; + while (parent != null) { + if (parent.getClass().equals(classType)) + return (T) parent; + parent = parent.parentNode; + } + return null; + } + public List<Node> getChildrenNodes() { return childrenNodes; } @@ -341,6 +384,7 @@ public abstract class Node implements Cloneable { * * When more than one comment preceeds a statement, the one immediately preceding it * it is associated with the statements, while the others are orphans. + * * @return all comments that cannot be attributed to a concept */ public List<Comment> getOrphanComments() { @@ -351,6 +395,7 @@ public abstract class Node implements Cloneable { * This is the list of Comment which are contained in the Node either because * they are properly associated to one of its children or because they are floating * around inside the Node + * * @return all Comments within the node as a list */ public List<Comment> getAllContainedComments() { @@ -413,6 +458,7 @@ public abstract class Node implements Cloneable { public boolean isPositionedAfter(Position position) { return range.isAfter(position); } + /** * @deprecated prefer using Range objects. */ diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java b/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java index 4228500f5..e9a9bc0c9 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/TypedNode.java @@ -31,8 +31,8 @@ import com.github.javaparser.ast.type.Type; *
* @since 2.3.1
*/
-public interface TypedNode {
+public interface TypedNode<T> {
Type getType();
- void setType(Type type);
+ T setType(Type type);
}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java index ca2c6e8b5..fc52474e9 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/AnnotationMemberDeclaration.java @@ -21,6 +21,10 @@ package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; + +import java.util.List; + import com.github.javaparser.Range; import com.github.javaparser.ast.DocumentableNode; import com.github.javaparser.ast.NamedNode; @@ -33,14 +37,11 @@ import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; - /** * @author Julio Vilmar Gesser */ -public final class AnnotationMemberDeclaration extends BodyDeclaration implements DocumentableNode, NamedNode, TypedNode, NodeWithModifiers { +public final class AnnotationMemberDeclaration extends BodyDeclaration + implements DocumentableNode, NamedNode, TypedNode<AnnotationMemberDeclaration>, NodeWithModifiers { private int modifiers; @@ -133,9 +134,10 @@ public final class AnnotationMemberDeclaration extends BodyDeclaration implement } @Override - public void setType(Type type) { + public AnnotationMemberDeclaration setType(Type type) { this.type = type; setAsParentNodeOf(type); + return this; } @Override diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/ClassOrInterfaceDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/ClassOrInterfaceDeclaration.java index b512fef23..7e788d38c 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/ClassOrInterfaceDeclaration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/ClassOrInterfaceDeclaration.java @@ -18,135 +18,414 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ - + package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.lang.annotation.Annotation; +import java.util.List; + +import com.github.javaparser.ASTHelper; +import com.github.javaparser.ClassUtils; import com.github.javaparser.Range; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Node; import com.github.javaparser.ast.TypeParameter; import com.github.javaparser.ast.expr.AnnotationExpr; +import com.github.javaparser.ast.expr.MarkerAnnotationExpr; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; +import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.ensureNotNull; - /** * @author Julio Vilmar Gesser */ public final class ClassOrInterfaceDeclaration extends TypeDeclaration { - private boolean interface_; - - private List<TypeParameter> typeParameters; - - // Can contain more than one item if this is an interface - private List<ClassOrInterfaceType> extendsList; - - private List<ClassOrInterfaceType> implementsList; - - public ClassOrInterfaceDeclaration() { - } - - public ClassOrInterfaceDeclaration(final int modifiers, final boolean isInterface, final String name) { - super(modifiers, name); - setInterface(isInterface); - } - - public ClassOrInterfaceDeclaration(final int modifiers, - final List<AnnotationExpr> annotations, final boolean isInterface, final String name, - final List<TypeParameter> typeParameters, final List<ClassOrInterfaceType> extendsList, - final List<ClassOrInterfaceType> implementsList, final List<BodyDeclaration> members) { - super(annotations, modifiers, name, members); - setInterface(isInterface); - setTypeParameters(typeParameters); - setExtends(extendsList); - setImplements(implementsList); - } - - /** - * @deprecated prefer using Range objects. - */ - @Deprecated - public ClassOrInterfaceDeclaration(final int beginLine, final int beginColumn, final int endLine, - final int endColumn, final int modifiers, - final List<AnnotationExpr> annotations, final boolean isInterface, final String name, - final List<TypeParameter> typeParameters, final List<ClassOrInterfaceType> extendsList, - final List<ClassOrInterfaceType> implementsList, final List<BodyDeclaration> members) { - this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), modifiers, annotations, isInterface, name, typeParameters, extendsList, implementsList, members); - } - - public ClassOrInterfaceDeclaration(Range range, final int modifiers, - final List<AnnotationExpr> annotations, final boolean isInterface, final String name, - final List<TypeParameter> typeParameters, final List<ClassOrInterfaceType> extendsList, - final List<ClassOrInterfaceType> implementsList, final List<BodyDeclaration> members) { - super(range, annotations, modifiers, name, members); - setInterface(isInterface); - setTypeParameters(typeParameters); - setExtends(extendsList); - setImplements(implementsList); - } - - @Override public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { - return v.visit(this, arg); - } - - @Override public <A> void accept(final VoidVisitor<A> v, final A arg) { - v.visit(this, arg); - } - - public List<ClassOrInterfaceType> getExtends() { + private boolean interface_; + + private List<TypeParameter> typeParameters; + + // Can contain more than one item if this is an interface + private List<ClassOrInterfaceType> extendsList; + + private List<ClassOrInterfaceType> implementsList; + + public ClassOrInterfaceDeclaration() { + } + + public ClassOrInterfaceDeclaration(final int modifiers, final boolean isInterface, final String name) { + super(modifiers, name); + setInterface(isInterface); + } + + public ClassOrInterfaceDeclaration(final int modifiers, + final List<AnnotationExpr> annotations, final boolean isInterface, + final String name, + final List<TypeParameter> typeParameters, + final List<ClassOrInterfaceType> extendsList, + final List<ClassOrInterfaceType> implementsList, + final List<BodyDeclaration> members) { + super(annotations, modifiers, name, members); + setInterface(isInterface); + setTypeParameters(typeParameters); + setExtends(extendsList); + setImplements(implementsList); + } + + /** + * @deprecated prefer using Range objects. + */ + @Deprecated + public ClassOrInterfaceDeclaration(final int beginLine, final int beginColumn, final int endLine, + final int endColumn, final int modifiers, + final List<AnnotationExpr> annotations, final boolean isInterface, + final String name, + final List<TypeParameter> typeParameters, + final List<ClassOrInterfaceType> extendsList, + final List<ClassOrInterfaceType> implementsList, + final List<BodyDeclaration> members) { + this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), modifiers, annotations, isInterface, name, + typeParameters, extendsList, implementsList, members); + } + + public ClassOrInterfaceDeclaration(Range range, final int modifiers, + final List<AnnotationExpr> annotations, final boolean isInterface, + final String name, + final List<TypeParameter> typeParameters, + final List<ClassOrInterfaceType> extendsList, + final List<ClassOrInterfaceType> implementsList, + final List<BodyDeclaration> members) { + super(range, annotations, modifiers, name, members); + setInterface(isInterface); + setTypeParameters(typeParameters); + setExtends(extendsList); + setImplements(implementsList); + } + + @Override + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + public List<ClassOrInterfaceType> getExtends() { extendsList = ensureNotNull(extendsList); return extendsList; - } + } - public List<ClassOrInterfaceType> getImplements() { + public List<ClassOrInterfaceType> getImplements() { implementsList = ensureNotNull(implementsList); return implementsList; - } + } - public List<TypeParameter> getTypeParameters() { - typeParameters = ensureNotNull(typeParameters); + public List<TypeParameter> getTypeParameters() { + typeParameters = ensureNotNull(typeParameters); return typeParameters; - } + } public boolean isInterface() { - return interface_; - } + return interface_; + } /** * * @param extendsList a null value is currently treated as an empty list. This behavior could change - * in the future, so please avoid passing null + * in the future, so please avoid passing null */ - public void setExtends(final List<ClassOrInterfaceType> extendsList) { - this.extendsList = extendsList; - setAsParentNodeOf(this.extendsList); - } + public void setExtends(final List<ClassOrInterfaceType> extendsList) { + this.extendsList = extendsList; + setAsParentNodeOf(this.extendsList); + } /** * * @param implementsList a null value is currently treated as an empty list. This behavior could change - * in the future, so please avoid passing null + * in the future, so please avoid passing null */ - public void setImplements(final List<ClassOrInterfaceType> implementsList) { - this.implementsList = implementsList; - setAsParentNodeOf(this.implementsList); - } + public void setImplements(final List<ClassOrInterfaceType> implementsList) { + this.implementsList = implementsList; + setAsParentNodeOf(this.implementsList); + } - public void setInterface(final boolean interface_) { - this.interface_ = interface_; - } + public void setInterface(final boolean interface_) { + this.interface_ = interface_; + } /** * * @param typeParameters a null value is currently treated as an empty list. This behavior could change - * in the future, so please avoid passing null + * in the future, so please avoid passing null + */ + public void setTypeParameters(final List<TypeParameter> typeParameters) { + this.typeParameters = typeParameters; + setAsParentNodeOf(this.typeParameters); + } + + /** + * Add an extends to this class or interface and automatically add the import + * + * @param clazz the class to extand from + * @return this, the {@link ClassOrInterfaceDeclaration} + */ + public ClassOrInterfaceDeclaration addExtends(Class<?> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addExtends(clazz.getSimpleName()); + } + + /** + * Add an extends to this class or interface + * + * @param name the name of the type to extends from + * @return this, the {@link ClassOrInterfaceDeclaration} + */ + public ClassOrInterfaceDeclaration addExtends(String name) { + ClassOrInterfaceType classOrInterfaceType = new ClassOrInterfaceType(name); + getExtends().add(classOrInterfaceType); + classOrInterfaceType.setParentNode(this); + return this; + } + + /** + * Add an implements to this class or interface + * + * @param name the name of the type to extends from + * @return this, the {@link ClassOrInterfaceDeclaration} + */ + public ClassOrInterfaceDeclaration addImplements(String name) { + ClassOrInterfaceType classOrInterfaceType = new ClassOrInterfaceType(name); + getImplements().add(classOrInterfaceType); + classOrInterfaceType.setParentNode(this); + return this; + } + + /** + * Add an implements to this class or interface and automatically add the import + * + * @param clazz the type to implements from + * @return this, the {@link ClassOrInterfaceDeclaration} + */ + public ClassOrInterfaceDeclaration addImplements(Class<?> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addImplements(clazz.getSimpleName()); + } + + /** + * Annotates this + * + * @param name the name of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + public NormalAnnotationExpr addAnnotation(String name) { + NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr( + ASTHelper.createNameExpr(name), null); + getAnnotations().add(normalAnnotationExpr); + normalAnnotationExpr.setParentNode(this); + return normalAnnotationExpr; + } + + /** + * Annotates this and automatically add the import + * + * @param clazz the class of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + public NormalAnnotationExpr addAnnotation(Class<? extends Annotation> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a marker annotation + * + * @param name the name of the annotation + * @return this + */ + public ClassOrInterfaceDeclaration addMarkerAnnotation(String name) { + MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr( + ASTHelper.createNameExpr(name)); + getAnnotations().add(markerAnnotationExpr); + markerAnnotationExpr.setParentNode(this); + return this; + } + + /** + * Annotates this with a marker annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + public ClassOrInterfaceDeclaration addMarkerAnnotation(Class<? extends Annotation> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addMarkerAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a single member annotation + * + * @param name the name of the annotation + * @return this + */ + public ClassOrInterfaceDeclaration addSingleMemberAnnotation(String name, String value) { + SingleMemberAnnotationExpr singleMemberAnnotationExpr = new SingleMemberAnnotationExpr( + ASTHelper.createNameExpr(name), ASTHelper.createNameExpr(value)); + getAnnotations().add(singleMemberAnnotationExpr); + singleMemberAnnotationExpr.setParentNode(this); + return this; + } + + /** + * Annotates this with a single member annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + public ClassOrInterfaceDeclaration addSingleMemberAnnotation(Class<? extends Annotation> clazz, String value) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addSingleMemberAnnotation(clazz.getSimpleName(), value); + } + + /** + * Add a field to this {@link ClassOrInterfaceDeclaration} and automatically add the import of the type if needed + * + * @param typeClass the type of the field + * @param modifiers the modifiers like {@link ModifierSet#PUBLIC} + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addField(Class<?> typeClass, int modifiers, String name) { + if (!ClassUtils.isPrimitiveOrWrapper(typeClass) && !typeClass.isArray()) { + Node parentNode = getParentNode(); + if (parentNode != null && parentNode instanceof CompilationUnit) { + ((CompilationUnit) parentNode).addImportWithoutDuplicates(typeClass); + } + } + return addField(typeClass.getSimpleName(), modifiers, name); + } + + /** + * Add a field to this {@link ClassOrInterfaceDeclaration} + * + * @param type the type of the field + * @param modifiers the modifiers like {@link ModifierSet#PUBLIC} + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addField(String type, int modifiers, String name) { + FieldDeclaration fieldDeclaration = new FieldDeclaration(); + fieldDeclaration.getVariables().add(new VariableDeclarator(new VariableDeclaratorId(name))); + fieldDeclaration.setModifiers(modifiers); + fieldDeclaration.setType(new ClassOrInterfaceType(type)); + getMembers().add(fieldDeclaration); + fieldDeclaration.setParentNode(this); + return fieldDeclaration; + } + + /** + * Add a private field to this {@link ClassOrInterfaceDeclaration} + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPrivateField(Class<?> typeClass, String name) { + return addField(typeClass, ModifierSet.PRIVATE, name); + } + + /** + * Add a private field to this {@link ClassOrInterfaceDeclaration} and automatically add the import of the type if + * needed + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPrivateField(String type, String name) { + return addField(type, ModifierSet.PRIVATE, name); + } + + /** + * Add a public field to this {@link ClassOrInterfaceDeclaration} + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPublicField(Class<?> typeClass, String name) { + return addField(typeClass, ModifierSet.PUBLIC, name); + } + + /** + * Add a public field to this {@link ClassOrInterfaceDeclaration} and automatically add the import of the type if + * needed + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPublicField(String type, String name) { + return addField(type, ModifierSet.PUBLIC, name); + } + + /** + * Add a protected field to this {@link ClassOrInterfaceDeclaration} + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addProtectedField(Class<?> typeClass, String name) { + return addField(typeClass, ModifierSet.PROTECTED, name); + } + + /** + * Add a protected field to this {@link ClassOrInterfaceDeclaration} and automatically add the import of the type + * if needed + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addProtectedField(String type, String name) { + return addField(type, ModifierSet.PROTECTED, name); + } + + /** + * Adds a methods to this {@link ClassOrInterfaceDeclaration} + * + * @param methodName the method name + * @param modifiers the modifiers like {@link ModifierSet#PUBLIC} + * @return the {@link MethodDeclaration} created */ - public void setTypeParameters(final List<TypeParameter> typeParameters) { - this.typeParameters = typeParameters; - setAsParentNodeOf(this.typeParameters); - } + public MethodDeclaration addMethod(String methodName, int modifiers) { + MethodDeclaration methodDeclaration = new MethodDeclaration(); + methodDeclaration.setName(methodName); + methodDeclaration.setModifiers(modifiers); + getMembers().add(methodDeclaration); + methodDeclaration.setParentNode(this); + return methodDeclaration; + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumConstantDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumConstantDeclaration.java index 38fdcb35f..c2c67020f 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumConstantDeclaration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumConstantDeclaration.java @@ -21,6 +21,12 @@ package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.util.List; + +import com.github.javaparser.ASTHelper; import com.github.javaparser.Range; import com.github.javaparser.ast.DocumentableNode; import com.github.javaparser.ast.NamedNode; @@ -30,11 +36,6 @@ import com.github.javaparser.ast.expr.Expression; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.ensureNotNull; - /** * @author Julio Vilmar Gesser */ @@ -121,4 +122,9 @@ public final class EnumConstantDeclaration extends BodyDeclaration implements Do } return null; } + + public EnumConstantDeclaration addArgument(String valueExpr) { + getArgs().add(ASTHelper.createNameExpr(valueExpr)); + return this; + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumDeclaration.java index fc863e98b..e00429c01 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumDeclaration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/EnumDeclaration.java @@ -21,17 +21,25 @@ package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.lang.annotation.Annotation; +import java.util.List; + +import com.github.javaparser.ASTHelper; +import com.github.javaparser.ClassUtils; import com.github.javaparser.Range; +import com.github.javaparser.ast.CompilationUnit; +import com.github.javaparser.ast.Node; import com.github.javaparser.ast.expr.AnnotationExpr; +import com.github.javaparser.ast.expr.MarkerAnnotationExpr; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; +import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.*; - /** * @author Julio Vilmar Gesser */ @@ -98,4 +106,243 @@ public final class EnumDeclaration extends TypeDeclaration { this.implementsList = implementsList; setAsParentNodeOf(this.implementsList); } + + /** + * Add an implements to this enum + * + * @param name the name of the type to extends from + * @return this, the {@link EnumDeclaration} + */ + public EnumDeclaration addImplements(String name) { + ClassOrInterfaceType classOrInterfaceType = new ClassOrInterfaceType(name); + getImplements().add(classOrInterfaceType); + classOrInterfaceType.setParentNode(this); + return this; + } + + /** + * Add an implements to this enum and automatically add the import + * + * @param clazz the type to implements from + * @return this, the {@link EnumDeclaration} + */ + public EnumDeclaration addImplements(Class<?> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addImplements(clazz.getSimpleName()); + } + + public EnumConstantDeclaration addEnumConstant(String name) { + EnumConstantDeclaration enumConstant = new EnumConstantDeclaration(name); + getEntries().add(enumConstant); + enumConstant.setParentNode(this); + return enumConstant; + } + + /** + * Annotates this + * + * @param name the name of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + public NormalAnnotationExpr addAnnotation(String name) { + NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr( + ASTHelper.createNameExpr(name), null); + getAnnotations().add(normalAnnotationExpr); + normalAnnotationExpr.setParentNode(this); + return normalAnnotationExpr; + } + + /** + * Annotates this and automatically add the import + * + * @param clazz the class of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + public NormalAnnotationExpr addAnnotation(Class<? extends Annotation> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a marker annotation + * + * @param name the name of the annotation + * @return this + */ + public EnumDeclaration addMarkerAnnotation(String name) { + MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr( + ASTHelper.createNameExpr(name)); + getAnnotations().add(markerAnnotationExpr); + markerAnnotationExpr.setParentNode(this); + return this; + } + + /** + * Annotates this with a marker annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + public EnumDeclaration addMarkerAnnotation(Class<? extends Annotation> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addMarkerAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a single member annotation + * + * @param name the name of the annotation + * @return this + */ + public EnumDeclaration addSingleMemberAnnotation(String name, String value) { + SingleMemberAnnotationExpr singleMemberAnnotationExpr = new SingleMemberAnnotationExpr( + ASTHelper.createNameExpr(name), ASTHelper.createNameExpr(value)); + getAnnotations().add(singleMemberAnnotationExpr); + singleMemberAnnotationExpr.setParentNode(this); + return this; + } + + /** + * Annotates this with a single member annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + public EnumDeclaration addSingleMemberAnnotation(Class<? extends Annotation> clazz, String value) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addSingleMemberAnnotation(clazz.getSimpleName(), value); + } + + /** + * Add a field to this {@link EnumDeclaration} and automatically add the import of the type if needed + * + * @param typeClass the type of the field + * @param modifiers the modifiers like {@link ModifierSet#PUBLIC} + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addField(Class<?> typeClass, int modifiers, String name) { + if (!ClassUtils.isPrimitiveOrWrapper(typeClass) && !typeClass.isArray()) { + Node parentNode = getParentNode(); + if (parentNode != null && parentNode instanceof CompilationUnit) { + ((CompilationUnit) parentNode).addImportWithoutDuplicates(typeClass); + } + } + return addField(typeClass.getSimpleName(), modifiers, name); + } + + /** + * Add a field to this {@link EnumDeclaration} + * + * @param type the type of the field + * @param modifiers the modifiers like {@link ModifierSet#PUBLIC} + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addField(String type, int modifiers, String name) { + FieldDeclaration fieldDeclaration = new FieldDeclaration(); + fieldDeclaration.getVariables().add(new VariableDeclarator(new VariableDeclaratorId(name))); + fieldDeclaration.setModifiers(modifiers); + fieldDeclaration.setType(new ClassOrInterfaceType(type)); + getMembers().add(fieldDeclaration); + fieldDeclaration.setParentNode(this); + return fieldDeclaration; + } + + /** + * Add a private field to this {@link EnumDeclaration} + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPrivateField(Class<?> typeClass, String name) { + return addField(typeClass, ModifierSet.PRIVATE, name); + } + + /** + * Add a private field to this {@link EnumDeclaration} and automatically add the import of the type if + * needed + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPrivateField(String type, String name) { + return addField(type, ModifierSet.PRIVATE, name); + } + + /** + * Add a public field to this {@link EnumDeclaration} + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPublicField(Class<?> typeClass, String name) { + return addField(typeClass, ModifierSet.PUBLIC, name); + } + + /** + * Add a public field to this {@link EnumDeclaration} and automatically add the import of the type if + * needed + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addPublicField(String type, String name) { + return addField(type, ModifierSet.PUBLIC, name); + } + + /** + * Add a protected field to this {@link EnumDeclaration} + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addProtectedField(Class<?> typeClass, String name) { + return addField(typeClass, ModifierSet.PROTECTED, name); + } + + /** + * Add a protected field to this {@link EnumDeclaration} and automatically add the import of the type + * if needed + * + * @param type the type of the field + * @param name the name of the field + * @return the {@link FieldDeclaration} created + */ + public FieldDeclaration addProtectedField(String type, String name) { + return addField(type, ModifierSet.PROTECTED, name); + } + + /** + * Adds a methods to this {@link EnumDeclaration} + * + * @param methodName the method name + * @param modifiers the modifiers like {@link ModifierSet#PUBLIC} + * @return the {@link MethodDeclaration} created + */ + public MethodDeclaration addMethod(String methodName, int modifiers) { + MethodDeclaration methodDeclaration = new MethodDeclaration(); + methodDeclaration.setName(methodName); + methodDeclaration.setModifiers(modifiers); + getMembers().add(methodDeclaration); + methodDeclaration.setParentNode(this); + return methodDeclaration; + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java index a966e430c..a8dd47549 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/FieldDeclaration.java @@ -18,29 +18,38 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ - + package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.List; + +import com.github.javaparser.ASTHelper; import com.github.javaparser.Range; +import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.DocumentableNode; import com.github.javaparser.ast.NodeWithModifiers; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.expr.AnnotationExpr; +import com.github.javaparser.ast.expr.MarkerAnnotationExpr; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; +import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; +import com.github.javaparser.ast.stmt.BlockStmt; +import com.github.javaparser.ast.stmt.ReturnStmt; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.ArrayList; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.*; - /** * @author Julio Vilmar Gesser */ -public final class FieldDeclaration extends BodyDeclaration implements DocumentableNode, TypedNode, NodeWithModifiers { +public final class FieldDeclaration extends BodyDeclaration + implements DocumentableNode, TypedNode<FieldDeclaration>, NodeWithModifiers { private int modifiers; @@ -52,39 +61,42 @@ public final class FieldDeclaration extends BodyDeclaration implements Documenta } public FieldDeclaration(int modifiers, Type type, VariableDeclarator variable) { - setModifiers(modifiers); - setType(type); - List<VariableDeclarator> aux = new ArrayList<VariableDeclarator>(); - aux.add(variable); - setVariables(aux); + setModifiers(modifiers); + setType(type); + List<VariableDeclarator> aux = new ArrayList<>(); + aux.add(variable); + setVariables(aux); } public FieldDeclaration(int modifiers, Type type, List<VariableDeclarator> variables) { - setModifiers(modifiers); - setType(type); - setVariables(variables); + setModifiers(modifiers); + setType(type); + setVariables(variables); } - public FieldDeclaration(int modifiers, List<AnnotationExpr> annotations, Type type, List<VariableDeclarator> variables) { + public FieldDeclaration(int modifiers, List<AnnotationExpr> annotations, Type type, + List<VariableDeclarator> variables) { super(annotations); setModifiers(modifiers); - setType(type); - setVariables(variables); + setType(type); + setVariables(variables); } /** * @deprecated prefer using Range objects. */ @Deprecated - public FieldDeclaration(int beginLine, int beginColumn, int endLine, int endColumn, int modifiers, List<AnnotationExpr> annotations, Type type, List<VariableDeclarator> variables) { + public FieldDeclaration(int beginLine, int beginColumn, int endLine, int endColumn, int modifiers, + List<AnnotationExpr> annotations, Type type, List<VariableDeclarator> variables) { this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), modifiers, annotations, type, variables); } - - public FieldDeclaration(Range range, int modifiers, List<AnnotationExpr> annotations, Type type, List<VariableDeclarator> variables) { + + public FieldDeclaration(Range range, int modifiers, List<AnnotationExpr> annotations, Type type, + List<VariableDeclarator> variables) { super(range, annotations); setModifiers(modifiers); - setType(type); - setVariables(variables); + setType(type); + setVariables(variables); } @Override @@ -118,26 +130,166 @@ public final class FieldDeclaration extends BodyDeclaration implements Documenta return variables; } - public void setModifiers(int modifiers) { + public FieldDeclaration setModifiers(int modifiers) { this.modifiers = modifiers; + return this; + } + + public FieldDeclaration addModifier(int modifier) { + this.modifiers |= modifier; + return this; } @Override - public void setType(Type type) { + public FieldDeclaration setType(Type type) { this.type = type; - setAsParentNodeOf(this.type); + setAsParentNodeOf(this.type); + return this; } public void setVariables(List<VariableDeclarator> variables) { this.variables = variables; - setAsParentNodeOf(this.variables); + setAsParentNodeOf(this.variables); } @Override public JavadocComment getJavaDoc() { - if(getComment() instanceof JavadocComment){ + if (getComment() instanceof JavadocComment) { return (JavadocComment) getComment(); } return null; } + + /** + * Annotates this + * + * @param name the name of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + public NormalAnnotationExpr addAnnotation(String name) { + NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr( + ASTHelper.createNameExpr(name), null); + getAnnotations().add(normalAnnotationExpr); + normalAnnotationExpr.setParentNode(this); + return normalAnnotationExpr; + } + + /** + * Annotates this and automatically add the import + * + * @param clazz the class of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + public NormalAnnotationExpr addAnnotation(Class<? extends Annotation> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a marker annotation + * + * @param name the name of the annotation + * @return this + */ + public FieldDeclaration addMarkerAnnotation(String name) { + MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr( + ASTHelper.createNameExpr(name)); + getAnnotations().add(markerAnnotationExpr); + markerAnnotationExpr.setParentNode(this); + return this; + } + + /** + * Annotates this with a marker annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + public FieldDeclaration addMarkerAnnotation(Class<? extends Annotation> clazz) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addMarkerAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a single member annotation + * + * @param name the name of the annotation + * @return this + */ + public FieldDeclaration addSingleMemberAnnotation(String name, String value) { + SingleMemberAnnotationExpr singleMemberAnnotationExpr = new SingleMemberAnnotationExpr( + ASTHelper.createNameExpr(name), ASTHelper.createNameExpr(value)); + getAnnotations().add(singleMemberAnnotationExpr); + singleMemberAnnotationExpr.setParentNode(this); + return this; + } + + /** + * Annotates this with a single member annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + public FieldDeclaration addSingleMemberAnnotation(Class<? extends Annotation> clazz, String value) { + CompilationUnit parentNode = getParentNodeOfType(CompilationUnit.class); + if (parentNode != null) { + parentNode.addImportWithoutDuplicates(clazz); + } + return addSingleMemberAnnotation(clazz.getSimpleName(), value); + } + + /** + * Create a getter for this field, <b>will only work if this field declares only 1 identifier and if this field is + * already added to a ClassOrInterfaceDeclaration</b> + * + * @return the {@link MethodDeclaration} created + */ + public MethodDeclaration createGetter() { + if (getVariables().size() != 1) + throw new IllegalStateException("You can use this only when the field declares only 1 variable name"); + ClassOrInterfaceDeclaration parent = getParentNodeOfType(ClassOrInterfaceDeclaration.class); + if (parent == null) + throw new IllegalStateException( + "You can use this only when the field is attached to a ClassOrInterfaceDeclaration"); + + String fieldName = getVariables().get(0).getId().getName(); + fieldName = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length()); + MethodDeclaration getter = parent.addMethod("get" + fieldName, ModifierSet.PUBLIC).setType(getType()); + BlockStmt blockStmt = new BlockStmt(); + getter.setBody(blockStmt); + ReturnStmt r = new ReturnStmt(ASTHelper.createNameExpr(fieldName)); + ASTHelper.addStmt(blockStmt, r); + return getter; + } + + /** + * Create a setter for this field, <b>will only work if this field declares only 1 identifier and if this field is + * already added to a ClassOrInterfaceDeclaration</b> + * + * @return the {@link MethodDeclaration} created + */ + public MethodDeclaration createSetter() { + if (getVariables().size() != 1) + throw new IllegalStateException("You can use this only when the field declares only 1 variable name"); + ClassOrInterfaceDeclaration parent = getParentNodeOfType(ClassOrInterfaceDeclaration.class); + if (parent == null) + throw new IllegalStateException( + "You can use this only when the field is attached to a ClassOrInterfaceDeclaration"); + String fieldName = getVariables().get(0).getId().getName(); + fieldName = fieldName.toUpperCase().substring(0, 1) + fieldName.substring(1, fieldName.length()); + + MethodDeclaration setter = parent.addMethod("set" + fieldName, ModifierSet.PUBLIC).setType(ASTHelper.VOID_TYPE); + setter.getParameters().add(new Parameter(getType(), new VariableDeclaratorId(fieldName))); + BlockStmt blockStmt2 = new BlockStmt(); + setter.setBody(blockStmt2); + ASTHelper.addStmt(blockStmt2, ASTHelper.createNameExpr("this." + fieldName + " = " + fieldName + ";")); + return setter; + } + } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java index 7c2e606c3..9aa0cd841 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/MethodDeclaration.java @@ -18,11 +18,21 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ - + package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.util.List; + import com.github.javaparser.Range; -import com.github.javaparser.ast.*; +import com.github.javaparser.ast.AccessSpecifier; +import com.github.javaparser.ast.DocumentableNode; +import com.github.javaparser.ast.NamedNode; +import com.github.javaparser.ast.NodeWithModifiers; +import com.github.javaparser.ast.TypeParameter; +import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.expr.AnnotationExpr; import com.github.javaparser.ast.expr.NameExpr; @@ -32,199 +42,209 @@ import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.ensureNotNull; - /** * @author Julio Vilmar Gesser */ -public final class MethodDeclaration extends BodyDeclaration implements DocumentableNode, WithDeclaration, NamedNode, TypedNode, NodeWithModifiers { +public final class MethodDeclaration extends BodyDeclaration + implements DocumentableNode, WithDeclaration, NamedNode, TypedNode<MethodDeclaration>, NodeWithModifiers { - private int modifiers; + private int modifiers; - private List<TypeParameter> typeParameters; + private List<TypeParameter> typeParameters; - private Type type; + private Type type; - private NameExpr name; + private NameExpr name; - private List<Parameter> parameters; + private List<Parameter> parameters; - private int arrayCount; + private int arrayCount; - private List<ReferenceType> throws_; + private List<ReferenceType> throws_; - private BlockStmt body; + private BlockStmt body; private boolean isDefault = false; public MethodDeclaration() { - } - - public MethodDeclaration(final int modifiers, final Type type, final String name) { - setModifiers(modifiers); - setType(type); - setName(name); - } - - public MethodDeclaration(final int modifiers, final Type type, final String name, final List<Parameter> parameters) { - setModifiers(modifiers); - setType(type); - setName(name); - setParameters(parameters); - } - - public MethodDeclaration(final int modifiers, final List<AnnotationExpr> annotations, - final List<TypeParameter> typeParameters, final Type type, final String name, - final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, final BlockStmt body) { - super(annotations); - setModifiers(modifiers); - setTypeParameters(typeParameters); - setType(type); - setName(name); - setParameters(parameters); - setArrayCount(arrayCount); - setThrows(throws_); - setBody(body); - } - - /** - * @deprecated prefer using Range objects. - */ - @Deprecated - public MethodDeclaration(final int beginLine, final int beginColumn, final int endLine, final int endColumn, - final int modifiers, final List<AnnotationExpr> annotations, - final List<TypeParameter> typeParameters, final Type type, final String name, - final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, final BlockStmt body) { - this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), modifiers, annotations, typeParameters, type, name, parameters, arrayCount, throws_, body); - } - - public MethodDeclaration(Range range, - final int modifiers, final List<AnnotationExpr> annotations, - final List<TypeParameter> typeParameters, final Type type, final String name, - final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, final BlockStmt body) { - super(range, annotations); - setModifiers(modifiers); - setTypeParameters(typeParameters); - setType(type); - setName(name); - setParameters(parameters); - setArrayCount(arrayCount); - setThrows(throws_); - setBody(body); - } - - @Override public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { - return v.visit(this, arg); - } - - @Override public <A> void accept(final VoidVisitor<A> v, final A arg) { - v.visit(this, arg); - } - - public int getArrayCount() { - return arrayCount; - } - - public BlockStmt getBody() { - return body; - } - - /** - * Return the modifiers of this member declaration. - * - * @see ModifierSet - * @return modifiers - */ - @Override - public int getModifiers() { - return modifiers; - } - - @Override - public String getName() { - return name.getName(); - } + } + + public MethodDeclaration(final int modifiers, final Type type, final String name) { + setModifiers(modifiers); + setType(type); + setName(name); + } + + public MethodDeclaration(final int modifiers, final Type type, final String name, + final List<Parameter> parameters) { + setModifiers(modifiers); + setType(type); + setName(name); + setParameters(parameters); + } + + public MethodDeclaration(final int modifiers, final List<AnnotationExpr> annotations, + final List<TypeParameter> typeParameters, final Type type, final String name, + final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, + final BlockStmt body) { + super(annotations); + setModifiers(modifiers); + setTypeParameters(typeParameters); + setType(type); + setName(name); + setParameters(parameters); + setArrayCount(arrayCount); + setThrows(throws_); + setBody(body); + } + + /** + * @deprecated prefer using Range objects. + */ + @Deprecated + public MethodDeclaration(final int beginLine, final int beginColumn, final int endLine, final int endColumn, + final int modifiers, final List<AnnotationExpr> annotations, + final List<TypeParameter> typeParameters, final Type type, final String name, + final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, + final BlockStmt body) { + this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), modifiers, annotations, typeParameters, + type, name, parameters, arrayCount, throws_, body); + } + + public MethodDeclaration(Range range, + final int modifiers, final List<AnnotationExpr> annotations, + final List<TypeParameter> typeParameters, final Type type, final String name, + final List<Parameter> parameters, final int arrayCount, final List<ReferenceType> throws_, + final BlockStmt body) { + super(range, annotations); + setModifiers(modifiers); + setTypeParameters(typeParameters); + setType(type); + setName(name); + setParameters(parameters); + setArrayCount(arrayCount); + setThrows(throws_); + setBody(body); + } + + @Override + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + public int getArrayCount() { + return arrayCount; + } + + public BlockStmt getBody() { + return body; + } + + /** + * Return the modifiers of this member declaration. + * + * @see ModifierSet + * @return modifiers + */ + @Override + public int getModifiers() { + return modifiers; + } + + @Override + public String getName() { + return name.getName(); + } public NameExpr getNameExpr() { return name; } - public List<Parameter> getParameters() { + public List<Parameter> getParameters() { parameters = ensureNotNull(parameters); return parameters; - } + } - public List<ReferenceType> getThrows() { + public List<ReferenceType> getThrows() { throws_ = ensureNotNull(throws_); return throws_; - } + } - @Override - public Type getType() { - return type; - } + @Override + public Type getType() { + return type; + } - public List<TypeParameter> getTypeParameters() { + public List<TypeParameter> getTypeParameters() { typeParameters = ensureNotNull(typeParameters); return typeParameters; - } + } - public void setArrayCount(final int arrayCount) { - this.arrayCount = arrayCount; - } + public void setArrayCount(final int arrayCount) { + this.arrayCount = arrayCount; + } - public void setBody(final BlockStmt body) { - this.body = body; - setAsParentNodeOf(this.body); - } + public MethodDeclaration setBody(final BlockStmt body) { + this.body = body; + setAsParentNodeOf(this.body); + return this; + } - public void setModifiers(final int modifiers) { - this.modifiers = modifiers; - } + public MethodDeclaration setModifiers(final int modifiers) { + this.modifiers = modifiers; + return this; + } - public void setName(final String name) { - setNameExpr(new NameExpr(name)); - } + public MethodDeclaration setName(final String name) { + setNameExpr(new NameExpr(name)); + return this; + } - public void setNameExpr(final NameExpr name) { + public MethodDeclaration setNameExpr(final NameExpr name) { this.name = name; - setAsParentNodeOf(this.name); + setAsParentNodeOf(this.name); + return this; } - public void setParameters(final List<Parameter> parameters) { - this.parameters = parameters; - setAsParentNodeOf(this.parameters); - } - - public void setThrows(final List<ReferenceType> throws_) { - this.throws_ = throws_; - setAsParentNodeOf(this.throws_); - } + public MethodDeclaration setParameters(final List<Parameter> parameters) { + this.parameters = parameters; + setAsParentNodeOf(this.parameters); + return this; + } - @Override - public void setType(final Type type) { - this.type = type; - setAsParentNodeOf(this.type); - } + public MethodDeclaration setThrows(final List<ReferenceType> throws_) { + this.throws_ = throws_; + setAsParentNodeOf(this.throws_); + return this; + } - public void setTypeParameters(final List<TypeParameter> typeParameters) { - this.typeParameters = typeParameters; - setAsParentNodeOf(typeParameters); - } + @Override + public MethodDeclaration setType(final Type type) { + this.type = type; + setAsParentNodeOf(this.type); + return this; + } + public MethodDeclaration setTypeParameters(final List<TypeParameter> typeParameters) { + this.typeParameters = typeParameters; + setAsParentNodeOf(typeParameters); + return this; + } public boolean isDefault() { return isDefault; } - public void setDefault(boolean isDefault) { + public MethodDeclaration setDefault(boolean isDefault) { this.isDefault = isDefault; + return this; } - @Override public String getDeclarationAsString() { return getDeclarationAsString(true, true, true); @@ -234,35 +254,37 @@ public final class MethodDeclaration extends BodyDeclaration implements Document public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows) { return getDeclarationAsString(includingModifiers, includingThrows, true); } - + /** * The declaration returned has this schema: * * [accessSpecifier] [static] [abstract] [final] [native] * [synchronized] returnType methodName ([paramType [paramName]]) * [throws exceptionsList] + * * @return method declaration as String */ @Override - public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName) { + public String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, + boolean includingParameterName) { StringBuilder sb = new StringBuilder(); if (includingModifiers) { AccessSpecifier accessSpecifier = ModifierSet.getAccessSpecifier(getModifiers()); sb.append(accessSpecifier.getCodeRepresenation()); sb.append(accessSpecifier == AccessSpecifier.DEFAULT ? "" : " "); - if (ModifierSet.isStatic(getModifiers())){ + if (ModifierSet.isStatic(getModifiers())) { sb.append("static "); } - if (ModifierSet.isAbstract(getModifiers())){ + if (ModifierSet.isAbstract(getModifiers())) { sb.append("abstract "); } - if (ModifierSet.isFinal(getModifiers())){ + if (ModifierSet.isFinal(getModifiers())) { sb.append("final "); } - if (ModifierSet.isNative(getModifiers())){ + if (ModifierSet.isNative(getModifiers())) { sb.append("native "); } - if (ModifierSet.isSynchronized(getModifiers())){ + if (ModifierSet.isSynchronized(getModifiers())) { sb.append("synchronized "); } } @@ -272,8 +294,7 @@ public final class MethodDeclaration extends BodyDeclaration implements Document sb.append(getName()); sb.append("("); boolean firstParam = true; - for (Parameter param : getParameters()) - { + for (Parameter param : getParameters()) { if (firstParam) { firstParam = false; } else { @@ -284,7 +305,7 @@ public final class MethodDeclaration extends BodyDeclaration implements Document } else { sb.append(param.getType().toStringWithoutComments()); if (param.isVarArgs()) { - sb.append("..."); + sb.append("..."); } } } @@ -304,11 +325,11 @@ public final class MethodDeclaration extends BodyDeclaration implements Document return sb.toString(); } - @Override - public JavadocComment getJavaDoc() { - if(getComment() instanceof JavadocComment){ - return (JavadocComment) getComment(); - } - return null; - } + @Override + public JavadocComment getJavaDoc() { + if (getComment() instanceof JavadocComment) { + return (JavadocComment) getComment(); + } + return null; + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java b/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java index 4cdbbadd5..adcb909d0 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/body/Parameter.java @@ -21,6 +21,10 @@ package com.github.javaparser.ast.body; +import static com.github.javaparser.Position.pos; + +import java.util.List; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.expr.AnnotationExpr; @@ -28,14 +32,10 @@ import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; - /** * @author Julio Vilmar Gesser */ -public final class Parameter extends BaseParameter implements TypedNode { +public final class Parameter extends BaseParameter implements TypedNode<Parameter> { private Type type; private boolean isVarArgs; @@ -87,9 +87,10 @@ public final class Parameter extends BaseParameter implements TypedNode { } @Override - public void setType(Type type) { + public Parameter setType(Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } public void setVarArgs(boolean isVarArgs) { diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java index a4b8dd514..ac64ae05b 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ArrayCreationExpr.java @@ -21,21 +21,21 @@ package com.github.javaparser.ast.expr; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.util.List; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.*; - /** * @author Julio Vilmar Gesser */ -public final class ArrayCreationExpr extends Expression implements TypedNode { +public final class ArrayCreationExpr extends Expression implements TypedNode<ArrayCreationExpr> { private Type type; @@ -139,9 +139,10 @@ public final class ArrayCreationExpr extends Expression implements TypedNode { } @Override - public void setType(Type type) { + public ArrayCreationExpr setType(Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } public List<List<AnnotationExpr>> getArraysAnnotations() { diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java index fbc776845..a0ce8a84e 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/CastExpr.java @@ -21,18 +21,18 @@ package com.github.javaparser.ast.expr; +import static com.github.javaparser.Position.pos; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import static com.github.javaparser.Position.pos; - /** * @author Julio Vilmar Gesser */ -public final class CastExpr extends Expression implements TypedNode { +public final class CastExpr extends Expression implements TypedNode<CastExpr> { private Type type; @@ -85,8 +85,9 @@ public final class CastExpr extends Expression implements TypedNode { } @Override - public void setType(Type type) { + public CastExpr setType(Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java index ad97aef38..161aeeee7 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/ClassExpr.java @@ -21,14 +21,14 @@ package com.github.javaparser.ast.expr; +import static com.github.javaparser.Position.pos; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import static com.github.javaparser.Position.pos; - /** * Defines an expression that accesses the class of a type. * Example: @@ -37,7 +37,7 @@ import static com.github.javaparser.Position.pos; * </code> * @author Julio Vilmar Gesser */ -public final class ClassExpr extends Expression implements TypedNode { +public final class ClassExpr extends Expression implements TypedNode<ClassExpr> { private Type type; @@ -77,8 +77,9 @@ public final class ClassExpr extends Expression implements TypedNode { } @Override - public void setType(Type type) { + public ClassExpr setType(Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java index d0426d64c..43d1912e5 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/InstanceOfExpr.java @@ -21,18 +21,18 @@ package com.github.javaparser.ast.expr; +import static com.github.javaparser.Position.pos; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import static com.github.javaparser.Position.pos; - /** * @author Julio Vilmar Gesser */ -public final class InstanceOfExpr extends Expression implements TypedNode { +public final class InstanceOfExpr extends Expression implements TypedNode<InstanceOfExpr> { private Expression expr; @@ -84,8 +84,9 @@ public final class InstanceOfExpr extends Expression implements TypedNode { } @Override - public void setType(final Type type) { + public InstanceOfExpr setType(final Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/NormalAnnotationExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/NormalAnnotationExpr.java index c70ff612c..ce09b432a 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/NormalAnnotationExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/NormalAnnotationExpr.java @@ -18,63 +18,91 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ - + package com.github.javaparser.ast.expr; -import com.github.javaparser.Range; -import com.github.javaparser.ast.visitor.GenericVisitor; -import com.github.javaparser.ast.visitor.VoidVisitor; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; import java.util.List; -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.*; +import com.github.javaparser.ASTHelper; +import com.github.javaparser.Range; +import com.github.javaparser.ast.visitor.GenericVisitor; +import com.github.javaparser.ast.visitor.VoidVisitor; /** * @author Julio Vilmar Gesser */ public final class NormalAnnotationExpr extends AnnotationExpr { - private List<MemberValuePair> pairs; - - public NormalAnnotationExpr() { - } - - public NormalAnnotationExpr(final NameExpr name, final List<MemberValuePair> pairs) { - setName(name); - setPairs(pairs); - } - - /** - * @deprecated prefer using Range objects. - */ - @Deprecated - public NormalAnnotationExpr(final int beginLine, final int beginColumn, final int endLine, final int endColumn, - final NameExpr name, final List<MemberValuePair> pairs) { - this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), name, pairs); - } - - public NormalAnnotationExpr(final Range range, final NameExpr name, final List<MemberValuePair> pairs) { - super(range); - setName(name); - setPairs(pairs); - } - - @Override public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { - return v.visit(this, arg); - } - - @Override public <A> void accept(final VoidVisitor<A> v, final A arg) { - v.visit(this, arg); - } - - public List<MemberValuePair> getPairs() { + private List<MemberValuePair> pairs; + + public NormalAnnotationExpr() { + } + + public NormalAnnotationExpr(final NameExpr name, final List<MemberValuePair> pairs) { + setName(name); + setPairs(pairs); + } + + /** + * @deprecated prefer using Range objects. + */ + @Deprecated + public NormalAnnotationExpr(final int beginLine, final int beginColumn, final int endLine, final int endColumn, + final NameExpr name, final List<MemberValuePair> pairs) { + this(new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)), name, pairs); + } + + public NormalAnnotationExpr(final Range range, final NameExpr name, final List<MemberValuePair> pairs) { + super(range); + setName(name); + setPairs(pairs); + } + + @Override + public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { + return v.visit(this, arg); + } + + @Override + public <A> void accept(final VoidVisitor<A> v, final A arg) { + v.visit(this, arg); + } + + public List<MemberValuePair> getPairs() { pairs = ensureNotNull(pairs); return pairs; - } + } + + public void setPairs(final List<MemberValuePair> pairs) { + this.pairs = pairs; + setAsParentNodeOf(this.pairs); + } + + /** + * adds a pair to this annotation + * + * @param key + * @param value + * @return this, the {@link NormalAnnotationExpr} + */ + public NormalAnnotationExpr addPair(String key, String value) { + return addPair(key, ASTHelper.createNameExpr(value)); + } - public void setPairs(final List<MemberValuePair> pairs) { - this.pairs = pairs; - setAsParentNodeOf(this.pairs); - } + /** + * adds a pair to this annotation + * + * @param key + * @param value + * @return this, the {@link NormalAnnotationExpr} + */ + public NormalAnnotationExpr addPair(String key, NameExpr value) { + MemberValuePair memberValuePair = new MemberValuePair(key, value); + getPairs().add(memberValuePair); + memberValuePair.setParentNode(this); + return this; + } } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java index 8a46522b0..8c6e87a45 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/TypeExpr.java @@ -21,20 +21,20 @@ package com.github.javaparser.ast.expr; +import static com.github.javaparser.Position.pos; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import static com.github.javaparser.Position.pos; - /** * This class is just instantiated as scopes for MethodReferenceExpr nodes to encapsulate Types. * @author Raquel Pau * */ -public class TypeExpr extends Expression implements TypedNode { +public class TypeExpr extends Expression implements TypedNode<TypeExpr> { private Type type; @@ -69,9 +69,10 @@ public class TypeExpr extends Expression implements TypedNode { } @Override - public void setType(Type type) { + public TypeExpr setType(Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java index 12afaa9f0..2bcbee340 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/expr/VariableDeclarationExpr.java @@ -21,6 +21,11 @@ package com.github.javaparser.ast.expr; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.util.List; + import com.github.javaparser.Range; import com.github.javaparser.ast.NodeWithModifiers; import com.github.javaparser.ast.TypedNode; @@ -30,15 +35,11 @@ import com.github.javaparser.ast.type.Type; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.*; - /** * @author Julio Vilmar Gesser */ -public final class VariableDeclarationExpr extends Expression implements TypedNode, NodeWithModifiers { +public final class VariableDeclarationExpr extends Expression + implements TypedNode<VariableDeclarationExpr>, NodeWithModifiers { private int modifiers; @@ -126,9 +127,10 @@ public final class VariableDeclarationExpr extends Expression implements TypedNo } @Override - public void setType(final Type type) { + public VariableDeclarationExpr setType(final Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } public void setVars(final List<VariableDeclarator> vars) { diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java b/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java index 0dd665541..763241ac5 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/type/ReferenceType.java @@ -21,21 +21,21 @@ package com.github.javaparser.ast.type; +import static com.github.javaparser.Position.pos; +import static com.github.javaparser.ast.internal.Utils.ensureNotNull; + +import java.util.List; + import com.github.javaparser.Range; import com.github.javaparser.ast.TypedNode; import com.github.javaparser.ast.expr.AnnotationExpr; import com.github.javaparser.ast.visitor.GenericVisitor; import com.github.javaparser.ast.visitor.VoidVisitor; -import java.util.List; - -import static com.github.javaparser.Position.pos; -import static com.github.javaparser.ast.internal.Utils.*; - /** * @author Julio Vilmar Gesser */ -public final class ReferenceType extends Type implements TypedNode { +public final class ReferenceType extends Type implements TypedNode<ReferenceType> { private Type type; @@ -108,9 +108,10 @@ public final class ReferenceType extends Type implements TypedNode { } @Override - public void setType(final Type type) { + public ReferenceType setType(final Type type) { this.type = type; setAsParentNodeOf(this.type); + return this; } /** |