diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2018-03-04 08:21:35 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2018-03-04 08:21:35 +0000 |
commit | b4c1397d5df9370f6358d4f8e9efd27e0f67dec1 (patch) | |
tree | 6789ec288d344cf5fd5d057bcf1efc9545b1af28 /javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes | |
parent | 92d661a1d239131fb5c1e019a8f2ac7584d2d3f6 (diff) | |
parent | 1afe9e0652b9b53edade5aa276162abe27b32a67 (diff) | |
download | platform_external_javaparser-cba172c9c102fc74ac4ecdb1040a84d4dd6aad3a.tar.gz platform_external_javaparser-cba172c9c102fc74ac4ecdb1040a84d4dd6aad3a.tar.bz2 platform_external_javaparser-cba172c9c102fc74ac4ecdb1040a84d4dd6aad3a.zip |
Snap for 4632767 from 1afe9e0652b9b53edade5aa276162abe27b32a67 to pi-releaseandroid-wear-9.0.0_r9android-wear-9.0.0_r8android-wear-9.0.0_r7android-wear-9.0.0_r6android-wear-9.0.0_r5android-wear-9.0.0_r4android-wear-9.0.0_r3android-wear-9.0.0_r23android-wear-9.0.0_r22android-wear-9.0.0_r21android-wear-9.0.0_r20android-wear-9.0.0_r2android-wear-9.0.0_r19android-wear-9.0.0_r18android-wear-9.0.0_r17android-wear-9.0.0_r16android-wear-9.0.0_r15android-wear-9.0.0_r14android-wear-9.0.0_r13android-wear-9.0.0_r12android-wear-9.0.0_r11android-wear-9.0.0_r10android-wear-9.0.0_r1android-vts-9.0_r9android-vts-9.0_r8android-vts-9.0_r7android-vts-9.0_r6android-vts-9.0_r5android-vts-9.0_r4android-vts-9.0_r14android-vts-9.0_r13android-vts-9.0_r12android-vts-9.0_r11android-vts-9.0_r10android-cts-9.0_r9android-cts-9.0_r8android-cts-9.0_r7android-cts-9.0_r6android-cts-9.0_r5android-cts-9.0_r4android-cts-9.0_r3android-cts-9.0_r2android-cts-9.0_r13android-cts-9.0_r12android-cts-9.0_r11android-cts-9.0_r10android-cts-9.0_r1android-9.0.0_r9android-9.0.0_r8android-9.0.0_r7android-9.0.0_r60android-9.0.0_r6android-9.0.0_r59android-9.0.0_r58android-9.0.0_r57android-9.0.0_r56android-9.0.0_r55android-9.0.0_r54android-9.0.0_r53android-9.0.0_r52android-9.0.0_r51android-9.0.0_r50android-9.0.0_r5android-9.0.0_r49android-9.0.0_r48android-9.0.0_r3android-9.0.0_r2android-9.0.0_r18android-9.0.0_r17android-9.0.0_r10android-9.0.0_r1security-pi-releasepie-vts-releasepie-security-releasepie-s2-releasepie-release-2pie-releasepie-r2-s2-releasepie-r2-s1-releasepie-r2-releasepie-platform-releasepie-gsipie-cuttlefish-testingpie-cts-release
Change-Id: Ibe65883e94ed5a7272dff3f100393987a1cf3da2
Diffstat (limited to 'javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes')
38 files changed, 2515 insertions, 0 deletions
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithAnnotations.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithAnnotations.java new file mode 100644 index 000000000..72060c612 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithAnnotations.java @@ -0,0 +1,214 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.*; + +import java.lang.annotation.Annotation; +import java.util.Optional; + +import static com.github.javaparser.JavaParser.parseExpression; +import static com.github.javaparser.JavaParser.parseName; + +/** + * A node that can be annotated. + * + * @author Federico Tomassetti + * @since July 2014 + */ +public interface NodeWithAnnotations<N extends Node> { + NodeList<AnnotationExpr> getAnnotations(); + + N setAnnotations(NodeList<AnnotationExpr> annotations); + + void tryAddImportToParentCompilationUnit(Class<?> clazz); + + default AnnotationExpr getAnnotation(int i) { + return getAnnotations().get(i); + } + + @SuppressWarnings("unchecked") + default N setAnnotation(int i, AnnotationExpr element) { + getAnnotations().set(i, element); + return (N) this; + } + + @SuppressWarnings("unchecked") + default N addAnnotation(AnnotationExpr element) { + getAnnotations().add(element); + return (N) this; + } + + /** + * Annotates this + * + * @param name the name of the annotation + * @return this + */ + @SuppressWarnings("unchecked") + default N addAnnotation(String name) { + NormalAnnotationExpr annotation = new NormalAnnotationExpr( + parseName(name), new NodeList<>()); + getAnnotations().add(annotation); + return (N) this; + } + + /** + * Annotates this + * + * @param name the name of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + @SuppressWarnings("unchecked") + default NormalAnnotationExpr addAndGetAnnotation(String name) { + NormalAnnotationExpr annotation = new NormalAnnotationExpr( + parseName(name), new NodeList<>()); + getAnnotations().add(annotation); + return annotation; + } + + /** + * Annotates this node and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + default N addAnnotation(Class<? extends Annotation> clazz) { + tryAddImportToParentCompilationUnit(clazz); + return addAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this node and automatically add the import + * + * @param clazz the class of the annotation + * @return the {@link NormalAnnotationExpr} added + */ + default NormalAnnotationExpr addAndGetAnnotation(Class<? extends Annotation> clazz) { + tryAddImportToParentCompilationUnit(clazz); + return addAndGetAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a marker annotation + * + * @param name the name of the annotation + * @return this + */ + @SuppressWarnings("unchecked") + default N addMarkerAnnotation(String name) { + MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr( + parseName(name)); + getAnnotations().add(markerAnnotationExpr); + return (N) this; + } + + /** + * Annotates this with a marker annotation and automatically add the import + * + * @param clazz the class of the annotation + * @return this + */ + default N addMarkerAnnotation(Class<? extends Annotation> clazz) { + tryAddImportToParentCompilationUnit(clazz); + return addMarkerAnnotation(clazz.getSimpleName()); + } + + /** + * Annotates this with a single member annotation + * + * @param name the name of the annotation + * @param expression the part between () + * @return this + */ + @SuppressWarnings("unchecked") + default N addSingleMemberAnnotation(String name, Expression expression) { + SingleMemberAnnotationExpr singleMemberAnnotationExpr = new SingleMemberAnnotationExpr( + parseName(name), expression); + getAnnotations().add(singleMemberAnnotationExpr); + return (N) this; + } + + /** + * Annotates this with a single member annotation + * + * @param name the name of the annotation + * @param value the value, don't forget to add \"\" for a string value + * @return this + */ + default N addSingleMemberAnnotation(String name, String value) { + return addSingleMemberAnnotation(name, parseExpression(value)); + } + + /** + * Annotates this with a single member annotation and automatically add the import + * + * @param clazz the class of the annotation + * @param value the value, don't forget to add \"\" for a string value + * @return this + */ + default N addSingleMemberAnnotation(Class<? extends Annotation> clazz, + String value) { + tryAddImportToParentCompilationUnit(clazz); + return addSingleMemberAnnotation(clazz.getSimpleName(), value); + } + + /** + * Check whether an annotation with this name is present on this element + * + * @param annotationName the name of the annotation + * @return true if found, false if not + */ + default boolean isAnnotationPresent(String annotationName) { + return getAnnotations().stream().anyMatch(a -> a.getName().getIdentifier().equals(annotationName)); + } + + /** + * Check whether an annotation with this class is present on this element + * + * @param annotationClass the class of the annotation + * @return true if found, false if not + */ + default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { + return isAnnotationPresent(annotationClass.getSimpleName()); + } + + /** + * Try to find an annotation by its name + * + * @param annotationName the name of the annotation + */ + default Optional<AnnotationExpr> getAnnotationByName(String annotationName) { + return getAnnotations().stream().filter(a -> a.getName().getIdentifier().equals(annotationName)).findFirst(); + } + + /** + * Try to find an annotation by its class + * + * @param annotationClass the class of the annotation + */ + default Optional<AnnotationExpr> getAnnotationByClass(Class<? extends Annotation> annotationClass) { + return getAnnotationByName(annotationClass.getSimpleName()); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithArguments.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithArguments.java new file mode 100644 index 000000000..702707ca1 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithArguments.java @@ -0,0 +1,59 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.expr.Expression; + +import static com.github.javaparser.JavaParser.parseExpression; + +/** + * A node with arguments. + */ +public interface NodeWithArguments<N extends Node> { + N setArguments(NodeList<Expression> arguments); + + NodeList<Expression> getArguments(); + + default Expression getArgument(int i) { + return getArguments().get(i); + } + + @SuppressWarnings("unchecked") + default N addArgument(String arg) { + return addArgument(parseExpression(arg)); + } + + @SuppressWarnings("unchecked") + default N addArgument(Expression arg) { + getArguments().add(arg); + return (N) this; + } + + @SuppressWarnings("unchecked") + default N setArgument(int i, Expression arg) { + getArguments().set(i, arg); + return (N) this; + } + +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithBlockStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithBlockStmt.java new file mode 100644 index 000000000..a23e3e266 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithBlockStmt.java @@ -0,0 +1,40 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.stmt.BlockStmt;
+
+/**
+ * A node with a body that is a BlockStmt.
+ */
+public interface NodeWithBlockStmt<N extends Node> {
+ BlockStmt getBody();
+
+ N setBody(BlockStmt block);
+
+ default BlockStmt createBody() {
+ BlockStmt block = new BlockStmt();
+ setBody(block);
+ return block;
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithBody.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithBody.java new file mode 100644 index 000000000..2388824af --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithBody.java @@ -0,0 +1,38 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.stmt.BlockStmt;
+import com.github.javaparser.ast.stmt.Statement;
+
+public interface NodeWithBody<N extends Node> {
+ Statement getBody();
+
+ N setBody(final Statement body);
+
+ default BlockStmt createBlockStatementAsBody() {
+ BlockStmt b = new BlockStmt();
+ setBody(b);
+ return b;
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithCondition.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithCondition.java new file mode 100644 index 000000000..4113f8b69 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithCondition.java @@ -0,0 +1,10 @@ +package com.github.javaparser.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.Expression; + +public interface NodeWithCondition<N extends Node> { + Expression getCondition(); + + N setCondition(Expression condition); +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithConstructors.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithConstructors.java new file mode 100644 index 000000000..b2f2507ee --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithConstructors.java @@ -0,0 +1,71 @@ +package com.github.javaparser.ast.nodeTypes; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.body.ConstructorDeclaration; + +import java.util.Arrays; +import java.util.EnumSet; +import java.util.List; +import java.util.Optional; + +import static java.util.Collections.unmodifiableList; +import static java.util.stream.Collectors.toCollection; +import static java.util.stream.Collectors.toList; + +public interface NodeWithConstructors<N extends Node> extends NodeWithSimpleName<N>, NodeWithMembers<N> { + /** + * Try to find a {@link ConstructorDeclaration} with no parameters by its name + * + * @return the constructors found (multiple in case of polymorphism) + */ + default Optional<ConstructorDeclaration> getDefaultConstructor() { + return getMembers().stream().filter(bd -> bd instanceof ConstructorDeclaration).map(bd -> (ConstructorDeclaration) bd).filter(cd -> cd.getParameters().isEmpty()).findFirst(); + } + + /** + * Adds a constructor to this + * + * @param modifiers the modifiers like {@link Modifier#PUBLIC} + * @return the created constructor + */ + default ConstructorDeclaration addConstructor(Modifier... modifiers) { + ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(); + constructorDeclaration.setModifiers(Arrays.stream(modifiers).collect(toCollection(() -> EnumSet.noneOf(Modifier.class)))); + constructorDeclaration.setName(getName()); + getMembers().add(constructorDeclaration); + return constructorDeclaration; + } + + /** + * Find all constructors for this class. + * + * @return the constructors found. This list is immutable. + */ + default List<ConstructorDeclaration> getConstructors() { + return unmodifiableList(getMembers().stream().filter(m -> m instanceof ConstructorDeclaration).map(m -> (ConstructorDeclaration) m).collect(toList())); + } + + /** + * Try to find a {@link ConstructorDeclaration} by its parameters types + * + * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void + * foo(Map<Integer,String> myMap,int number) + * @return the constructor found (multiple in case of overloading) + */ + default Optional<ConstructorDeclaration> getConstructorByParameterTypes(String... paramTypes) { + return getConstructors().stream().filter(m -> m.hasParametersOfType(paramTypes)).findFirst(); + } + + /** + * Try to find a {@link ConstructorDeclaration} by its parameters types + * + * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void + * foo(Map<Integer,String> myMap,int number) + * @return the constructors found (multiple in case of overloading) + */ + default Optional<ConstructorDeclaration> getConstructorByParameterTypes(Class<?>... paramTypes) { + return getConstructors().stream().filter(m -> m.hasParametersOfType(paramTypes)).findFirst(); + } + +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithDeclaration.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithDeclaration.java new file mode 100644 index 000000000..d490ed158 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithDeclaration.java @@ -0,0 +1,61 @@ +/* + * 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.ast.nodeTypes; + +/** + * Node with a declaration representable as a String. + * + * @author Federico Tomassetti + * @since July 2014 + */ +public interface NodeWithDeclaration { + + /** + * As {@link NodeWithDeclaration#getDeclarationAsString(boolean, boolean, boolean)} including + * the modifiers, the throws clause and the parameters with both type and name. + * + * @return String representation of declaration + */ + String getDeclarationAsString(); + + /** + * As {@link NodeWithDeclaration#getDeclarationAsString(boolean, boolean, boolean)} including + * the parameters with both type and name. + * + * @param includingModifiers flag to include the modifiers (if present) in the string produced + * @param includingThrows flag to include the throws clause (if present) in the string produced + * @return String representation of declaration based on parameter flags + */ + String getDeclarationAsString(boolean includingModifiers, boolean includingThrows); + + /** + * A simple representation of the element declaration. + * It should fit one string. + * + * @param includingModifiers flag to include the modifiers (if present) in the string produced + * @param includingThrows flag to include the throws clause (if present) in the string produced + * @param includingParameterName flag to include the parameter name (while the parameter type is always included) in + * the string produced + * @return String representation of declaration based on parameter flags + */ + String getDeclarationAsString(boolean includingModifiers, boolean includingThrows, boolean includingParameterName); +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithExpression.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithExpression.java new file mode 100644 index 000000000..3a1dd712f --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithExpression.java @@ -0,0 +1,40 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.Expression; + +import static com.github.javaparser.JavaParser.parseExpression; + +/** + * A node that has an expression in it. + */ +public interface NodeWithExpression<N extends Node> { + Expression getExpression(); + + N setExpression(Expression expression); + + default N setExpression(String expression) { + return setExpression(parseExpression(expression)); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithExtends.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithExtends.java new file mode 100644 index 000000000..af4adf800 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithExtends.java @@ -0,0 +1,92 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.type.ClassOrInterfaceType;
+
+import static com.github.javaparser.JavaParser.parseClassOrInterfaceType;
+
+/**
+ * A node that extends other types.
+ */
+public interface NodeWithExtends<N extends Node> {
+ NodeList<ClassOrInterfaceType> getExtendedTypes();
+
+ void tryAddImportToParentCompilationUnit(Class<?> clazz);
+
+ default ClassOrInterfaceType getExtendedTypes(int i) {
+ return getExtendedTypes().get(i);
+ }
+
+ N setExtendedTypes(NodeList<ClassOrInterfaceType> extendsList);
+
+ @SuppressWarnings("unchecked")
+ default N setExtendedType(int i, ClassOrInterfaceType extend) {
+ getExtendedTypes().set(i, extend);
+ return (N) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ default N addExtendedType(ClassOrInterfaceType extend) {
+ getExtendedTypes().add(extend);
+ return (N) this;
+ }
+
+ /**
+ * @deprecated use addExtendedType
+ */
+ default N addExtends(Class<?> clazz) {
+ return addExtendedType(clazz);
+ }
+
+ /**
+ * @deprecated use addExtendedType
+ */
+ default N addExtends(String name) {
+ return addExtendedType(name);
+ }
+
+ /**
+ * Add an "extends" to this and automatically add the import
+ *
+ * @param clazz the class to extand from
+ * @return this
+ */
+ default N addExtendedType(Class<?> clazz) {
+ tryAddImportToParentCompilationUnit(clazz);
+ return addExtendedType(clazz.getSimpleName());
+ }
+
+ /**
+ * Add an "extends" to this
+ *
+ * @param name the name of the type to extends from
+ * @return this
+ */
+ @SuppressWarnings("unchecked")
+ default N addExtendedType(String name) {
+ getExtendedTypes().add(parseClassOrInterfaceType(name));
+ return (N) this;
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithIdentifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithIdentifier.java new file mode 100644 index 000000000..22a583a44 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithIdentifier.java @@ -0,0 +1,41 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; + +import static com.github.javaparser.utils.Utils.assertNonEmpty; + +public interface NodeWithIdentifier<N extends Node> { + String getIdentifier(); + + N setIdentifier(String identifier); + + default String getId() { + return getIdentifier(); + } + + default N setId(String identifier) { + assertNonEmpty(identifier); + return setIdentifier(identifier); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithImplements.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithImplements.java new file mode 100644 index 000000000..84cee4d32 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithImplements.java @@ -0,0 +1,88 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.type.ClassOrInterfaceType;
+
+import static com.github.javaparser.JavaParser.parseClassOrInterfaceType;
+
+/**
+ * A node that implements other types.
+ */
+public interface NodeWithImplements<N extends Node> {
+ NodeList<ClassOrInterfaceType> getImplementedTypes();
+
+ default ClassOrInterfaceType getImplementedTypes(int i) {
+ return getImplementedTypes().get(i);
+ }
+
+ N setImplementedTypes(NodeList<ClassOrInterfaceType> implementsList);
+
+ void tryAddImportToParentCompilationUnit(Class<?> clazz);
+
+ @SuppressWarnings("unchecked")
+ default N setImplementedType(int i, ClassOrInterfaceType implement) {
+ getImplementedTypes().set(i, implement);
+ return (N) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ default N addImplementedType(ClassOrInterfaceType implement) {
+ getImplementedTypes().add(implement);
+ return (N) this;
+ }
+
+ /** @deprecated use addImplementedType instead */
+ default N addImplements(String name) {
+ return addImplementedType(name);
+ }
+
+ /** @deprecated use addImplementedType instead */
+ default N addImplements(Class<?> clazz) {
+ return addImplementedType(clazz);
+ }
+
+ /**
+ * Add an implements to this
+ *
+ * @param name the name of the type to extends from
+ * @return this
+ */
+ @SuppressWarnings("unchecked")
+ default N addImplementedType(String name) {
+ getImplementedTypes().add(parseClassOrInterfaceType(name));
+ return (N) this;
+ }
+
+ /**
+ * Add an implements to this and automatically add the import
+ *
+ * @param clazz the type to implements from
+ * @return this
+ */
+ default N addImplementedType(Class<?> clazz) {
+ tryAddImportToParentCompilationUnit(clazz);
+ return addImplementedType(clazz.getSimpleName());
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithJavadoc.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithJavadoc.java new file mode 100644 index 000000000..592947e07 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithJavadoc.java @@ -0,0 +1,91 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.comments.Comment; +import com.github.javaparser.ast.comments.JavadocComment; +import com.github.javaparser.javadoc.Javadoc; + +import java.util.Optional; + +/** + * A node that can be documented with a Javadoc comment. + */ +public interface NodeWithJavadoc<N extends Node> { + Optional<Comment> getComment(); + + Node setComment(Comment comment); + + /** + * Gets the JavadocComment for this node. You can set the JavadocComment by calling setJavadocComment passing a + * JavadocComment. + * + * @return The JavadocComment for this node wrapped in an optional as it may be absent. + */ + default Optional<JavadocComment> getJavadocComment() { + return getComment() + .filter(comment -> comment instanceof JavadocComment) + .map(comment -> (JavadocComment) comment); + } + + /** + * Gets the Javadoc for this node. You can set the Javadoc by calling setJavadocComment passing a Javadoc. + * + * @return The Javadoc for this node wrapped in an optional as it may be absent. + */ + default Optional<Javadoc> getJavadoc() { + return getJavadocComment().map(JavadocComment::parse); + } + + /** + * Use this to store additional information to this node. + * + * @param comment to be set + */ + @SuppressWarnings("unchecked") + default N setJavadocComment(String comment) { + return setJavadocComment(new JavadocComment(" " + comment)); + } + + default N setJavadocComment(JavadocComment comment) { + setComment(comment); + return (N) this; + } + + default N setJavadocComment(String indentation, Javadoc javadoc) { + return setJavadocComment(javadoc.toComment(indentation)); + } + + default N setJavadocComment(Javadoc javadoc) { + return setJavadocComment(javadoc.toComment()); + } + + default boolean removeJavaDocComment() { + return hasJavaDocComment() && getComment().get().remove(); + } + + default boolean hasJavaDocComment() { + return getComment().isPresent() && getComment().get() instanceof JavadocComment; + } + +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithMembers.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithMembers.java new file mode 100644 index 000000000..6d5f0c3ff --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithMembers.java @@ -0,0 +1,328 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Modifier;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.body.*;
+import com.github.javaparser.ast.expr.Expression;
+import com.github.javaparser.ast.stmt.BlockStmt;
+import com.github.javaparser.ast.type.Type;
+import com.github.javaparser.ast.type.VoidType;
+
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Optional;
+
+import static com.github.javaparser.JavaParser.parseType;
+import static java.util.Collections.unmodifiableList;
+import static java.util.stream.Collectors.toCollection;
+import static java.util.stream.Collectors.toList;
+
+/**
+ * A node having members.
+ * <p>
+ * The main reason for this interface is to permit users to manipulate homogeneously all nodes with a getMembers
+ * method.
+ */
+public interface NodeWithMembers<N extends Node> {
+ /**
+ * @return all members inside the braces of this node,
+ * like fields, methods, nested types, etc.
+ */
+ NodeList<BodyDeclaration<?>> getMembers();
+
+ void tryAddImportToParentCompilationUnit(Class<?> clazz);
+
+ default BodyDeclaration<?> getMember(int i) {
+ return getMembers().get(i);
+ }
+
+ @SuppressWarnings("unchecked")
+ default N setMember(int i, BodyDeclaration<?> member) {
+ getMembers().set(i, member);
+ return (N) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ default N addMember(BodyDeclaration<?> member) {
+ getMembers().add(member);
+ return (N) this;
+ }
+
+ N setMembers(NodeList<BodyDeclaration<?>> members);
+
+ /**
+ * Add a field to this and automatically add the import of the type if needed
+ *
+ * @param typeClass the type of the field
+ * @param name the name of the field
+ * @param modifiers the modifiers like {@link Modifier#PUBLIC}
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addField(Class<?> typeClass, String name, Modifier... modifiers) {
+ tryAddImportToParentCompilationUnit(typeClass);
+ return addField(typeClass.getSimpleName(), name, modifiers);
+ }
+
+ /**
+ * Add a field to this.
+ *
+ * @param type the type of the field
+ * @param name the name of the field
+ * @param modifiers the modifiers like {@link Modifier#PUBLIC}
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addField(String type, String name, Modifier... modifiers) {
+ return addField(parseType(type), name, modifiers);
+ }
+
+ /**
+ * Add a field to this.
+ *
+ * @param type the type of the field
+ * @param name the name of the field
+ * @param modifiers the modifiers like {@link Modifier#PUBLIC}
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addField(Type type, String name, Modifier... modifiers) {
+ FieldDeclaration fieldDeclaration = new FieldDeclaration();
+ VariableDeclarator variable = new VariableDeclarator(type, name);
+ fieldDeclaration.getVariables().add(variable);
+ fieldDeclaration.setModifiers(Arrays.stream(modifiers)
+ .collect(toCollection(() -> EnumSet.noneOf(Modifier.class))));
+ getMembers().add(fieldDeclaration);
+ return fieldDeclaration;
+ }
+
+ /**
+ * Add a field to this.
+ *
+ * @param type the type of the field
+ * @param name the name of the field
+ * @param initializer the initializer of the field
+ * @param modifiers the modifiers like {@link Modifier#PUBLIC}
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addFieldWithInitializer(Type type, String name, Expression initializer, Modifier... modifiers) {
+ FieldDeclaration declaration = addField(type, name, modifiers);
+ declaration.getVariables().iterator().next().setInitializer(initializer);
+ return declaration;
+ }
+
+ /**
+ * Add a private field to this.
+ *
+ * @param typeClass the type of the field
+ * @param name the name of the field
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addPrivateField(Class<?> typeClass, String name) {
+ return addField(typeClass, name, Modifier.PRIVATE);
+ }
+
+ /**
+ * Add a private field to this 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
+ */
+ default FieldDeclaration addPrivateField(String type, String name) {
+ return addField(type, name, Modifier.PRIVATE);
+ }
+
+ /**
+ * Add a public field to this.
+ *
+ * @param typeClass the type of the field
+ * @param name the name of the field
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addPublicField(Class<?> typeClass, String name) {
+ return addField(typeClass, name, Modifier.PUBLIC);
+ }
+
+ /**
+ * Add a public field to this 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
+ */
+ default FieldDeclaration addPublicField(String type, String name) {
+ return addField(type, name, Modifier.PUBLIC);
+ }
+
+ /**
+ * Add a protected field to this.
+ *
+ * @param typeClass the type of the field
+ * @param name the name of the field
+ * @return the {@link FieldDeclaration} created
+ */
+ default FieldDeclaration addProtectedField(Class<?> typeClass, String name) {
+ return addField(typeClass, name, Modifier.PROTECTED);
+ }
+
+ /**
+ * Add a protected field to this 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
+ */
+ default FieldDeclaration addProtectedField(String type, String name) {
+ return addField(type, name, Modifier.PROTECTED);
+ }
+
+ /**
+ * Adds a methods with void return by default to this.
+ *
+ * @param methodName the method name
+ * @param modifiers the modifiers like {@link Modifier#PUBLIC}
+ * @return the {@link MethodDeclaration} created
+ */
+ default MethodDeclaration addMethod(String methodName, Modifier... modifiers) {
+ MethodDeclaration methodDeclaration = new MethodDeclaration();
+ methodDeclaration.setName(methodName);
+ methodDeclaration.setType(new VoidType());
+ methodDeclaration.setModifiers(Arrays.stream(modifiers)
+ .collect(toCollection(() -> EnumSet.noneOf(Modifier.class))));
+ getMembers().add(methodDeclaration);
+ return methodDeclaration;
+ }
+
+ /**
+ * Add an initializer block ({@link InitializerDeclaration}) to this.
+ */
+ default BlockStmt addInitializer() {
+ BlockStmt block = new BlockStmt();
+ InitializerDeclaration initializerDeclaration = new InitializerDeclaration(false, block);
+ getMembers().add(initializerDeclaration);
+ return block;
+ }
+
+ /**
+ * Add a static initializer block ({@link InitializerDeclaration}) to this.
+ */
+ default BlockStmt addStaticInitializer() {
+ BlockStmt block = new BlockStmt();
+ InitializerDeclaration initializerDeclaration = new InitializerDeclaration(true, block);
+ getMembers().add(initializerDeclaration);
+ return block;
+ }
+
+ /**
+ * Try to find a {@link MethodDeclaration} by its name
+ *
+ * @param name the name of the method
+ * @return the methods found (multiple in case of overloading)
+ */
+ default List<MethodDeclaration> getMethodsByName(String name) {
+ return unmodifiableList(getMethods().stream()
+ .filter(m -> m.getNameAsString().equals(name))
+ .collect(toList()));
+ }
+
+ /**
+ * Find all methods in the members of this node.
+ *
+ * @return the methods found. This list is immutable.
+ */
+ default List<MethodDeclaration> getMethods() {
+ return unmodifiableList(getMembers().stream()
+ .filter(m -> m instanceof MethodDeclaration)
+ .map(m -> (MethodDeclaration) m)
+ .collect(toList()));
+ }
+
+ /**
+ * Try to find a {@link MethodDeclaration} by its parameters types
+ *
+ * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
+ * foo(Map<Integer,String> myMap,int number)
+ * @return the methods found (multiple in case of overloading)
+ */
+ default List<MethodDeclaration> getMethodsByParameterTypes(String... paramTypes) {
+ return unmodifiableList(getMethods().stream()
+ .filter(m -> m.hasParametersOfType(paramTypes))
+ .collect(toList()));
+ }
+
+ /**
+ * Try to find {@link MethodDeclaration}s by their name and parameters types
+ *
+ * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
+ * foo(Map<Integer,String> myMap,int number)
+ * @return the methods found (multiple in case of overloading)
+ */
+ default List<MethodDeclaration> getMethodsBySignature(String name, String... paramTypes) {
+ return unmodifiableList(getMethodsByName(name).stream()
+ .filter(m -> m.hasParametersOfType(paramTypes))
+ .collect(toList()));
+ }
+
+ /**
+ * Try to find a {@link MethodDeclaration} by its parameters types
+ *
+ * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
+ * foo(Map<Integer,String> myMap,int number)
+ * @return the methods found (multiple in case of overloading)
+ */
+ default List<MethodDeclaration> getMethodsByParameterTypes(Class<?>... paramTypes) {
+ return unmodifiableList(getMethods().stream()
+ .filter(m -> m.hasParametersOfType(paramTypes))
+ .collect(toList()));
+ }
+
+ /**
+ * Try to find a {@link FieldDeclaration} by its name
+ *
+ * @param name the name of the field
+ * @return null if not found, the FieldDeclaration otherwise
+ */
+ default Optional<FieldDeclaration> getFieldByName(String name) {
+ return getMembers().stream()
+ .filter(m -> m instanceof FieldDeclaration)
+ .map(f -> (FieldDeclaration) f)
+ .filter(f -> f.getVariables().stream()
+ .anyMatch(var -> var.getNameAsString().equals(name)))
+ .findFirst();
+ }
+
+ /**
+ * Find all fields in the members of this node.
+ *
+ * @return the fields found. This list is immutable.
+ */
+ default List<FieldDeclaration> getFields() {
+ return unmodifiableList(getMembers().stream()
+ .filter(m -> m instanceof FieldDeclaration)
+ .map(m -> (FieldDeclaration) m)
+ .collect(toList()));
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithModifiers.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithModifiers.java new file mode 100644 index 000000000..3a499bfa3 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithModifiers.java @@ -0,0 +1,73 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Modifier; +import com.github.javaparser.ast.Node; + +import java.util.Arrays; +import java.util.EnumSet; +import java.util.stream.Collectors; + +/** + * A Node with Modifiers. + * Note that not all modifiers may be valid for this node. + */ +public interface NodeWithModifiers<N extends Node> { + /** + * Return the modifiers of this variable declaration. + * Warning: modifying the returned set will not trigger observers, + * you have to use setModifiers for that. + * + * @return modifiers + * @see Modifier + */ + EnumSet<Modifier> getModifiers(); + + N setModifiers(EnumSet<Modifier> modifiers); + + @SuppressWarnings("unchecked") + default N addModifier(Modifier... modifiers) { + EnumSet<Modifier> newModifiers = getModifiers().clone(); + newModifiers.addAll(Arrays.stream(modifiers) + .collect(Collectors.toCollection(() -> EnumSet.noneOf(Modifier.class)))); + setModifiers(newModifiers); + return (N) this; + } + + @SuppressWarnings("unchecked") + default N removeModifier(Modifier... m) { + EnumSet<Modifier> newModifiers = getModifiers().clone(); + newModifiers.removeAll(Arrays.stream(m) + .collect(Collectors.toCollection(() -> EnumSet.noneOf(Modifier.class)))); + setModifiers(newModifiers); + return (N) this; + } + default N setModifier(Modifier m, boolean set) { + if (set) { + return addModifier(m); + } else { + return removeModifier(m); + } + } + +}
\ No newline at end of file diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithName.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithName.java new file mode 100644 index 000000000..bc25cad05 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithName.java @@ -0,0 +1,51 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.Name; + +import static com.github.javaparser.JavaParser.parseName; +import static com.github.javaparser.utils.Utils.assertNonEmpty; + +/** + * A node with a (qualified) name. + * <p> + * The main reason for this interface is to permit users to manipulate homogeneously all nodes with a getName method. + * + * @since 2.0.1 + */ +public interface NodeWithName<N extends Node> { + Name getName(); + + N setName(Name name); + + @SuppressWarnings("unchecked") + default N setName(String name) { + assertNonEmpty(name); + return setName(parseName(name)); + } + + default String getNameAsString() { + return getName().asString(); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalBlockStmt.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalBlockStmt.java new file mode 100644 index 000000000..0bf89a4d2 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalBlockStmt.java @@ -0,0 +1,44 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.stmt.BlockStmt;
+
+import java.util.Optional;
+
+/**
+ * A node with a body that is a BlockStmt, which is optional.
+ */
+public interface NodeWithOptionalBlockStmt<N extends Node> {
+ Optional<BlockStmt> getBody();
+
+ N setBody(BlockStmt block);
+
+ N removeBody();
+
+ default BlockStmt createBody() {
+ BlockStmt block = new BlockStmt();
+ setBody(block);
+ return block;
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalLabel.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalLabel.java new file mode 100644 index 000000000..93ab11a5a --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalLabel.java @@ -0,0 +1,28 @@ +package com.github.javaparser.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.SimpleName; + +import java.util.Optional; + +import static com.github.javaparser.utils.Utils.assertNonEmpty; + +/** + * A node that has an optional label. + */ +public interface NodeWithOptionalLabel<T extends Node> { + Optional<SimpleName> getLabel(); + + T setLabel(SimpleName label); + + T removeLabel(); + + default T setLabel(String label) { + assertNonEmpty(label); + return setLabel(new SimpleName(label)); + } + + default Optional<String> getLabelAsString() { + return getLabel().flatMap(l -> Optional.of(l.getIdentifier())); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java new file mode 100644 index 000000000..46bd59c84 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithOptionalScope.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2017 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.Expression; + +import java.util.Optional; + +/** + * Represents a node which has an optional scope expression eg. method calls (object.method()). + */ +public interface NodeWithOptionalScope<N extends Node> extends NodeWithTraversableScope { + + Optional<Expression> getScope(); + + N setScope(Expression scope); + + N removeScope(); + + default Optional<Expression> traverseScope() { + return getScope(); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithParameters.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithParameters.java new file mode 100644 index 000000000..517d8f574 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithParameters.java @@ -0,0 +1,162 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.body.Parameter;
+import com.github.javaparser.ast.type.Type;
+
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import static com.github.javaparser.JavaParser.parseType;
+import static java.util.stream.Collectors.toSet;
+
+public interface NodeWithParameters<N extends Node> {
+ NodeList<Parameter> getParameters();
+
+ default Parameter getParameter(int i) {
+ return getParameters().get(i);
+ }
+
+ void tryAddImportToParentCompilationUnit(Class<?> clazz);
+
+ @SuppressWarnings("unchecked")
+ default N setParameter(int i, Parameter parameter) {
+ getParameters().set(i, parameter);
+ return (N) this;
+ }
+
+ N setParameters(NodeList<Parameter> parameters);
+
+ default N addParameter(Type type, String name) {
+ return addParameter(new Parameter(type, name));
+ }
+
+ default N addParameter(Class<?> paramClass, String name) {
+ tryAddImportToParentCompilationUnit(paramClass);
+ return addParameter(parseType(paramClass.getSimpleName()), name);
+ }
+
+ /**
+ * Remember to import the class in the compilation unit yourself
+ *
+ * @param className the name of the class, ex : org.test.Foo or Foo if you added manually the import
+ * @param name the name of the parameter
+ */
+ default N addParameter(String className, String name) {
+ return addParameter(parseType(className), name);
+ }
+
+ @SuppressWarnings("unchecked")
+ default N addParameter(Parameter parameter) {
+ getParameters().add(parameter);
+ return (N) this;
+ }
+
+ default Parameter addAndGetParameter(Type type, String name) {
+ return addAndGetParameter(new Parameter(type, name));
+ }
+
+ default Parameter addAndGetParameter(Class<?> paramClass, String name) {
+ tryAddImportToParentCompilationUnit(paramClass);
+ return addAndGetParameter(parseType(paramClass.getSimpleName()), name);
+ }
+
+ /**
+ * Remember to import the class in the compilation unit yourself
+ *
+ * @param className the name of the class, ex : org.test.Foo or Foo if you added manually the import
+ * @param name the name of the parameter
+ * @return the {@link Parameter} created
+ */
+ default Parameter addAndGetParameter(String className, String name) {
+ return addAndGetParameter(parseType(className), name);
+ }
+
+ default Parameter addAndGetParameter(Parameter parameter) {
+ getParameters().add(parameter);
+ return parameter;
+ }
+
+ /**
+ * Try to find a {@link Parameter} by its name
+ *
+ * @param name the name of the param
+ * @return null if not found, the param found otherwise
+ */
+ default Optional<Parameter> getParameterByName(String name) {
+ return getParameters().stream()
+ .filter(p -> p.getNameAsString().equals(name)).findFirst();
+ }
+
+ /**
+ * Try to find a {@link Parameter} by its type
+ *
+ * @param type the type of the param
+ * @return null if not found, the param found otherwise
+ */
+ default Optional<Parameter> getParameterByType(String type) {
+ return getParameters().stream()
+ .filter(p -> p.getType().toString().equals(type)).findFirst();
+ }
+
+ /**
+ * Try to find a {@link Parameter} by its type
+ *
+ * @param type the type of the param <b>take care about generics, it wont work</b>
+ * @return null if not found, the param found otherwise
+ */
+ default Optional<Parameter> getParameterByType(Class<?> type) {
+ return getParameters().stream()
+ .filter(p -> p.getType().toString().equals(type.getSimpleName())).findFirst();
+ }
+
+ /**
+ * Check if the parameters have certain types.
+ *
+ * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
+ * foo(Map<Integer,String> myMap,int number)
+ * @return true if all parameters match
+ */
+ default boolean hasParametersOfType(String... paramTypes) {
+ return getParameters().stream()
+ .map(p -> p.getType().toString())
+ .collect(toSet())
+ .equals(Stream.of(paramTypes).collect(toSet()));
+ }
+
+ /**
+ * Check if the parameters have certain types. Note that this is a match in SimpleName, so "java.awt.List" and
+ * "java.util.List" are identical to this algorithm.
+ *
+ * @param paramTypes the types of parameters like "Map<Integer,String>","int" to match<br> void
+ * foo(Map<Integer,String> myMap,int number)
+ * @return true if all parameters match
+ */
+ default boolean hasParametersOfType(Class<?>... paramTypes) {
+ return getParameters().stream().map(p -> p.getType().toString())
+ .collect(toSet())
+ .equals(Stream.of(paramTypes).map(Class::getSimpleName).collect(toSet()));
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithRange.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithRange.java new file mode 100644 index 000000000..d1e2fb767 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithRange.java @@ -0,0 +1,54 @@ +package com.github.javaparser.ast.nodeTypes; + +import com.github.javaparser.Position; +import com.github.javaparser.Range; +import com.github.javaparser.ast.Node; + +import java.util.Optional; + +/** + * A node that has a Range, which is every Node. + * + */ +public interface NodeWithRange<N> { + Optional<Range> getRange(); + + N setRange(Range range); + + /** + * The begin position of this node in the source file. + */ + default Optional<Position> getBegin() { + return getRange().map(r -> r.begin); + } + + /** + * The end position of this node in the source file. + */ + default Optional<Position> getEnd() { + return getRange().map(r -> r.end); + } + + default boolean containsWithin(Node other) { + if (getRange().isPresent() && other.getRange().isPresent()) { + return getRange().get().contains(other.getRange().get()); + } + return false; + } + + /** + * @deprecated use isAfter() on range + */ + @Deprecated + default boolean isPositionedAfter(Position position) { + return getRange().map(r -> r.isAfter(position)).orElse(false); + } + + /** + * @deprecated use isBefore() on range + */ + @Deprecated + default boolean isPositionedBefore(Position position) { + return getRange().map(r -> r.isBefore(position)).orElse(false); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java new file mode 100644 index 000000000..15ea3ac25 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithScope.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2017 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.Expression; + +import java.util.Optional; + +/** + * Represents a node which has a required scope expression eg. field access (object.method). + */ +public interface NodeWithScope<N extends Node> extends NodeWithTraversableScope { + + Expression getScope(); + + N setScope(Expression scope); + + default Optional<Expression> traverseScope() { + return Optional.of(getScope()); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithSimpleName.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithSimpleName.java new file mode 100644 index 000000000..ebd3957b8 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithSimpleName.java @@ -0,0 +1,53 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.expr.NameExpr; +import com.github.javaparser.ast.expr.SimpleName; + +import static com.github.javaparser.utils.Utils.assertNonEmpty; + +/** + * A node with a name. + * <p> + * The main reason for this interface is to permit users to manipulate homogeneously all nodes with a getName method. + */ +public interface NodeWithSimpleName<N extends Node> { + SimpleName getName(); + + N setName(SimpleName name); + + @SuppressWarnings("unchecked") + default N setName(String name) { + assertNonEmpty(name); + return setName(new SimpleName(name)); + } + + default String getNameAsString() { + return getName().getIdentifier(); + } + + default NameExpr getNameAsExpression() { + return new NameExpr(getName()); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithStatements.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithStatements.java new file mode 100644 index 000000000..4d2c90cec --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithStatements.java @@ -0,0 +1,114 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.JavaParser;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.NodeList;
+import com.github.javaparser.ast.expr.Expression;
+import com.github.javaparser.ast.expr.NameExpr;
+import com.github.javaparser.ast.stmt.ExpressionStmt;
+import com.github.javaparser.ast.stmt.Statement;
+
+import static com.github.javaparser.JavaParser.*;
+
+/**
+ * A node that contains a list of statements.
+ */
+public interface NodeWithStatements<N extends Node> {
+ NodeList<Statement> getStatements();
+
+ default Statement getStatement(int i) {
+ return getStatements().get(i);
+ }
+
+ @SuppressWarnings("unchecked")
+ default N setStatement(int i, Statement statement) {
+ getStatements().set(i, statement);
+ return (N) this;
+ }
+
+ N setStatements(final NodeList<Statement> statements);
+
+ @SuppressWarnings("unchecked")
+ default N addStatement(Statement statement) {
+ getStatements().add(statement);
+ return (N) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ default N addStatement(int index, final Statement statement) {
+ getStatements().add(index, statement);
+ return (N) this;
+ }
+
+ default N addStatement(Expression expr) {
+ return addStatement(new ExpressionStmt(expr));
+ }
+
+ /**
+ * It will use {@link JavaParser#parseStatement(String)} inside, so it should end with a semi column
+ */
+ default N addStatement(String statement) {
+ return addStatement(parseStatement(statement));
+ }
+
+ default N addStatement(int index, final Expression expr) {
+ Statement stmt = new ExpressionStmt(expr);
+ return addStatement(index, stmt);
+ }
+
+ default <A extends Statement> A addAndGetStatement(A statement) {
+ getStatements().add(statement);
+ return statement;
+ }
+
+ default Statement addAndGetStatement(int index, final Statement statement) {
+ getStatements().add(index, statement);
+ return statement;
+ }
+
+ default ExpressionStmt addAndGetStatement(Expression expr) {
+ ExpressionStmt statement = new ExpressionStmt(expr);
+ return addAndGetStatement(statement);
+ }
+
+ default ExpressionStmt addAndGetStatement(String statement) {
+ return addAndGetStatement(new NameExpr(statement));
+ }
+
+ default boolean isEmpty() {
+ return getStatements().isEmpty();
+ }
+
+ @SuppressWarnings("unchecked")
+ default N copyStatements(NodeList<Statement> nodeList) {
+ for (Statement n : nodeList) {
+ addStatement(n.clone());
+ }
+ return (N) this;
+ }
+
+ default N copyStatements(NodeWithStatements<?> other) {
+ return copyStatements(other.getStatements());
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithThrownExceptions.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithThrownExceptions.java new file mode 100644 index 000000000..e1b9007f6 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithThrownExceptions.java @@ -0,0 +1,90 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.type.ReferenceType; + +import static com.github.javaparser.JavaParser.parseClassOrInterfaceType; + +/** + * A node that declares the types of exception it throws. + */ +public interface NodeWithThrownExceptions<N extends Node> { + N setThrownExceptions(NodeList<ReferenceType> thrownExceptions); + + NodeList<ReferenceType> getThrownExceptions(); + + void tryAddImportToParentCompilationUnit(Class<?> clazz); + + default ReferenceType getThrownException(int i) { + return getThrownExceptions().get(i); + } + + /** + * Adds this type to the throws clause + * + * @param throwType the exception type + * @return this + */ + @SuppressWarnings("unchecked") + default N addThrownException(ReferenceType throwType) { + getThrownExceptions().add(throwType); + return (N) this; + } + + /** + * Adds this class to the throws clause + * + * @param clazz the exception class + * @return this + */ + default N addThrownException(Class<? extends Throwable> clazz) { + tryAddImportToParentCompilationUnit(clazz); + return addThrownException(parseClassOrInterfaceType(clazz.getSimpleName())); + } + + /** + * Check whether this elements throws this exception class. + * Note that this is simply a text compare of the simple name of the class, + * no actual type resolution takes place. + * + * @param clazz the class of the exception + * @return true if found in throws clause, false if not + */ + default boolean isThrown(Class<? extends Throwable> clazz) { + return isThrown(clazz.getSimpleName()); + } + + /** + * Check whether this elements throws this exception class + * Note that this is simply a text compare, + * no actual type resolution takes place. + * + * @param throwableName the class of the exception + * @return true if found in throws clause, false if not + */ + default boolean isThrown(String throwableName) { + return getThrownExceptions().stream().anyMatch(t -> t.toString().equals(throwableName)); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTokenRange.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTokenRange.java new file mode 100644 index 000000000..b043f6426 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTokenRange.java @@ -0,0 +1,15 @@ +package com.github.javaparser.ast.nodeTypes; + +import com.github.javaparser.TokenRange; + +import java.util.Optional; + +/** + * A node that has a Range, which is every Node. + * + */ +public interface NodeWithTokenRange<N> { + Optional<TokenRange> getTokenRange(); + + N setTokenRange(TokenRange range); +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java new file mode 100644 index 000000000..a1b9b3be5 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTraversableScope.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2007-2010 Júlio Vilmar Gesser. + * Copyright (C) 2011, 2013-2017 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.ast.nodeTypes; + +import com.github.javaparser.ast.expr.Expression; + +import java.util.Optional; + +/** + * Represents a node which has a scope expression that can be traversed/walked. + * This unifies scope access for NodeWithScope and NodeWithOptionalScope. + */ +public interface NodeWithTraversableScope { + + /** + * @return the scope of this node, regardless of optionality. + * An optional scope is returned directly. + * A required scope is returned in an "Optional", but will never be empty. + */ + Optional<Expression> traverseScope(); +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithType.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithType.java new file mode 100644 index 000000000..06e66fb91 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithType.java @@ -0,0 +1,78 @@ +/*
+ * 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.ast.nodeTypes;
+
+import com.github.javaparser.ast.CompilationUnit;
+import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.type.Type;
+
+import static com.github.javaparser.JavaParser.parseType;
+import static com.github.javaparser.utils.Utils.assertNonEmpty;
+
+/**
+ * A node with a type.
+ * <p>
+ * The main reason for this interface is to permit users to manipulate homogeneously all nodes with getType/setType
+ * methods
+ *
+ * @since 2.3.1
+ */
+public interface NodeWithType<N extends Node, T extends Type> {
+ /**
+ * Gets the type
+ *
+ * @return the type
+ */
+ T getType();
+
+ /**
+ * Sets the type
+ *
+ * @param type the type
+ * @return this
+ */
+ N setType(T type);
+
+ void tryAddImportToParentCompilationUnit(Class<?> clazz);
+
+ /**
+ * Sets this type to this class and try to import it to the {@link CompilationUnit} if needed
+ *
+ * @param typeClass the type
+ * @return this
+ */
+ @SuppressWarnings("unchecked")
+ default N setType(Class<?> typeClass) {
+ tryAddImportToParentCompilationUnit(typeClass);
+ return setType((T) parseType(typeClass.getSimpleName()));
+ }
+
+ @SuppressWarnings("unchecked")
+ default N setType(final String typeString) {
+ assertNonEmpty(typeString);
+ return setType((T) parseType(typeString));
+ }
+
+ default String getTypeAsString() {
+ return getType().asString();
+ }
+}
diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTypeArguments.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTypeArguments.java new file mode 100644 index 000000000..0c72b225e --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTypeArguments.java @@ -0,0 +1,83 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.type.Type; +import com.github.javaparser.metamodel.DerivedProperty; + +import java.util.Optional; + +import static com.github.javaparser.ast.NodeList.nodeList; + +/** + * A node that can have type arguments. + * <p> + * <pre> + * new X(); --> typeArguments == Optional is empty + * new X<>(); --> typeArguments = [], diamondOperator = true + * new X<C,D>(); --> typeArguments = [C,D], diamondOperator = false + * </pre> + */ +public interface NodeWithTypeArguments<N extends Node> { + /** + * @return the types that can be found in the type arguments: <String, Integer>. + */ + Optional<NodeList<Type>> getTypeArguments(); + + /** + * Allows you to set the generic arguments + * + * @param typeArguments The list of types of the generics, can be null + */ + N setTypeArguments(NodeList<Type> typeArguments); + + /** + * @return whether the type arguments look like <>. + */ + @DerivedProperty + default boolean isUsingDiamondOperator() { + return getTypeArguments().isPresent() && getTypeArguments().get().isEmpty(); + } + + /** + * Sets the type arguments to <>. + */ + @SuppressWarnings("unchecked") + default N setDiamondOperator() { + return setTypeArguments(new NodeList<>()); + } + + /** + * Removes all type arguments, including the surrounding <>. + */ + @SuppressWarnings("unchecked") + default N removeTypeArguments() { + return setTypeArguments((NodeList<Type>) null); + } + + @SuppressWarnings("unchecked") + default N setTypeArguments(Type... typeArguments) { + return setTypeArguments(nodeList(typeArguments)); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTypeParameters.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTypeParameters.java new file mode 100644 index 000000000..8817a7620 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithTypeParameters.java @@ -0,0 +1,60 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.type.TypeParameter; + +/** + * A node that can have type parameters. + * <pre> + * class X {} --> typeParameters == [] + * class X<> {} --> does not occur. + * class X<C,D> {} --> typeParameters = [C,D] + * </pre> + */ +public interface NodeWithTypeParameters<N extends Node> { + NodeList<TypeParameter> getTypeParameters(); + + default TypeParameter getTypeParameter(int i) { + return getTypeParameters().get(i); + } + + @SuppressWarnings("unchecked") + default N setTypeParameter(int i, TypeParameter typeParameter) { + getTypeParameters().set(i, typeParameter); + return (N) this; + } + + @SuppressWarnings("unchecked") + default N addTypeParameter(TypeParameter typeParameter) { + getTypeParameters().add(typeParameter); + return (N) this; + } + + N setTypeParameters(NodeList<TypeParameter> typeParameters); + + default boolean isGeneric() { + return getTypeParameters().size() > 0; + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithVariables.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithVariables.java new file mode 100644 index 000000000..5daf38a14 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithVariables.java @@ -0,0 +1,161 @@ +/* + * 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.ast.nodeTypes; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.NodeList; +import com.github.javaparser.ast.body.VariableDeclarator; +import com.github.javaparser.ast.type.ArrayType; +import com.github.javaparser.ast.type.Type; +import com.github.javaparser.metamodel.DerivedProperty; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * A node which has a list of variables. + */ +public interface NodeWithVariables<N extends Node> { + NodeList<VariableDeclarator> getVariables(); + + N setVariables(NodeList<VariableDeclarator> variables); + + default VariableDeclarator getVariable(int i) { + return getVariables().get(i); + } + + @SuppressWarnings("unchecked") + default N setVariable(int i, VariableDeclarator variableDeclarator) { + getVariables().set(i, variableDeclarator); + return (N) this; + } + + @SuppressWarnings("unchecked") + default N addVariable(VariableDeclarator variableDeclarator) { + getVariables().add(variableDeclarator); + return (N) this; + } + + /** + * Returns the type that is shared between all variables. + * This is a shortcut for when you are certain that all variables share one type. + * What makes this difficult is arrays, and being able to set the type. + * <br/>For <code>int a;</code> this is int. + * <br/>For <code>int a,b,c,d;</code> this is also int. + * <br/>For <code>int a,b[],c;</code> this is an assertion error since b is an int[], not an int. + * <br/>For <code>int a,b;</code>, then doing setType(String) on b, this is an assertion error. It is also a situation that you don't really want. + */ + default Type getCommonType() { + NodeList<VariableDeclarator> variables = getVariables(); + if (variables.isEmpty()) { + throw new AssertionError("There is no common type since there are no variables."); + } + Type type = variables.get(0).getType(); + for (int i = 1; i < variables.size(); i++) { + if (!variables.get(i).getType().equals(type)) { + throw new AssertionError("The variables do not have a common type."); + } + } + return type; + } + + /** + * Returns the element type. + * <br/>For <code>int a;</code> this is int. + * <br/>For <code>int a,b,c,d;</code> this is also int. + * <br/>For <code>int a,b[],c;</code> this is also int. Note: no mention of b being an array. + * <br/>For <code>int a,b;</code>, then doing setType(String) on b, then calling getElementType(). This is an assertion error. It is also a situation that you don't really want. + */ + default Type getElementType() { + NodeList<VariableDeclarator> variables = getVariables(); + if (variables.isEmpty()) { + throw new AssertionError("There is no element type since there are no variables."); + } + Type type = variables.get(0).getType().getElementType(); + for (int i = 1; i < variables.size(); i++) { + if (!variables.get(i).getType().getElementType().equals(type)) { + throw new AssertionError("The variables do not have a common type."); + } + } + return type; + } + + /** + * Returns the type that maximum shared type between all variables. + * The minimum common type does never include annotations on the array level. + * <p> + * <br/>For <code>int a;</code> this is int. + * <br/>For <code>int a,b,c,d;</code> this is also int. + * <br/>For <code>int a,b[],c;</code> this is also int. + * <br/>For <code>int[] a[][],b[],c[][];</code> this is int[][]. + */ + @DerivedProperty + default Optional<Type> getMaximumCommonType() { + return calculateMaximumCommonType(getVariables().stream().map(v -> v.getType()).collect(Collectors.toList())); + } + + static Optional<Type> calculateMaximumCommonType(List<Type> types) { + // we use a local class because we cannot use an helper static method in an interface + class Helper { + // Conceptually: given a type we start from the Element Type and get as many array levels as indicated + // From the implementation point of view we start from the actual type and we remove how many array + // levels as needed to get the target level of arrays + // It returns null if the type has less array levels then the desired target + private Optional<Type> toArrayLevel(Type type, int level) { + if (level > type.getArrayLevel()) { + return Optional.empty(); + } + for (int i = type.getArrayLevel(); i > level; i--) { + if (!(type instanceof ArrayType)) { + return Optional.empty(); + } + type = ((ArrayType) type).getComponentType(); + } + return Optional.of(type); + } + } + + Helper helper = new Helper(); + int level = 0; + boolean keepGoing = true; + // In practice we want to check for how many levels of arrays all the variables have the same type, + // including also the annotations + while (keepGoing) { + final int currentLevel = level; + // Now, given that equality on nodes consider the position the simplest way is to compare + // the pretty-printed string got for a node. We just check all them are the same and if they + // are we just just is not null + Object[] values = types.stream().map(v -> { + Optional<Type> t = helper.toArrayLevel(v, currentLevel); + return t.map(Node::toString).orElse(null); + }).distinct().toArray(); + if (values.length == 1 && values[0] != null) { + level++; + } else { + keepGoing = false; + } + } + return helper.toArrayLevel(types.get(0), --level); + } + +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithAbstractModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithAbstractModifier.java new file mode 100644 index 000000000..c81c4c616 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithAbstractModifier.java @@ -0,0 +1,20 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.ABSTRACT; + +/** + * A node that can be abstract. + */ +public interface NodeWithAbstractModifier<N extends Node> extends NodeWithModifiers<N> { + default boolean isAbstract() { + return getModifiers().contains(ABSTRACT); + } + + @SuppressWarnings("unchecked") + default N setAbstract(boolean set) { + return setModifier(ABSTRACT, set); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithAccessModifiers.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithAccessModifiers.java new file mode 100644 index 000000000..956962448 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithAccessModifiers.java @@ -0,0 +1,9 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; + +/** + * A node that can be public, protected, and/or private. + */ +public interface NodeWithAccessModifiers<N extends Node> extends NodeWithPublicModifier<N>, NodeWithPrivateModifier<N>, NodeWithProtectedModifier<N> { +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithFinalModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithFinalModifier.java new file mode 100644 index 000000000..91aae402e --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithFinalModifier.java @@ -0,0 +1,20 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.FINAL; + +/** + * A node that can be final. + */ +public interface NodeWithFinalModifier<N extends Node> extends NodeWithModifiers<N> { + default boolean isFinal() { + return getModifiers().contains(FINAL); + } + + @SuppressWarnings("unchecked") + default N setFinal(boolean set) { + return setModifier(FINAL, set); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithPrivateModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithPrivateModifier.java new file mode 100644 index 000000000..4aa022cfe --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithPrivateModifier.java @@ -0,0 +1,20 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.*; + +/** + * A node that can be private. + */ +public interface NodeWithPrivateModifier<N extends Node> extends NodeWithModifiers<N> { + default boolean isPrivate() { + return getModifiers().contains(PRIVATE); + } + + @SuppressWarnings("unchecked") + default N setPrivate(boolean set) { + return setModifier(PRIVATE, set); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithProtectedModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithProtectedModifier.java new file mode 100644 index 000000000..d2200b74a --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithProtectedModifier.java @@ -0,0 +1,20 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.*; + +/** + * A node that can be protected. + */ +public interface NodeWithProtectedModifier<N extends Node> extends NodeWithModifiers<N> { + default boolean isProtected() { + return getModifiers().contains(PROTECTED); + } + + @SuppressWarnings("unchecked") + default N setProtected(boolean set) { + return setModifier(PROTECTED, set); + } +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithPublicModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithPublicModifier.java new file mode 100644 index 000000000..64b4067bf --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithPublicModifier.java @@ -0,0 +1,21 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.*; + +/** + * A node that can be public. + */ +public interface NodeWithPublicModifier<N extends Node> extends NodeWithModifiers<N> { + default boolean isPublic() { + return getModifiers().contains(PUBLIC); + } + + @SuppressWarnings("unchecked") + default N setPublic(boolean set) { + return setModifier(PUBLIC, set); + } + +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithStaticModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithStaticModifier.java new file mode 100644 index 000000000..5ac63ea40 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithStaticModifier.java @@ -0,0 +1,22 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.STATIC; + +/** + * A node that can be static. + */ +public interface NodeWithStaticModifier<N extends Node> extends NodeWithModifiers<N> { + + default boolean isStatic() { + return getModifiers().contains(STATIC); + } + + @SuppressWarnings("unchecked") + default N setStatic(boolean set) { + return setModifier(STATIC, set); + } + +} diff --git a/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithStrictfpModifier.java b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithStrictfpModifier.java new file mode 100644 index 000000000..1d8fb4112 --- /dev/null +++ b/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/modifiers/NodeWithStrictfpModifier.java @@ -0,0 +1,20 @@ +package com.github.javaparser.ast.nodeTypes.modifiers; + +import com.github.javaparser.ast.Node; +import com.github.javaparser.ast.nodeTypes.NodeWithModifiers; + +import static com.github.javaparser.ast.Modifier.STRICTFP; + +/** + * A node that can be strictfp. + */ +public interface NodeWithStrictfpModifier<N extends Node> extends NodeWithModifiers<N> { + default boolean isStrictfp() { + return getModifiers().contains(STRICTFP); + } + + @SuppressWarnings("unchecked") + default N setStrictfp(boolean set) { + return setModifier(STRICTFP, set); + } +} |