aboutsummaryrefslogtreecommitdiff
path: root/javaparser-core-generators/src
diff options
context:
space:
mode:
authorDanny van Bruggen <hexagonaal@gmail.com>2017-11-05 01:04:10 +0100
committerDanny van Bruggen <hexagonaal@gmail.com>2017-11-05 01:04:10 +0100
commitbb989d92ac7dd22551f00a157357fcdce3563b7c (patch)
tree53541954435076562bd13078bfaa7607c675b2aa /javaparser-core-generators/src
parent2740e3bf6d224a138ea284fed10874ed75a211db (diff)
downloadjavaparser-bb989d92ac7dd22551f00a157357fcdce3563b7c.tar.gz
Fix generics weirdness that failed on Java 9
Diffstat (limited to 'javaparser-core-generators/src')
-rw-r--r--javaparser-core-generators/src/main/java/com/github/javaparser/generator/Generator.java62
-rw-r--r--javaparser-core-generators/src/main/java/com/github/javaparser/generator/NodeGenerator.java48
2 files changed, 57 insertions, 53 deletions
diff --git a/javaparser-core-generators/src/main/java/com/github/javaparser/generator/Generator.java b/javaparser-core-generators/src/main/java/com/github/javaparser/generator/Generator.java
index 832568595..f7860b04b 100644
--- a/javaparser-core-generators/src/main/java/com/github/javaparser/generator/Generator.java
+++ b/javaparser-core-generators/src/main/java/com/github/javaparser/generator/Generator.java
@@ -1,6 +1,8 @@
package com.github.javaparser.generator;
import com.github.javaparser.ast.Node;
+import com.github.javaparser.ast.body.CallableDeclaration;
+import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.StringLiteralExpr;
@@ -8,8 +10,10 @@ import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.utils.SourceRoot;
import javax.annotation.Generated;
+import java.util.List;
import static com.github.javaparser.ast.NodeList.toNodeList;
+import static com.github.javaparser.utils.CodeGenerationUtils.f;
/**
* A general pattern that the generators in this module will follow.
@@ -23,15 +27,19 @@ public abstract class Generator {
public abstract void generate() throws Exception;
- protected <T extends Node & NodeWithAnnotations<? extends T>> void annotateGenerated(T node) {
+ protected <T extends Node & NodeWithAnnotations<?>> void annotateGenerated(T node) {
annotate(node, Generated.class, new StringLiteralExpr(getClass().getName()));
}
- protected <T extends Node & NodeWithAnnotations<? extends T>> void annotateSuppressWarnings(T node) {
+ protected <T extends Node & NodeWithAnnotations<?>> void annotateSuppressWarnings(T node) {
annotate(node, SuppressWarnings.class, new StringLiteralExpr("unchecked"));
}
- private <T extends Node & NodeWithAnnotations<? extends T>> void annotate(T node, Class<?> annotation, Expression content) {
+ protected void annotateOverridden(MethodDeclaration method) {
+ annotate(method, Override.class, null);
+ }
+
+ private <T extends Node & NodeWithAnnotations<?>> void annotate(T node, Class<?> annotation, Expression content) {
node.setAnnotations(
node.getAnnotations().stream()
.filter(a -> !a.getNameAsString().equals(annotation.getSimpleName()))
@@ -45,8 +53,52 @@ public abstract class Generator {
node.tryAddImportToParentCompilationUnit(annotation);
}
- protected void annotateOverridden(MethodDeclaration method) {
- annotate(method, Override.class, null);
+ /**
+ * Utility method that looks for a method or constructor with an identical signature as "callable" and replaces it
+ * with callable. If not found, adds callable. When the new callable has no javadoc, any old javadoc will be kept.
+ */
+ protected void addOrReplaceWhenSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callable) {
+ addMethod(containingClassOrInterface, callable, () -> containingClassOrInterface.addMember(callable));
+ }
+
+ /**
+ * Utility method that looks for a method or constructor with an identical signature as "callable" and replaces it
+ * with callable. If not found, fails. When the new callable has no javadoc, any old javadoc will be kept. The
+ * method or constructor is annotated with the generator class.
+ */
+ protected void replaceWhenSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callable) {
+ addMethod(containingClassOrInterface, callable,
+ () -> {
+ throw new AssertionError(f("Wanted to regenerate a method with signature %s in %s, but it wasn't there.", callable.getSignature(), containingClassOrInterface.getNameAsString()));
+ });
+ }
+
+ private void addMethod(
+ ClassOrInterfaceDeclaration containingClassOrInterface,
+ CallableDeclaration<?> callable,
+ Runnable onNoExistingMethod) {
+ List<CallableDeclaration<?>> existingCallables = containingClassOrInterface.getCallablesWithSignature(callable.getSignature());
+ if (existingCallables.isEmpty()) {
+ onNoExistingMethod.run();
+ return;
+ }
+ if (existingCallables.size() > 1) {
+ throw new AssertionError(f("Wanted to regenerate a method with signature %s in %s, but found more than one.", callable.getSignature(), containingClassOrInterface.getNameAsString()));
+ }
+ final CallableDeclaration<?> existingCallable = existingCallables.get(0);
+ callable.setJavadocComment(callable.getJavadocComment().orElse(existingCallable.getJavadocComment().orElse(null)));
+ annotateGenerated(callable);
+ containingClassOrInterface.getMembers().replace(existingCallable, callable);
+ }
+
+ /**
+ * Removes all methods from containingClassOrInterface that have the same signature as callable. This is not used by
+ * any code, but it is useful when changing a generator and you need to get rid of a set of outdated methods.
+ */
+ protected void removeMethodWithSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callable) {
+ for (CallableDeclaration<?> existingCallable : containingClassOrInterface.getCallablesWithSignature(callable.getSignature())) {
+ containingClassOrInterface.remove(existingCallable);
+ }
}
}
diff --git a/javaparser-core-generators/src/main/java/com/github/javaparser/generator/NodeGenerator.java b/javaparser-core-generators/src/main/java/com/github/javaparser/generator/NodeGenerator.java
index 01840316b..673990df2 100644
--- a/javaparser-core-generators/src/main/java/com/github/javaparser/generator/NodeGenerator.java
+++ b/javaparser-core-generators/src/main/java/com/github/javaparser/generator/NodeGenerator.java
@@ -43,52 +43,4 @@ public abstract class NodeGenerator extends Generator {
}
protected abstract void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) throws Exception;
-
- /**
- * Utility method that looks for a method or constructor with an identical signature as "callable" and replaces it
- * with callable. If not found, adds callable. When the new callable has no javadoc, any old javadoc will be kept.
- */
- protected void addOrReplaceWhenSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callable) {
- addMethod(containingClassOrInterface, callable, () -> containingClassOrInterface.addMember(callable));
- }
-
- /**
- * Utility method that looks for a method or constructor with an identical signature as "callable" and replaces it
- * with callable. If not found, fails. When the new callable has no javadoc, any old javadoc will be kept. The
- * method or constructor is annotated with the generator class.
- */
- protected void replaceWhenSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callable) {
- addMethod(containingClassOrInterface, callable,
- () -> {
- throw new AssertionError(f("Wanted to regenerate a method with signature %s in %s, but it wasn't there.", callable.getSignature(), containingClassOrInterface.getNameAsString()));
- });
- }
-
- private void addMethod(
- ClassOrInterfaceDeclaration containingClassOrInterface,
- CallableDeclaration<?> callable,
- Runnable onNoExistingMethod) {
- List<CallableDeclaration<?>> existingCallables = containingClassOrInterface.getCallablesWithSignature(callable.getSignature());
- if (existingCallables.isEmpty()) {
- onNoExistingMethod.run();
- return;
- }
- if (existingCallables.size() > 1) {
- throw new AssertionError(f("Wanted to regenerate a method with signature %s in %s, but found more than one.", callable.getSignature(), containingClassOrInterface.getNameAsString()));
- }
- final CallableDeclaration<?> existingCallable = existingCallables.get(0);
- callable.setJavadocComment(callable.getJavadocComment().orElse(existingCallable.getJavadocComment().orElse(null)));
- annotateGenerated(callable);
- containingClassOrInterface.getMembers().replace(existingCallable, callable);
- }
-
- /**
- * Removes all methods from containingClassOrInterface that have the same signature as callable. This is not used by
- * any code, but it is useful when changing a generator and you need to get rid of a set of outdated methods.
- */
- protected void removeMethodWithSameSignature(ClassOrInterfaceDeclaration containingClassOrInterface, CallableDeclaration<?> callable) {
- for (CallableDeclaration<?> existingCallable : containingClassOrInterface.getCallablesWithSignature(callable.getSignature())) {
- containingClassOrInterface.remove(existingCallable);
- }
- }
}