j3.27.0.0 working
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>javaparser-parent</artifactId>
|
||||
<groupId>com.github.javaparser</groupId>
|
||||
<version>3.27.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>javaparser-core-generators</artifactId>
|
||||
<description>A code generator framework, and the generators for javaparser-core</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.javaparser</groupId>
|
||||
<artifactId>javaparser-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>run-generators</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate-javaparser-core</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<classpathScope>test</classpathScope>
|
||||
<mainClass>com.github.javaparser.generator.core.CoreGenerator</mainClass>
|
||||
<arguments>
|
||||
<argument>${project.basedir}</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator;
|
||||
|
||||
import com.github.javaparser.ParseResult;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class CompilationUnitGenerator extends Generator {
|
||||
|
||||
protected CompilationUnitGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate() throws Exception {
|
||||
List<ParseResult<CompilationUnit>> parsedCus = sourceRoot.tryToParse();
|
||||
for (ParseResult<CompilationUnit> cu : parsedCus) {
|
||||
cu.ifSuccessful(this::generateCompilationUnit);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void generateCompilationUnit(CompilationUnit compilationUnit);
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator;
|
||||
|
||||
import static com.github.javaparser.ast.NodeList.toNodeList;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.Generated;
|
||||
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;
|
||||
import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A general pattern that the generators in this module will follow.
|
||||
*/
|
||||
public abstract class Generator {
|
||||
protected final SourceRoot sourceRoot;
|
||||
|
||||
protected Generator(SourceRoot sourceRoot) {
|
||||
this.sourceRoot = sourceRoot;
|
||||
}
|
||||
|
||||
public abstract void generate() throws Exception;
|
||||
|
||||
protected <T extends Node & NodeWithAnnotations<?>> void annotateGenerated(T node) {
|
||||
annotate(node, Generated.class, new StringLiteralExpr(getClass().getName()));
|
||||
}
|
||||
|
||||
protected <T extends Node & NodeWithAnnotations<?>> void annotateSuppressWarnings(T node) {
|
||||
annotate(node, SuppressWarnings.class, new StringLiteralExpr("unchecked"));
|
||||
}
|
||||
|
||||
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()))
|
||||
.collect(toNodeList()));
|
||||
|
||||
if (content != null) {
|
||||
node.addSingleMemberAnnotation(annotation.getSimpleName(), content);
|
||||
} else {
|
||||
node.addMarkerAnnotation(annotation.getSimpleName());
|
||||
}
|
||||
node.tryAddImportToParentCompilationUnit(annotation);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
.orElseGet(() -> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.utils.Log;
|
||||
import com.github.javaparser.utils.Pair;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Makes it easier to generate code in the core AST nodes. The generateNode method will get every node type passed to
|
||||
* it, ready for modification.
|
||||
*/
|
||||
public abstract class NodeGenerator extends Generator {
|
||||
protected NodeGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
public final void generate() throws Exception {
|
||||
Log.info("Running %s", () -> getClass().getSimpleName());
|
||||
for (BaseNodeMetaModel nodeMetaModel : JavaParserMetaModel.getNodeMetaModels()) {
|
||||
Pair<CompilationUnit, ClassOrInterfaceDeclaration> result = parseNode(nodeMetaModel);
|
||||
generateNode(nodeMetaModel, result.a, result.b);
|
||||
}
|
||||
after();
|
||||
}
|
||||
|
||||
protected Pair<CompilationUnit, ClassOrInterfaceDeclaration> parseNode(BaseNodeMetaModel nodeMetaModel) {
|
||||
CompilationUnit nodeCu =
|
||||
sourceRoot.parse(nodeMetaModel.getPackageName(), nodeMetaModel.getTypeName() + ".java");
|
||||
ClassOrInterfaceDeclaration nodeCoid = nodeCu.getClassByName(nodeMetaModel.getTypeName())
|
||||
.orElseThrow(() -> new AssertionError("Can't find class"));
|
||||
return new Pair<>(nodeCu, nodeCoid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Annotate a method with the {@link Override} annotation, if it overrides other method.
|
||||
*
|
||||
* @param nodeMetaModel The current meta model.
|
||||
* @param methodDeclaration The method declaration.
|
||||
*/
|
||||
protected void annotateWhenOverridden(BaseNodeMetaModel nodeMetaModel, MethodDeclaration methodDeclaration) {
|
||||
Class<? extends Node> type = nodeMetaModel.getType();
|
||||
Class<?> superClass = type.getSuperclass();
|
||||
|
||||
boolean isOverriding = Arrays.stream(superClass.getMethods())
|
||||
.filter(m -> m.getName().equals(methodDeclaration.getNameAsString()))
|
||||
.anyMatch(m -> m.getParameters().length
|
||||
== methodDeclaration.getParameters().size());
|
||||
if (isOverriding) {
|
||||
annotateOverridden(methodDeclaration);
|
||||
}
|
||||
}
|
||||
|
||||
protected void after() throws Exception {}
|
||||
|
||||
protected abstract void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid)
|
||||
throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator;
|
||||
|
||||
import static com.github.javaparser.ast.Modifier.Keyword.PUBLIC;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;
|
||||
import com.github.javaparser.ast.expr.Name;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.utils.Log;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Makes it easier to generate visitor classes.
|
||||
* It will create missing visit methods on the fly,
|
||||
* and will ask you to fill in the bodies of the visit methods.
|
||||
*/
|
||||
public abstract class VisitorGenerator extends Generator {
|
||||
private final String pkg;
|
||||
private final String visitorClassName;
|
||||
private final String returnType;
|
||||
private final String argumentType;
|
||||
private final boolean createMissingVisitMethods;
|
||||
|
||||
protected VisitorGenerator(
|
||||
SourceRoot sourceRoot,
|
||||
String pkg,
|
||||
String visitorClassName,
|
||||
String returnType,
|
||||
String argumentType,
|
||||
boolean createMissingVisitMethods) {
|
||||
super(sourceRoot);
|
||||
this.pkg = pkg;
|
||||
this.visitorClassName = visitorClassName;
|
||||
this.returnType = returnType;
|
||||
this.argumentType = argumentType;
|
||||
this.createMissingVisitMethods = createMissingVisitMethods;
|
||||
}
|
||||
|
||||
public final void generate() throws Exception {
|
||||
Log.info("Running %s", () -> getClass().getSimpleName());
|
||||
|
||||
final CompilationUnit compilationUnit = sourceRoot
|
||||
.tryToParse(pkg, visitorClassName + ".java")
|
||||
.getResult()
|
||||
.get();
|
||||
|
||||
Optional<ClassOrInterfaceDeclaration> visitorClassOptional = compilationUnit.getClassByName(visitorClassName);
|
||||
if (!visitorClassOptional.isPresent()) {
|
||||
visitorClassOptional = compilationUnit.getInterfaceByName(visitorClassName);
|
||||
}
|
||||
final ClassOrInterfaceDeclaration visitorClass = visitorClassOptional.get();
|
||||
|
||||
JavaParserMetaModel.getNodeMetaModels().stream()
|
||||
.filter((baseNodeMetaModel) -> !baseNodeMetaModel.isAbstract())
|
||||
.forEach(node -> generateVisitMethodForNode(node, visitorClass, compilationUnit));
|
||||
after();
|
||||
}
|
||||
|
||||
protected void after() throws Exception {}
|
||||
|
||||
private void generateVisitMethodForNode(
|
||||
BaseNodeMetaModel node, ClassOrInterfaceDeclaration visitorClass, CompilationUnit compilationUnit) {
|
||||
final Optional<MethodDeclaration> existingVisitMethod = visitorClass.getMethods().stream()
|
||||
.filter(m -> "visit".equals(m.getNameAsString()))
|
||||
.filter(m -> m.getParameter(0).getType().toString().equals(node.getTypeName()))
|
||||
.findFirst();
|
||||
|
||||
if (existingVisitMethod.isPresent()) {
|
||||
generateVisitMethodBody(node, existingVisitMethod.get(), compilationUnit);
|
||||
} else if (createMissingVisitMethods) {
|
||||
MethodDeclaration newVisitMethod = visitorClass
|
||||
.addMethod("visit")
|
||||
.addParameter(node.getTypeNameGenerified(), "n")
|
||||
.addParameter(argumentType, "arg")
|
||||
.setType(returnType);
|
||||
if (!visitorClass.isInterface()) {
|
||||
newVisitMethod
|
||||
.addAnnotation(new MarkerAnnotationExpr(new Name("Override")))
|
||||
.addModifier(PUBLIC);
|
||||
}
|
||||
generateVisitMethodBody(node, newVisitMethod, compilationUnit);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit);
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core;
|
||||
|
||||
import static com.github.javaparser.ParserConfiguration.LanguageLevel.RAW;
|
||||
|
||||
import com.github.javaparser.ParserConfiguration;
|
||||
import com.github.javaparser.StaticJavaParser;
|
||||
import com.github.javaparser.generator.core.node.*;
|
||||
import com.github.javaparser.generator.core.other.BndGenerator;
|
||||
import com.github.javaparser.generator.core.other.TokenKindGenerator;
|
||||
import com.github.javaparser.generator.core.quality.NotNullGenerator;
|
||||
import com.github.javaparser.generator.core.visitor.*;
|
||||
import com.github.javaparser.utils.Log;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* Generates all generated visitors in the javaparser-core module.
|
||||
* Suggested usage is by running the run_core_generators.sh script.
|
||||
* You may want to run_metamodel_generator.sh before that.
|
||||
*/
|
||||
public class CoreGenerator {
|
||||
private static final ParserConfiguration parserConfiguration = new ParserConfiguration().setLanguageLevel(RAW)
|
||||
// .setStoreTokens(false)
|
||||
// .setAttributeComments(false)
|
||||
// .setLexicalPreservationEnabled(true)
|
||||
;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length != 1) {
|
||||
throw new RuntimeException("Need 1 parameter: the JavaParser source checkout root directory.");
|
||||
}
|
||||
Log.setAdapter(new Log.StandardOutStandardErrorAdapter());
|
||||
final Path root = Paths.get(args[0], "..", "javaparser-core", "src", "main", "java");
|
||||
final SourceRoot sourceRoot = new SourceRoot(root, parserConfiguration)
|
||||
// .setPrinter(LexicalPreservingPrinter::print)
|
||||
;
|
||||
StaticJavaParser.setConfiguration(parserConfiguration);
|
||||
|
||||
final Path generatedJavaCcRoot =
|
||||
Paths.get(args[0], "..", "javaparser-core", "target", "generated-sources", "javacc");
|
||||
final SourceRoot generatedJavaCcSourceRoot = new SourceRoot(generatedJavaCcRoot, parserConfiguration)
|
||||
// .setPrinter(LexicalPreservingPrinter::print)
|
||||
;
|
||||
|
||||
new CoreGenerator().run(sourceRoot, generatedJavaCcSourceRoot);
|
||||
|
||||
sourceRoot.saveAll();
|
||||
}
|
||||
|
||||
private void run(SourceRoot sourceRoot, SourceRoot generatedJavaCcSourceRoot) throws Exception {
|
||||
new TypeCastingGenerator(sourceRoot).generate();
|
||||
new GenericListVisitorAdapterGenerator(sourceRoot).generate();
|
||||
new GenericVisitorAdapterGenerator(sourceRoot).generate();
|
||||
new GenericVisitorWithDefaultsGenerator(sourceRoot).generate();
|
||||
new EqualsVisitorGenerator(sourceRoot).generate();
|
||||
new ObjectIdentityEqualsVisitorGenerator(sourceRoot).generate();
|
||||
new NoCommentEqualsVisitorGenerator(sourceRoot).generate();
|
||||
new VoidVisitorAdapterGenerator(sourceRoot).generate();
|
||||
new VoidVisitorGenerator(sourceRoot).generate();
|
||||
new VoidVisitorWithDefaultsGenerator(sourceRoot).generate();
|
||||
new GenericVisitorGenerator(sourceRoot).generate();
|
||||
new HashCodeVisitorGenerator(sourceRoot).generate();
|
||||
new ObjectIdentityHashCodeVisitorGenerator(sourceRoot).generate();
|
||||
new NoCommentHashCodeVisitorGenerator(sourceRoot).generate();
|
||||
new CloneVisitorGenerator(sourceRoot).generate();
|
||||
new ModifierVisitorGenerator(sourceRoot).generate();
|
||||
|
||||
new PropertyGenerator(sourceRoot).generate();
|
||||
new RemoveMethodGenerator(sourceRoot).generate();
|
||||
new ReplaceMethodGenerator(sourceRoot).generate();
|
||||
new CloneGenerator(sourceRoot).generate();
|
||||
new GetMetaModelGenerator(sourceRoot).generate();
|
||||
new MainConstructorGenerator(sourceRoot).generate();
|
||||
new NodeModifierGenerator(sourceRoot).generate();
|
||||
new AcceptGenerator(sourceRoot).generate();
|
||||
new TokenKindGenerator(sourceRoot, generatedJavaCcSourceRoot).generate();
|
||||
new BndGenerator(sourceRoot).generate();
|
||||
|
||||
new NotNullGenerator(sourceRoot).generate();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.visitor.GenericVisitor;
|
||||
import com.github.javaparser.ast.visitor.VoidVisitor;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class AcceptGenerator extends NodeGenerator {
|
||||
private final MethodDeclaration genericAccept;
|
||||
private final MethodDeclaration voidAccept;
|
||||
|
||||
public AcceptGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
genericAccept = parseBodyDeclaration(
|
||||
"@Override public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { return v.visit(this, arg); }")
|
||||
.asMethodDeclaration();
|
||||
voidAccept = parseBodyDeclaration(
|
||||
"@Override public <A> void accept(final VoidVisitor<A> v, final A arg) { v.visit(this, arg); }")
|
||||
.asMethodDeclaration();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
if (nodeMetaModel.isAbstract()) {
|
||||
return;
|
||||
}
|
||||
nodeCu.addImport(GenericVisitor.class);
|
||||
nodeCu.addImport(VoidVisitor.class);
|
||||
addOrReplaceWhenSameSignature(nodeCoid, genericAccept);
|
||||
addOrReplaceWhenSameSignature(nodeCoid, voidAccept);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.visitor.CloneVisitor;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class CloneGenerator extends NodeGenerator {
|
||||
public CloneGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
nodeCu.addImport(CloneVisitor.class);
|
||||
MethodDeclaration cloneMethod = (MethodDeclaration) parseBodyDeclaration(f(
|
||||
"@Override public %s clone() { return (%s) accept(new CloneVisitor(), null); }",
|
||||
nodeMetaModel.getTypeNameGenerified(), nodeMetaModel.getTypeNameGenerified()));
|
||||
addOrReplaceWhenSameSignature(nodeCoid, cloneMethod);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class GetMetaModelGenerator extends NodeGenerator {
|
||||
public GetMetaModelGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
final MethodDeclaration getMetaModelMethod = (MethodDeclaration) parseBodyDeclaration(f(
|
||||
"%s public %s getMetaModel() { return JavaParserMetaModel.%s; }",
|
||||
nodeMetaModel.isRootNode() ? "" : "@Override",
|
||||
nodeMetaModel.getClass().getSimpleName(),
|
||||
nodeMetaModel.getMetaModelFieldName()));
|
||||
|
||||
addOrReplaceWhenSameSignature(nodeCoid, getMetaModelMethod);
|
||||
nodeCu.addImport(nodeMetaModel.getClass().getName());
|
||||
nodeCu.addImport(JavaParserMetaModel.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseExplicitConstructorInvocationStmt;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.TokenRange;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.ConstructorDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SeparatedItemStringBuilder;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class MainConstructorGenerator extends NodeGenerator {
|
||||
public MainConstructorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
if (nodeMetaModel.is(Node.class)) {
|
||||
return;
|
||||
}
|
||||
ConstructorDeclaration constructor = new ConstructorDeclaration()
|
||||
.setPublic(true)
|
||||
.setName(nodeCoid.getNameAsString())
|
||||
.addParameter(TokenRange.class, "tokenRange")
|
||||
.setJavadocComment("\n * This constructor is used by the parser and is considered private.\n ");
|
||||
|
||||
BlockStmt body = constructor.getBody();
|
||||
|
||||
SeparatedItemStringBuilder superCall = new SeparatedItemStringBuilder("super(", ", ", ");");
|
||||
superCall.append("tokenRange");
|
||||
for (PropertyMetaModel parameter : nodeMetaModel.getConstructorParameters()) {
|
||||
constructor.addParameter(parameter.getTypeNameForSetter(), parameter.getName());
|
||||
if (nodeMetaModel.getDeclaredPropertyMetaModels().contains(parameter)) {
|
||||
body.addStatement(f("%s(%s);", parameter.getSetterMethodName(), parameter.getName()));
|
||||
} else {
|
||||
superCall.append(parameter.getName());
|
||||
}
|
||||
}
|
||||
|
||||
body.getStatements().addFirst(parseExplicitConstructorInvocationStmt(superCall.toString()));
|
||||
|
||||
body.addStatement("customInitialization();");
|
||||
|
||||
addOrReplaceWhenSameSignature(nodeCoid, constructor);
|
||||
nodeCu.addImport(TokenRange.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class NodeModifierGenerator extends NodeGenerator {
|
||||
public NodeModifierGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
nodeCoid.setFinal(false).setPublic(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseType;
|
||||
import static com.github.javaparser.ast.Modifier.Keyword.FINAL;
|
||||
import static com.github.javaparser.ast.Modifier.Keyword.PUBLIC;
|
||||
import static com.github.javaparser.ast.Modifier.createModifierList;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
import static com.github.javaparser.utils.Utils.camelCaseToScreaming;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.EnumConstantDeclaration;
|
||||
import com.github.javaparser.ast.body.EnumDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.observer.ObservableProperty;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.generator.core.utils.CodeUtils;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.*;
|
||||
|
||||
public class PropertyGenerator extends NodeGenerator {
|
||||
|
||||
private final Map<String, PropertyMetaModel> declaredProperties = new HashMap<>();
|
||||
private final Map<String, PropertyMetaModel> derivedProperties = new HashMap<>();
|
||||
|
||||
public PropertyGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
for (PropertyMetaModel property : nodeMetaModel.getDeclaredPropertyMetaModels()) {
|
||||
generateGetter(nodeMetaModel, nodeCoid, property);
|
||||
generateSetter(nodeMetaModel, nodeCoid, property);
|
||||
}
|
||||
nodeMetaModel.getDerivedPropertyMetaModels().forEach(p -> derivedProperties.put(p.getName(), p));
|
||||
}
|
||||
|
||||
private void generateSetter(
|
||||
BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
|
||||
// Ensure the relevant imports have been added for the methods/annotations used
|
||||
nodeCoid.findCompilationUnit().get().addImport(ObservableProperty.class);
|
||||
|
||||
final String name = property.getName();
|
||||
// Fill body
|
||||
final String observableName = camelCaseToScreaming(name.startsWith("is") ? name.substring(2) : name);
|
||||
declaredProperties.put(observableName, property);
|
||||
|
||||
if (property == JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel) {
|
||||
// Node.comment has a very specific setter that we shouldn't overwrite.
|
||||
return;
|
||||
}
|
||||
|
||||
final MethodDeclaration setter = new MethodDeclaration(
|
||||
createModifierList(PUBLIC),
|
||||
parseType(property.getContainingNodeMetaModel().getTypeNameGenerified()),
|
||||
property.getSetterMethodName());
|
||||
annotateWhenOverridden(nodeMetaModel, setter);
|
||||
if (property.getContainingNodeMetaModel().hasWildcard()) {
|
||||
setter.setType(parseType("T"));
|
||||
}
|
||||
setter.addAndGetParameter(property.getTypeNameForSetter(), property.getName())
|
||||
.addModifier(FINAL);
|
||||
|
||||
final BlockStmt body = setter.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
if (property.isRequired()) {
|
||||
Class<?> type = property.getType();
|
||||
if (property.isNonEmpty() && property.isSingular()) {
|
||||
nodeCoid.findCompilationUnit()
|
||||
.get()
|
||||
.addImport("com.github.javaparser.utils.Utils.assertNonEmpty", true, false);
|
||||
body.addStatement(f("assertNonEmpty(%s);", name));
|
||||
} else if (type != boolean.class && type != int.class) {
|
||||
nodeCoid.findCompilationUnit()
|
||||
.get()
|
||||
.addImport("com.github.javaparser.utils.Utils.assertNotNull", true, false);
|
||||
body.addStatement(f("assertNotNull(%s);", name));
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the new value is the same as the old value
|
||||
String returnValue = CodeUtils.castValue("this", setter.getType(), nodeMetaModel.getTypeName());
|
||||
body.addStatement(f("if (%s == this.%s) { return %s; }", name, name, returnValue));
|
||||
|
||||
body.addStatement(f("notifyPropertyChange(ObservableProperty.%s, this.%s, %s);", observableName, name, name));
|
||||
if (property.isNode()) {
|
||||
body.addStatement(f("if (this.%s != null) this.%s.setParentNode(null);", name, name));
|
||||
}
|
||||
body.addStatement(f("this.%s = %s;", name, name));
|
||||
if (property.isNode()) {
|
||||
body.addStatement(f("setAsParentNodeOf(%s);", name));
|
||||
}
|
||||
if (property.getContainingNodeMetaModel().hasWildcard()) {
|
||||
body.addStatement(f("return (T) this;"));
|
||||
} else {
|
||||
body.addStatement(f("return this;"));
|
||||
}
|
||||
addOrReplaceWhenSameSignature(nodeCoid, setter);
|
||||
if (property.getContainingNodeMetaModel().hasWildcard()) {
|
||||
annotateSuppressWarnings(setter);
|
||||
}
|
||||
}
|
||||
|
||||
private void generateGetter(
|
||||
BaseNodeMetaModel nodeMetaModel, ClassOrInterfaceDeclaration nodeCoid, PropertyMetaModel property) {
|
||||
final MethodDeclaration getter = new MethodDeclaration(
|
||||
createModifierList(PUBLIC), parseType(property.getTypeNameForGetter()), property.getGetterMethodName());
|
||||
annotateWhenOverridden(nodeMetaModel, getter);
|
||||
final BlockStmt body = getter.getBody().get();
|
||||
body.getStatements().clear();
|
||||
if (property.isOptional()) {
|
||||
// Ensure imports have been included.
|
||||
nodeCoid.findCompilationUnit().get().addImport(Optional.class);
|
||||
body.addStatement(f("return Optional.ofNullable(%s);", property.getName()));
|
||||
} else {
|
||||
body.addStatement(f("return %s;", property.getName()));
|
||||
}
|
||||
addOrReplaceWhenSameSignature(nodeCoid, getter);
|
||||
}
|
||||
|
||||
private void generateObservableProperty(
|
||||
EnumDeclaration observablePropertyEnum, PropertyMetaModel property, boolean derived) {
|
||||
boolean isAttribute = !Node.class.isAssignableFrom(property.getType());
|
||||
String name = property.getName();
|
||||
String constantName = camelCaseToScreaming(name.startsWith("is") ? name.substring(2) : name);
|
||||
EnumConstantDeclaration enumConstantDeclaration = observablePropertyEnum.addEnumConstant(constantName);
|
||||
if (isAttribute) {
|
||||
enumConstantDeclaration.addArgument("Type.SINGLE_ATTRIBUTE");
|
||||
} else {
|
||||
if (property.isNodeList()) {
|
||||
enumConstantDeclaration.addArgument("Type.MULTIPLE_REFERENCE");
|
||||
} else {
|
||||
enumConstantDeclaration.addArgument("Type.SINGLE_REFERENCE");
|
||||
}
|
||||
}
|
||||
if (derived) {
|
||||
enumConstantDeclaration.addArgument("true");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void after() throws Exception {
|
||||
CompilationUnit observablePropertyCu = sourceRoot
|
||||
.tryToParse("com.github.javaparser.ast.observer", "ObservableProperty.java")
|
||||
.getResult()
|
||||
.get();
|
||||
EnumDeclaration observablePropertyEnum =
|
||||
observablePropertyCu.getEnumByName("ObservableProperty").get();
|
||||
observablePropertyEnum.getEntries().clear();
|
||||
List<String> observablePropertyNames = new LinkedList<>(declaredProperties.keySet());
|
||||
observablePropertyNames.sort(String::compareTo);
|
||||
for (String propName : observablePropertyNames) {
|
||||
generateObservableProperty(observablePropertyEnum, declaredProperties.get(propName), false);
|
||||
}
|
||||
List<String> derivedPropertyNames = new LinkedList<>(derivedProperties.keySet());
|
||||
derivedPropertyNames.sort(String::compareTo);
|
||||
for (String propName : derivedPropertyNames) {
|
||||
generateObservableProperty(observablePropertyEnum, derivedProperties.get(propName), true);
|
||||
}
|
||||
observablePropertyEnum.addEnumConstant("RANGE");
|
||||
observablePropertyEnum.addEnumConstant("COMMENTED_NODE");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
import static com.github.javaparser.utils.Utils.capitalize;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class RemoveMethodGenerator extends NodeGenerator {
|
||||
public RemoveMethodGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
MethodDeclaration removeNodeMethod =
|
||||
(MethodDeclaration) parseBodyDeclaration("public boolean remove(Node node) {}");
|
||||
nodeCu.addImport(Node.class);
|
||||
annotateWhenOverridden(nodeMetaModel, removeNodeMethod);
|
||||
|
||||
final BlockStmt body = removeNodeMethod.getBody().get();
|
||||
|
||||
body.addStatement("if (node == null) { return false; }");
|
||||
int numberPropertiesDeclared = 0;
|
||||
for (PropertyMetaModel property : nodeMetaModel.getDeclaredPropertyMetaModels()) {
|
||||
if (!property.isNode()) {
|
||||
continue;
|
||||
}
|
||||
String check;
|
||||
if (property.isNodeList()) {
|
||||
check = nodeListCheck(property);
|
||||
} else {
|
||||
if (property.isRequired()) {
|
||||
continue;
|
||||
}
|
||||
String removeAttributeMethodName = generateRemoveMethodForAttribute(nodeCoid, nodeMetaModel, property);
|
||||
check = attributeCheck(property, removeAttributeMethodName);
|
||||
}
|
||||
if (property.isOptional()) {
|
||||
check = f("if (%s != null) { %s }", property.getName(), check);
|
||||
}
|
||||
body.addStatement(check);
|
||||
numberPropertiesDeclared++;
|
||||
}
|
||||
if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
|
||||
body.addStatement("return super.remove(node);");
|
||||
} else {
|
||||
body.addStatement("return false;");
|
||||
}
|
||||
|
||||
if (!nodeMetaModel.isRootNode() && numberPropertiesDeclared == 0) {
|
||||
removeMethodWithSameSignature(nodeCoid, removeNodeMethod);
|
||||
} else {
|
||||
addOrReplaceWhenSameSignature(nodeCoid, removeNodeMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private String attributeCheck(PropertyMetaModel property, String removeAttributeMethodName) {
|
||||
return f(
|
||||
"if (node == %s) {" + " %s();" + " return true;\n" + "}",
|
||||
property.getName(), removeAttributeMethodName);
|
||||
}
|
||||
|
||||
private String nodeListCheck(PropertyMetaModel property) {
|
||||
return f(
|
||||
"for (int i = 0; i < %s.size(); i++) {" + " if (%s.get(i) == node) {"
|
||||
+ " %s.remove(i);"
|
||||
+ " return true;"
|
||||
+ " }"
|
||||
+ "}",
|
||||
property.getName(), property.getName(), property.getName());
|
||||
}
|
||||
|
||||
private String generateRemoveMethodForAttribute(
|
||||
ClassOrInterfaceDeclaration nodeCoid, BaseNodeMetaModel nodeMetaModel, PropertyMetaModel property) {
|
||||
final String methodName = "remove" + capitalize(property.getName());
|
||||
final MethodDeclaration removeMethod = (MethodDeclaration)
|
||||
parseBodyDeclaration(f("public %s %s() {}", nodeMetaModel.getTypeName(), methodName));
|
||||
|
||||
final BlockStmt block = removeMethod.getBody().get();
|
||||
block.addStatement(f("return %s((%s) null);", property.getSetterMethodName(), property.getTypeNameForSetter()));
|
||||
|
||||
addOrReplaceWhenSameSignature(nodeCoid, removeMethod);
|
||||
return methodName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class ReplaceMethodGenerator extends NodeGenerator {
|
||||
public ReplaceMethodGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid) {
|
||||
MethodDeclaration replaceNodeMethod =
|
||||
(MethodDeclaration) parseBodyDeclaration("public boolean replace(Node node, Node replacementNode) {}");
|
||||
nodeCu.addImport(Node.class);
|
||||
annotateWhenOverridden(nodeMetaModel, replaceNodeMethod);
|
||||
|
||||
final BlockStmt body = replaceNodeMethod.getBody().get();
|
||||
|
||||
body.addStatement("if (node == null) { return false; }");
|
||||
|
||||
int numberPropertiesDeclared = 0;
|
||||
for (PropertyMetaModel property : nodeMetaModel.getDeclaredPropertyMetaModels()) {
|
||||
if (!property.isNode()) {
|
||||
continue;
|
||||
}
|
||||
String check;
|
||||
if (property.isNodeList()) {
|
||||
check = nodeListCheck(property);
|
||||
} else {
|
||||
check = attributeCheck(property, property.getSetterMethodName());
|
||||
}
|
||||
if (property.isOptional()) {
|
||||
check = f("if (%s != null) { %s }", property.getName(), check);
|
||||
}
|
||||
body.addStatement(check);
|
||||
numberPropertiesDeclared++;
|
||||
}
|
||||
if (nodeMetaModel.getSuperNodeMetaModel().isPresent()) {
|
||||
body.addStatement("return super.replace(node, replacementNode);");
|
||||
} else {
|
||||
body.addStatement("return false;");
|
||||
}
|
||||
|
||||
if (!nodeMetaModel.isRootNode() && numberPropertiesDeclared == 0) {
|
||||
removeMethodWithSameSignature(nodeCoid, replaceNodeMethod);
|
||||
} else {
|
||||
addOrReplaceWhenSameSignature(nodeCoid, replaceNodeMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private String attributeCheck(PropertyMetaModel property, String attributeSetterName) {
|
||||
return f(
|
||||
"if (node == %s) {" + " %s((%s) replacementNode);" + " return true;\n" + "}",
|
||||
property.getName(), attributeSetterName, property.getTypeName());
|
||||
}
|
||||
|
||||
private String nodeListCheck(PropertyMetaModel property) {
|
||||
return f(
|
||||
"for (int i = 0; i < %s.size(); i++) {" + " if (%s.get(i) == node) {"
|
||||
+ " %s.set(i, (%s) replacementNode);"
|
||||
+ " return true;"
|
||||
+ " }"
|
||||
+ "}",
|
||||
property.getName(), property.getName(), property.getName(), property.getTypeName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.node;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseBodyDeclaration;
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
import static com.github.javaparser.utils.Utils.set;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.generator.NodeGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.utils.Pair;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TypeCastingGenerator extends NodeGenerator {
|
||||
private final Set<BaseNodeMetaModel> baseNodes = set(
|
||||
JavaParserMetaModel.statementMetaModel,
|
||||
JavaParserMetaModel.expressionMetaModel,
|
||||
JavaParserMetaModel.typeMetaModel,
|
||||
JavaParserMetaModel.moduleDirectiveMetaModel,
|
||||
JavaParserMetaModel.bodyDeclarationMetaModel,
|
||||
JavaParserMetaModel.commentMetaModel);
|
||||
|
||||
public TypeCastingGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateNode(
|
||||
BaseNodeMetaModel nodeMetaModel, CompilationUnit nodeCu, ClassOrInterfaceDeclaration nodeCoid)
|
||||
throws Exception {
|
||||
Pair<CompilationUnit, ClassOrInterfaceDeclaration> baseCode = null;
|
||||
for (BaseNodeMetaModel baseNode : baseNodes) {
|
||||
if (nodeMetaModel == baseNode) {
|
||||
// We adjust the base models from the child nodes,
|
||||
// so we don't do anything when we *are* the base model.
|
||||
return;
|
||||
}
|
||||
if (nodeMetaModel.isInstanceOfMetaModel(baseNode)) {
|
||||
baseCode = parseNode(baseNode);
|
||||
}
|
||||
}
|
||||
|
||||
if (baseCode == null) {
|
||||
// Node is not a child of one of the base nodes, so we don't want to generate this method for it.
|
||||
return;
|
||||
}
|
||||
|
||||
final String typeName = nodeMetaModel.getTypeName();
|
||||
final ClassOrInterfaceDeclaration baseCoid = baseCode.b;
|
||||
final CompilationUnit baseCu = baseCode.a;
|
||||
|
||||
generateIsType(nodeMetaModel, baseCu, nodeCoid, baseCoid, typeName);
|
||||
generateAsType(nodeMetaModel, baseCu, nodeCoid, baseCoid, typeName);
|
||||
generateToType(nodeMetaModel, nodeCu, baseCu, nodeCoid, baseCoid, typeName);
|
||||
generateIfType(nodeMetaModel, nodeCu, baseCu, nodeCoid, baseCoid, typeName);
|
||||
}
|
||||
|
||||
private void generateAsType(
|
||||
BaseNodeMetaModel nodeMetaModel,
|
||||
CompilationUnit baseCu,
|
||||
ClassOrInterfaceDeclaration nodeCoid,
|
||||
ClassOrInterfaceDeclaration baseCoid,
|
||||
String typeName) {
|
||||
baseCu.addImport("com.github.javaparser.utils.CodeGenerationUtils.f", true, false);
|
||||
|
||||
final MethodDeclaration asTypeBaseMethod = (MethodDeclaration) parseBodyDeclaration(f(
|
||||
"public %s as%s() { throw new IllegalStateException(f(\"%%s is not %s, it is %%s\", this, this.getClass().getSimpleName())); }",
|
||||
typeName, typeName, typeName));
|
||||
final MethodDeclaration asTypeNodeMethod = (MethodDeclaration)
|
||||
parseBodyDeclaration(f("@Override public %s as%s() { return this; }", typeName, typeName));
|
||||
|
||||
annotateWhenOverridden(nodeMetaModel, asTypeNodeMethod);
|
||||
|
||||
addOrReplaceWhenSameSignature(baseCoid, asTypeBaseMethod);
|
||||
addOrReplaceWhenSameSignature(nodeCoid, asTypeNodeMethod);
|
||||
}
|
||||
|
||||
private void generateToType(
|
||||
BaseNodeMetaModel nodeMetaModel,
|
||||
CompilationUnit nodeCu,
|
||||
CompilationUnit baseCu,
|
||||
ClassOrInterfaceDeclaration nodeCoid,
|
||||
ClassOrInterfaceDeclaration baseCoid,
|
||||
String typeName) {
|
||||
baseCu.addImport(Optional.class);
|
||||
nodeCu.addImport(Optional.class);
|
||||
|
||||
final MethodDeclaration toTypeBaseMethod = (MethodDeclaration) parseBodyDeclaration(
|
||||
f("public Optional<%s> to%s() { return Optional.empty(); }", typeName, typeName, typeName));
|
||||
final MethodDeclaration toTypeNodeMethod = (MethodDeclaration) parseBodyDeclaration(
|
||||
f("@Override public Optional<%s> to%s() { return Optional.of(this); }", typeName, typeName));
|
||||
|
||||
annotateWhenOverridden(nodeMetaModel, toTypeNodeMethod);
|
||||
|
||||
addOrReplaceWhenSameSignature(baseCoid, toTypeBaseMethod);
|
||||
addOrReplaceWhenSameSignature(nodeCoid, toTypeNodeMethod);
|
||||
}
|
||||
|
||||
private void generateIfType(
|
||||
BaseNodeMetaModel nodeMetaModel,
|
||||
CompilationUnit nodeCu,
|
||||
CompilationUnit baseCu,
|
||||
ClassOrInterfaceDeclaration nodeCoid,
|
||||
ClassOrInterfaceDeclaration baseCoid,
|
||||
String typeName) {
|
||||
baseCu.addImport(Consumer.class);
|
||||
nodeCu.addImport(Consumer.class);
|
||||
|
||||
final MethodDeclaration ifTypeBaseMethod = (MethodDeclaration)
|
||||
parseBodyDeclaration(f("public void if%s(Consumer<%s> action) { }", typeName, typeName));
|
||||
final MethodDeclaration ifTypeNodeMethod = (MethodDeclaration) parseBodyDeclaration(
|
||||
f("public void if%s(Consumer<%s> action) { action.accept(this); }", typeName, typeName));
|
||||
|
||||
annotateWhenOverridden(nodeMetaModel, ifTypeNodeMethod);
|
||||
|
||||
addOrReplaceWhenSameSignature(baseCoid, ifTypeBaseMethod);
|
||||
addOrReplaceWhenSameSignature(nodeCoid, ifTypeNodeMethod);
|
||||
}
|
||||
|
||||
private void generateIsType(
|
||||
BaseNodeMetaModel nodeMetaModel,
|
||||
CompilationUnit baseCu,
|
||||
ClassOrInterfaceDeclaration nodeCoid,
|
||||
ClassOrInterfaceDeclaration baseCoid,
|
||||
String typeName) {
|
||||
final MethodDeclaration baseIsTypeMethod =
|
||||
(MethodDeclaration) parseBodyDeclaration(f("public boolean is%s() { return false; }", typeName));
|
||||
final MethodDeclaration overriddenIsTypeMethod = (MethodDeclaration)
|
||||
parseBodyDeclaration(f("@Override public boolean is%s() { return true; }", typeName));
|
||||
|
||||
annotateWhenOverridden(nodeMetaModel, overriddenIsTypeMethod);
|
||||
|
||||
addOrReplaceWhenSameSignature(nodeCoid, overriddenIsTypeMethod);
|
||||
addOrReplaceWhenSameSignature(baseCoid, baseIsTypeMethod);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.other;
|
||||
|
||||
import com.github.javaparser.generator.Generator;
|
||||
import com.github.javaparser.utils.Log;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Generates the bnd.bnd file in javaparser-core.
|
||||
*/
|
||||
public class BndGenerator extends Generator {
|
||||
|
||||
public BndGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate() throws IOException {
|
||||
Log.info("Running %s", () -> getClass().getSimpleName());
|
||||
Path root = sourceRoot.getRoot();
|
||||
Path projectRoot = root.getParent().getParent().getParent();
|
||||
String lineSeparator = System.getProperty("line.separator");
|
||||
try (Stream<Path> stream = Files.walk(root)) {
|
||||
String packagesList = stream.filter(Files::isRegularFile)
|
||||
.map(path -> getPackageName(root, path))
|
||||
.distinct()
|
||||
.sorted()
|
||||
.reduce(
|
||||
null,
|
||||
(packageList, packageName) -> concatPackageName(packageName, packageList, lineSeparator));
|
||||
Path output = projectRoot.resolve("bnd.bnd");
|
||||
try (Writer writer = Files.newBufferedWriter(output)) {
|
||||
Path templateFile = projectRoot.resolve("bnd.bnd.template");
|
||||
String template = new String(Files.readAllBytes(templateFile), StandardCharsets.UTF_8);
|
||||
writer.write(template.replace("{exportedPackages}", packagesList));
|
||||
}
|
||||
Log.info("Written " + output);
|
||||
}
|
||||
}
|
||||
|
||||
private String concatPackageName(String packageName, String packageList, String lineSeparator) {
|
||||
return (packageList == null ? ("\\" + lineSeparator) : (packageList + ", \\" + lineSeparator)) + " "
|
||||
+ packageName;
|
||||
}
|
||||
|
||||
private static String getPackageName(Path root, Path path) {
|
||||
return root.relativize(path.getParent()).toString().replace(File.separatorChar, '.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.other;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Prints the LETTER and PART_LETTER tokens. They should be inserted into the grammar manually.
|
||||
*/
|
||||
public class GrammarLetterGenerator {
|
||||
public static void main(String[] args) {
|
||||
generate(
|
||||
"LETTER",
|
||||
c -> Character.isJavaIdentifierStart(c)
|
||||
|| Character.isHighSurrogate((char) (int) c)
|
||||
|| Character.isLowSurrogate((char) (int) c));
|
||||
generate(
|
||||
"PART_LETTER",
|
||||
c -> Character.isJavaIdentifierPart(c)
|
||||
|| Character.isHighSurrogate((char) (int) c)
|
||||
|| Character.isLowSurrogate((char) (int) c));
|
||||
}
|
||||
|
||||
private static void generate(String tokenName, Function<Integer, Boolean> f) {
|
||||
final String indent = " ";
|
||||
System.out.println(" < #" + tokenName + ": [");
|
||||
System.out.print(indent);
|
||||
int nltime = 0;
|
||||
int i = 0;
|
||||
while (i < 0x10000) {
|
||||
while (!f.apply(i) && i < 0x10000) {
|
||||
i++;
|
||||
}
|
||||
String start = format(i);
|
||||
while (f.apply(i) && i < 0x10000) {
|
||||
i++;
|
||||
}
|
||||
String end = format(i - 1);
|
||||
if (i >= 0x10000) {
|
||||
break;
|
||||
}
|
||||
if (start.equals(end)) {
|
||||
nltime++;
|
||||
System.out.print(start + ", ");
|
||||
} else {
|
||||
nltime += 2;
|
||||
System.out.print(start + "-" + end + ", ");
|
||||
}
|
||||
if (nltime >= 10) {
|
||||
nltime = 0;
|
||||
System.out.println();
|
||||
System.out.print(indent);
|
||||
}
|
||||
}
|
||||
// Too lazy to remove the final illegal comma.
|
||||
System.out.println("]");
|
||||
System.out.println(" | <UNICODE_ESCAPE>");
|
||||
System.out.println(" >");
|
||||
}
|
||||
|
||||
private static String format(int i) {
|
||||
return String.format("\"\\u%04x\"", i);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.other;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.NodeList;
|
||||
import com.github.javaparser.ast.body.BodyDeclaration;
|
||||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
|
||||
import com.github.javaparser.ast.body.EnumConstantDeclaration;
|
||||
import com.github.javaparser.ast.body.EnumDeclaration;
|
||||
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
|
||||
import com.github.javaparser.ast.stmt.ReturnStmt;
|
||||
import com.github.javaparser.ast.stmt.SwitchEntry;
|
||||
import com.github.javaparser.ast.stmt.SwitchStmt;
|
||||
import com.github.javaparser.generator.Generator;
|
||||
import com.github.javaparser.utils.Log;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates the TokenKind enum from {@link com.github.javaparser.GeneratedJavaParserConstants}
|
||||
*/
|
||||
public class TokenKindGenerator extends Generator {
|
||||
private final SourceRoot generatedJavaCcSourceRoot;
|
||||
|
||||
public TokenKindGenerator(SourceRoot sourceRoot, SourceRoot generatedJavaCcSourceRoot) {
|
||||
super(sourceRoot);
|
||||
this.generatedJavaCcSourceRoot = generatedJavaCcSourceRoot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generate() {
|
||||
Log.info("Running %s", () -> getClass().getSimpleName());
|
||||
|
||||
final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
|
||||
final ClassOrInterfaceDeclaration javaToken = javaTokenCu
|
||||
.getClassByName("JavaToken")
|
||||
.orElseThrow(() -> new AssertionError("Can't find class in java file."));
|
||||
final EnumDeclaration kindEnum = javaToken
|
||||
.findFirst(EnumDeclaration.class, e -> "Kind".equals(e.getNameAsString()))
|
||||
.orElseThrow(() -> new AssertionError("Can't find class in java file."));
|
||||
|
||||
kindEnum.getEntries().clear();
|
||||
annotateGenerated(kindEnum);
|
||||
|
||||
final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class)
|
||||
.orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
|
||||
valueOfSwitch.findAll(SwitchEntry.class).stream()
|
||||
.filter(e -> e.getLabels().isNonEmpty())
|
||||
.forEach(Node::remove);
|
||||
|
||||
final CompilationUnit constantsCu =
|
||||
generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
|
||||
final ClassOrInterfaceDeclaration constants = constantsCu
|
||||
.getInterfaceByName("GeneratedJavaParserConstants")
|
||||
.orElseThrow(() -> new AssertionError("Can't find class in java file."));
|
||||
for (BodyDeclaration<?> member : constants.getMembers()) {
|
||||
member.toFieldDeclaration()
|
||||
.filter(field -> {
|
||||
String javadoc = field.getJavadocComment().get().getContent();
|
||||
return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
|
||||
})
|
||||
.map(field -> field.getVariable(0))
|
||||
.ifPresent(var -> {
|
||||
final String name = var.getNameAsString();
|
||||
final IntegerLiteralExpr kind =
|
||||
var.getInitializer().get().asIntegerLiteralExpr();
|
||||
generateEnumEntry(kindEnum, name, kind);
|
||||
generateValueOfEntry(valueOfSwitch, name, kind);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void generateValueOfEntry(SwitchStmt valueOfSwitch, String name, IntegerLiteralExpr kind) {
|
||||
final SwitchEntry entry = new SwitchEntry(
|
||||
new NodeList<>(kind),
|
||||
SwitchEntry.Type.STATEMENT_GROUP,
|
||||
new NodeList<>(new ReturnStmt(name)),
|
||||
false,
|
||||
null);
|
||||
valueOfSwitch.getEntries().addFirst(entry);
|
||||
}
|
||||
|
||||
private void generateEnumEntry(EnumDeclaration kindEnum, String name, IntegerLiteralExpr kind) {
|
||||
final EnumConstantDeclaration enumEntry = new EnumConstantDeclaration(name);
|
||||
enumEntry.getArguments().add(kind);
|
||||
kindEnum.addEntry(enumEntry);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.quality;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.StaticJavaParser;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.NodeList;
|
||||
import com.github.javaparser.ast.body.CallableDeclaration;
|
||||
import com.github.javaparser.ast.body.ConstructorDeclaration;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.body.Parameter;
|
||||
import com.github.javaparser.ast.expr.AnnotationExpr;
|
||||
import com.github.javaparser.ast.expr.MethodCallExpr;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
|
||||
import com.github.javaparser.ast.stmt.ExpressionStmt;
|
||||
import com.github.javaparser.ast.stmt.Statement;
|
||||
import com.github.javaparser.generator.CompilationUnitGenerator;
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Generator to process annotations {@link com.github.javaparser.quality.NotNull}.
|
||||
*/
|
||||
public class NotNullGenerator extends CompilationUnitGenerator {
|
||||
|
||||
public NotNullGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateCompilationUnit(CompilationUnit compilationUnit) {
|
||||
compilationUnit.findAll(ConstructorDeclaration.class).forEach(this::generateQualityForConstructor);
|
||||
compilationUnit.findAll(MethodDeclaration.class).forEach(this::generateQualityForMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the pre conditions based on the method parameters.
|
||||
* <br>
|
||||
* If parameters are annotated with {@link com.github.javaparser.quality.NotNull} and a {@code null} is
|
||||
* passed, the method should throw an {@link IllegalArgumentException}.
|
||||
* <br>
|
||||
* If annotated with {@link com.github.javaparser.quality.Nullable}, other annotation or none, nothing should be
|
||||
* changed.
|
||||
*
|
||||
* @param methodDeclaration The method declaration to generate.
|
||||
*/
|
||||
protected void generateQualityForMethod(MethodDeclaration methodDeclaration) {
|
||||
methodDeclaration
|
||||
.getBody()
|
||||
.ifPresent(blockStmt ->
|
||||
generateQualityForParameter(methodDeclaration, methodDeclaration.getParameters(), blockStmt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the pre conditions based on the constructor parameters.
|
||||
* <br>
|
||||
* If parameters are annotated with {@link com.github.javaparser.quality.NotNull} and a {@code null} is
|
||||
* passed, the method should throw an {@link IllegalArgumentException}.
|
||||
* <br>
|
||||
* If annotated with {@link com.github.javaparser.quality.Nullable}, other annotation or none, nothing should be
|
||||
* changed.
|
||||
*
|
||||
* @param constructorDeclaration The constructor declaration to generate.
|
||||
*/
|
||||
protected void generateQualityForConstructor(ConstructorDeclaration constructorDeclaration) {
|
||||
generateQualityForParameter(
|
||||
constructorDeclaration, constructorDeclaration.getParameters(), constructorDeclaration.getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the pre conditions for the parameters.
|
||||
* <br>
|
||||
* If parameters are annotated with {@link com.github.javaparser.quality.NotNull} and a {@code null} is
|
||||
* passed, the method should throw an {@link IllegalArgumentException}.
|
||||
* <br>
|
||||
* If annotated with {@link com.github.javaparser.quality.Nullable}, other annotation or none, nothing should be
|
||||
* changed.
|
||||
*
|
||||
* @param callableDeclaration The declaration where the parameters belong.
|
||||
* @param parameters The list of parameters.
|
||||
* @param blockStmt The block where the assertions should be added.
|
||||
*
|
||||
* @param <N> The callable declaration type.
|
||||
*/
|
||||
protected <N extends CallableDeclaration<?>> void generateQualityForParameter(
|
||||
N callableDeclaration, NodeList<Parameter> parameters, BlockStmt blockStmt) {
|
||||
|
||||
List<Statement> assertions = new ArrayList<>();
|
||||
|
||||
for (Parameter parameter : parameters) {
|
||||
Optional<AnnotationExpr> nonNullAnnotation = parameter.getAnnotationByClass(NotNull.class);
|
||||
if (nonNullAnnotation.isPresent()) {
|
||||
assertions.add(createAssertion(parameter));
|
||||
}
|
||||
}
|
||||
|
||||
insertAssertionsInBlock(callableDeclaration, blockStmt, assertions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create assertion for the parameters.
|
||||
*
|
||||
* @param parameter The parameter to create the assertion.
|
||||
*
|
||||
* @return The assertion to be added to the code.
|
||||
*/
|
||||
private Statement createAssertion(Parameter parameter) {
|
||||
|
||||
parameter.tryAddImportToParentCompilationUnit(Preconditions.class);
|
||||
return StaticJavaParser.parseStatement(f(
|
||||
"Preconditions.checkNotNull(%s, \"Parameter %s can't be null.\");",
|
||||
parameter.getNameAsString(), parameter.getNameAsString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the assertions into the block.
|
||||
*
|
||||
* @param callableDeclaration The declaration where the parameters belong.
|
||||
* @param blockStmt The block where the assertions should be added.
|
||||
* @param assertions The list of assertions to be inserted.
|
||||
*
|
||||
* @param <N> The callable declaration type.
|
||||
*/
|
||||
private <N extends CallableDeclaration<?>> void insertAssertionsInBlock(
|
||||
N callableDeclaration, BlockStmt blockStmt, List<Statement> assertions) {
|
||||
|
||||
// If there's nothing to add, just ignore
|
||||
if (assertions.isEmpty()) return;
|
||||
|
||||
int position = 0;
|
||||
NodeList<Statement> statements = blockStmt.getStatements();
|
||||
|
||||
// When the callable is a constructor we must check if is a ExplicitConstructorInvocationStmt.
|
||||
if (callableDeclaration.isConstructorDeclaration()) {
|
||||
Optional<Statement> optionalFirstStatement = statements.getFirst();
|
||||
if (optionalFirstStatement.isPresent()) {
|
||||
|
||||
// Check if the first item is a "super" expr. If it's then we add the assertions after it.
|
||||
Statement firstStatement = optionalFirstStatement.get();
|
||||
if (firstStatement instanceof ExplicitConstructorInvocationStmt) {
|
||||
position = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register assertions
|
||||
for (int i = 0; i < assertions.size(); i++) {
|
||||
Statement assertion = assertions.get(i);
|
||||
|
||||
Optional<? extends Statement> optOldStmt = getSimilarAssertionInBlock(assertion, blockStmt);
|
||||
|
||||
if (optOldStmt.isPresent()) {
|
||||
optOldStmt.get().replace(assertion);
|
||||
} else {
|
||||
blockStmt.addStatement(position + i, assertion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<? extends Statement> getSimilarAssertionInBlock(Statement assertion, BlockStmt blockStmt) {
|
||||
|
||||
MethodCallExpr assertionCall =
|
||||
assertion.asExpressionStmt().getExpression().asMethodCallExpr();
|
||||
List<MethodCallExpr> methodCallExpressions = blockStmt.findAll(MethodCallExpr.class);
|
||||
|
||||
for (MethodCallExpr blockMethodCall : methodCallExpressions) {
|
||||
|
||||
// Check if the method calls name match
|
||||
if (blockMethodCall.getNameAsExpression().equals(assertionCall.getNameAsExpression())
|
||||
&& blockMethodCall.getScope().equals(assertionCall.getScope())
|
||||
&& blockMethodCall.getArguments().size() == 2
|
||||
&& blockMethodCall.getArguments().get(0).equals(assertionCall.getArgument(0))) {
|
||||
return blockMethodCall.findAncestor(ExpressionStmt.class);
|
||||
}
|
||||
}
|
||||
// TODO:
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.utils;
|
||||
|
||||
import com.github.javaparser.ast.type.Type;
|
||||
|
||||
public final class CodeUtils {
|
||||
|
||||
private CodeUtils() {
|
||||
// This constructor is used to hide the public one
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast the value if the current type doesn't match the required type.
|
||||
* <br>
|
||||
* Given the following example:
|
||||
* <code>
|
||||
* int withoutCast = 1;
|
||||
* double withCast = (double) 1;
|
||||
* </code>
|
||||
* The variable withoutCast doesn't need to be casted, since we have int as required type and int as value type.
|
||||
* While in the variable withCast we have double as required type and int as value type.
|
||||
*
|
||||
* @param value The value to be returned.
|
||||
* @param requiredType The expected type to be casted if needed.
|
||||
* @param valueType The type of the value to be returned.
|
||||
*
|
||||
* @return The value casted if needed.
|
||||
*/
|
||||
public static String castValue(String value, Type requiredType, String valueType) {
|
||||
String requiredTypeName = requiredType.asString();
|
||||
|
||||
if (requiredTypeName.equals(valueType)) return value;
|
||||
return String.format("(%s) %s", requiredTypeName, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.CompilationUnitMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SeparatedItemStringBuilder;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's CloneVisitor.
|
||||
*/
|
||||
public class CloneVisitorGenerator extends VisitorGenerator {
|
||||
public CloneVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "CloneVisitor", "Visitable", "Object", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isOptional() && field.isNodeList()) {
|
||||
body.addStatement(f(
|
||||
"NodeList<%s> %s = cloneList(n.%s.orElse(null), arg);",
|
||||
field.getTypeNameGenerified(), field.getName(), getter));
|
||||
} else if (field.isNodeList()) {
|
||||
body.addStatement(f(
|
||||
"NodeList<%s> %s = cloneList(n.%s, arg);",
|
||||
field.getTypeNameGenerified(), field.getName(), getter));
|
||||
} else {
|
||||
body.addStatement(
|
||||
f("%s %s = cloneNode(n.%s, arg);", field.getTypeNameGenerified(), field.getName(), getter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder(
|
||||
f("%s r = new %s(", node.getTypeNameGenerified(), node.getTypeNameGenerified()), ",", ");");
|
||||
builder.append("n.getTokenRange().orElse(null)");
|
||||
for (PropertyMetaModel field : node.getConstructorParameters()) {
|
||||
if ("comment".equals(field.getName())) {
|
||||
continue;
|
||||
}
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
builder.append(field.getName());
|
||||
} else {
|
||||
builder.append(f("n.%s()", field.getGetterMethodName()));
|
||||
}
|
||||
}
|
||||
|
||||
body.addStatement(builder.toString());
|
||||
if (node instanceof CompilationUnitMetaModel) {
|
||||
body.addStatement("n.getStorage().ifPresent(s -> r.setStorage(s.getPath(), s.getEncoding()));");
|
||||
}
|
||||
body.addStatement("r.setComment(comment);");
|
||||
body.addStatement("n.getOrphanComments().stream().map(Comment::clone).forEach(r::addOrphanComment);");
|
||||
body.addStatement("copyData(n, r);");
|
||||
body.addStatement("return r;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's EqualsVisitor.
|
||||
*/
|
||||
public class EqualsVisitorGenerator extends VisitorGenerator {
|
||||
public EqualsVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "EqualsVisitor", "Boolean", "Visitable", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
body.addStatement(f("final %s n2 = (%s) arg;", node.getTypeName(), node.getTypeName()));
|
||||
|
||||
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isNodeList()) {
|
||||
body.addStatement(f("if (!nodesEquals(n.%s, n2.%s)) return false;", getter, getter));
|
||||
} else {
|
||||
body.addStatement(f("if (!nodeEquals(n.%s, n2.%s)) return false;", getter, getter));
|
||||
}
|
||||
} else {
|
||||
body.addStatement(f("if (!objEquals(n.%s, n2.%s)) return false;", getter, getter));
|
||||
}
|
||||
}
|
||||
if (body.getStatements().size() == 1) {
|
||||
// Only the cast line was added, but nothing is using it, so remove it again.
|
||||
body.getStatements().clear();
|
||||
}
|
||||
body.addStatement("return true;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's GenericListVisitorAdapter.
|
||||
*/
|
||||
public class GenericListVisitorAdapterGenerator extends VisitorGenerator {
|
||||
public GenericListVisitorAdapterGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "GenericListVisitorAdapter", "List<R>", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
body.addStatement("List<R> result = new ArrayList<>();");
|
||||
body.addStatement("List<R> tmp;");
|
||||
|
||||
final String resultCheck = "if (tmp != null) result.addAll(tmp);";
|
||||
|
||||
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isOptional()) {
|
||||
body.addStatement(f(
|
||||
"if (n.%s.isPresent()) {" + " tmp = n.%s.get().accept(this, arg);" + " %s" + "}",
|
||||
getter, getter, resultCheck));
|
||||
} else {
|
||||
body.addStatement(f("{ tmp = n.%s.accept(this, arg); %s }", getter, resultCheck));
|
||||
}
|
||||
}
|
||||
}
|
||||
body.addStatement("return result;");
|
||||
Arrays.stream(new Class<?>[] {List.class, ArrayList.class})
|
||||
.filter(c -> compilationUnit.getImports().stream()
|
||||
.noneMatch(i -> c.getName().equals(i.getName().asString())))
|
||||
.forEach(compilationUnit::addImport);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's VoidVisitorAdapter.
|
||||
*/
|
||||
public class GenericVisitorAdapterGenerator extends VisitorGenerator {
|
||||
public GenericVisitorAdapterGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "GenericVisitorAdapter", "R", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
body.addStatement("R result;");
|
||||
|
||||
final String resultCheck = "if (result != null) return result;";
|
||||
|
||||
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isOptional()) {
|
||||
body.addStatement(f(
|
||||
"if (n.%s.isPresent()) {" + " result = n.%s.get().accept(this, arg);" + " %s" + "}",
|
||||
getter, getter, resultCheck));
|
||||
} else {
|
||||
body.addStatement(f("{ result = n.%s.accept(this, arg); %s }", getter, resultCheck));
|
||||
}
|
||||
}
|
||||
}
|
||||
body.addStatement("return null;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's GenericVisitor.
|
||||
*/
|
||||
public class GenericVisitorGenerator extends VisitorGenerator {
|
||||
public GenericVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "GenericVisitor", "R", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(false));
|
||||
|
||||
visitMethod.setBody(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's GenericVisitorWithDefaults.
|
||||
*/
|
||||
public class GenericVisitorWithDefaultsGenerator extends VisitorGenerator {
|
||||
public GenericVisitorWithDefaultsGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "GenericVisitorWithDefaults", "R", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
body.addStatement("return defaultAction(n, arg);");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseStatement;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SeparatedItemStringBuilder;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's HashCodeVisitor.
|
||||
*/
|
||||
public class HashCodeVisitorGenerator extends VisitorGenerator {
|
||||
public HashCodeVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "HashCodeVisitor", "Integer", "Void", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
final BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
final SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("return ", "* 31 +", ";");
|
||||
final List<PropertyMetaModel> propertyMetaModels = node.getAllPropertyMetaModels();
|
||||
if (propertyMetaModels.isEmpty()) {
|
||||
builder.append("0");
|
||||
} else {
|
||||
for (PropertyMetaModel field : propertyMetaModels) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
// Is this field another AST node? Visit it.
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isOptional()) {
|
||||
builder.append("(n.%s.isPresent()? n.%s.get().accept(this, arg):0)", getter, getter);
|
||||
} else {
|
||||
builder.append("(n.%s.accept(this, arg))", getter);
|
||||
}
|
||||
} else {
|
||||
Class<?> type = field.getType();
|
||||
if (type.equals(boolean.class)) {
|
||||
builder.append("(n.%s?1:0)", getter);
|
||||
} else if (type.equals(int.class)) {
|
||||
builder.append("n.%s", getter);
|
||||
} else {
|
||||
builder.append("(n.%s.hashCode())", getter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
body.addStatement(parseStatement(builder.toString()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.expr.BinaryExpr;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SeparatedItemStringBuilder;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ModifierVisitorGenerator extends VisitorGenerator {
|
||||
public ModifierVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "ModifierVisitor", "Visitable", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
// FIXME: Bit of a hacky way to get this fixed order, and then have everything else (note this list is reversed)
|
||||
List<String> order = Arrays.asList(
|
||||
// "comment", "name", "members", "parameters", "name",
|
||||
"modifiers", "annotations");
|
||||
List<PropertyMetaModel> sortedPropertyMetaModels = node.getAllPropertyMetaModels().stream()
|
||||
.sorted(
|
||||
Comparator.comparingInt((PropertyMetaModel o) -> order.indexOf(o.getName()))
|
||||
.reversed()
|
||||
// .thenComparing(PropertyMetaModel::getName)
|
||||
)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
//
|
||||
sortedPropertyMetaModels.forEach(property -> extracted(body, property));
|
||||
|
||||
//
|
||||
if (node.is(BinaryExpr.class)) {
|
||||
body.addStatement("if (left == null) return right;");
|
||||
body.addStatement("if (right == null) return left;");
|
||||
} else {
|
||||
final SeparatedItemStringBuilder collapseCheck =
|
||||
new SeparatedItemStringBuilder("if(", "||", ") return null;");
|
||||
sortedPropertyMetaModels.forEach(property -> {
|
||||
if (property.isRequired() && property.isNode()) {
|
||||
if (property.isNodeList()) {
|
||||
if (property.isNonEmpty()) {
|
||||
collapseCheck.append(f("%s.isEmpty()", property.getName()));
|
||||
}
|
||||
} else {
|
||||
collapseCheck.append(f("%s==null", property.getName()));
|
||||
}
|
||||
}
|
||||
});
|
||||
if (collapseCheck.hasItems()) {
|
||||
body.addStatement(collapseCheck.toString());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
sortedPropertyMetaModels.forEach(property -> {
|
||||
if (property.isNode()) {
|
||||
body.addStatement(f("n.%s(%s);", property.getSetterMethodName(), property.getName()));
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
body.addStatement("return n;");
|
||||
}
|
||||
|
||||
private void extracted(BlockStmt body, PropertyMetaModel property) {
|
||||
if (property.isNode()) {
|
||||
if (property.isNodeList()) {
|
||||
body.addStatement(f(
|
||||
"NodeList<%s> %s = modifyList(n.%s(), arg);",
|
||||
property.getTypeNameGenerified(), property.getName(), property.getGetterMethodName()));
|
||||
} else if (property.isOptional()) {
|
||||
body.addStatement(f(
|
||||
"%s %s = n.%s().map(s -> (%s) s.accept(this, arg)).orElse(null);",
|
||||
property.getTypeNameGenerified(),
|
||||
property.getName(),
|
||||
property.getGetterMethodName(),
|
||||
property.getTypeNameGenerified()));
|
||||
} else {
|
||||
body.addStatement(f(
|
||||
"%s %s = (%s) n.%s().accept(this, arg);",
|
||||
property.getTypeNameGenerified(),
|
||||
property.getName(),
|
||||
property.getTypeNameGenerified(),
|
||||
property.getGetterMethodName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
public class NoCommentEqualsVisitorGenerator extends VisitorGenerator {
|
||||
|
||||
public NoCommentEqualsVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "NoCommentEqualsVisitor", "Boolean", "Visitable", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
if (!(node.equals(JavaParserMetaModel.lineCommentMetaModel)
|
||||
|| node.equals(JavaParserMetaModel.blockCommentMetaModel)
|
||||
|| node.equals(JavaParserMetaModel.javadocCommentMetaModel))) {
|
||||
|
||||
body.addStatement(f("final %s n2 = (%s) arg;", node.getTypeName(), node.getTypeName()));
|
||||
|
||||
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.equals(JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel)) continue;
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isNodeList()) {
|
||||
body.addStatement(f("if (!nodesEquals(n.%s, n2.%s)) return false;", getter, getter));
|
||||
} else {
|
||||
body.addStatement(f("if (!nodeEquals(n.%s, n2.%s)) return false;", getter, getter));
|
||||
}
|
||||
} else {
|
||||
body.addStatement(f("if (!objEquals(n.%s, n2.%s)) return false;", getter, getter));
|
||||
}
|
||||
}
|
||||
if (body.getStatements().size() == 1) {
|
||||
// Only the cast line was added, but nothing is using it, so remove it again.
|
||||
body.getStatements().clear();
|
||||
}
|
||||
}
|
||||
body.addStatement("return true;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.StaticJavaParser.parseStatement;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.JavaParserMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SeparatedItemStringBuilder;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.util.List;
|
||||
|
||||
public class NoCommentHashCodeVisitorGenerator extends VisitorGenerator {
|
||||
|
||||
public NoCommentHashCodeVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "NoCommentHashCodeVisitor", "Integer", "Void", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
final BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
final SeparatedItemStringBuilder builder = new SeparatedItemStringBuilder("return ", "* 31 +", ";");
|
||||
final List<PropertyMetaModel> propertyMetaModels = node.getAllPropertyMetaModels();
|
||||
if (node.equals(JavaParserMetaModel.lineCommentMetaModel)
|
||||
|| node.equals(JavaParserMetaModel.blockCommentMetaModel)
|
||||
|| node.equals(JavaParserMetaModel.javadocCommentMetaModel)
|
||||
|| propertyMetaModels.isEmpty()) {
|
||||
builder.append("0");
|
||||
} else {
|
||||
for (PropertyMetaModel field : propertyMetaModels) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.equals(JavaParserMetaModel.nodeMetaModel.commentPropertyMetaModel)) {
|
||||
if (propertyMetaModels.size() == 1) {
|
||||
builder.append("0");
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// Is this field another AST node? Visit it.
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isOptional()) {
|
||||
builder.append("(n.%s.isPresent()? n.%s.get().accept(this, arg):0)", getter, getter);
|
||||
} else {
|
||||
builder.append("(n.%s.accept(this, arg))", getter);
|
||||
}
|
||||
} else {
|
||||
Class<?> type = field.getType();
|
||||
if (type.equals(boolean.class)) {
|
||||
builder.append("(n.%s?1:0)", getter);
|
||||
} else if (type.equals(int.class)) {
|
||||
builder.append("n.%s", getter);
|
||||
} else {
|
||||
builder.append("(n.%s.hashCode())", getter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
body.addStatement(parseStatement(builder.toString()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's ObjectIdentityEqualsVisitor.
|
||||
*/
|
||||
public class ObjectIdentityEqualsVisitorGenerator extends VisitorGenerator {
|
||||
public ObjectIdentityEqualsVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(
|
||||
sourceRoot,
|
||||
"com.github.javaparser.ast.visitor",
|
||||
"ObjectIdentityEqualsVisitor",
|
||||
"Boolean",
|
||||
"Visitable",
|
||||
true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
body.addStatement("return n == arg;");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's ObjectIdentityHashCodeVisitor.
|
||||
*/
|
||||
public class ObjectIdentityHashCodeVisitorGenerator extends VisitorGenerator {
|
||||
public ObjectIdentityHashCodeVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(
|
||||
sourceRoot,
|
||||
"com.github.javaparser.ast.visitor",
|
||||
"ObjectIdentityHashCodeVisitor",
|
||||
"Integer",
|
||||
"Void",
|
||||
true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
final BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
body.addStatement("return n.hashCode();");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import static com.github.javaparser.utils.CodeGenerationUtils.f;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.metamodel.PropertyMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's VoidVisitorAdapter.
|
||||
*/
|
||||
public class VoidVisitorAdapterGenerator extends VisitorGenerator {
|
||||
public VoidVisitorAdapterGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "VoidVisitorAdapter", "void", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
for (PropertyMetaModel field : node.getAllPropertyMetaModels()) {
|
||||
final String getter = field.getGetterMethodName() + "()";
|
||||
if (field.getNodeReference().isPresent()) {
|
||||
if (field.isOptional() && field.isNodeList()) {
|
||||
body.addStatement(f("n.%s.ifPresent( l -> l.forEach( v -> v.accept(this, arg)));", getter));
|
||||
} else if (field.isOptional()) {
|
||||
body.addStatement(f("n.%s.ifPresent(l -> l.accept(this, arg));", getter));
|
||||
} else if (field.isNodeList()) {
|
||||
body.addStatement(f("n.%s.forEach(p -> p.accept(this, arg));", getter));
|
||||
} else {
|
||||
body.addStatement(f("n.%s.accept(this, arg);", getter));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's VoidVisitor.
|
||||
*/
|
||||
public class VoidVisitorGenerator extends VisitorGenerator {
|
||||
public VoidVisitorGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "VoidVisitor", "void", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(false));
|
||||
|
||||
visitMethod.setBody(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.visitor;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.stmt.BlockStmt;
|
||||
import com.github.javaparser.generator.VisitorGenerator;
|
||||
import com.github.javaparser.metamodel.BaseNodeMetaModel;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
|
||||
/**
|
||||
* Generates JavaParser's VoidVisitorWithDefaults.
|
||||
*/
|
||||
public class VoidVisitorWithDefaultsGenerator extends VisitorGenerator {
|
||||
public VoidVisitorWithDefaultsGenerator(SourceRoot sourceRoot) {
|
||||
super(sourceRoot, "com.github.javaparser.ast.visitor", "VoidVisitorWithDefaults", "void", "A", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void generateVisitMethodBody(
|
||||
BaseNodeMetaModel node, MethodDeclaration visitMethod, CompilationUnit compilationUnit) {
|
||||
visitMethod.getParameters().forEach(p -> p.setFinal(true));
|
||||
|
||||
BlockStmt body = visitMethod.getBody().get();
|
||||
body.getStatements().clear();
|
||||
|
||||
body.addStatement("defaultAction(n, arg);");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.quality;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.printer.DefaultPrettyPrinter;
|
||||
import com.github.javaparser.utils.SourceRoot;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class NotNullGeneratorTest {
|
||||
|
||||
@Test
|
||||
void testExecutionOfGenerator() throws Exception {
|
||||
|
||||
// Setup the
|
||||
String resourcesFolderPath = getClass().getCanonicalName().replace(".", File.separator);
|
||||
|
||||
String basePath = Paths.get("src", "test", "resources").toString();
|
||||
Path originalFile = Paths.get(basePath, resourcesFolderPath, "original");
|
||||
Path expectedFile = Paths.get(basePath, resourcesFolderPath, "expected");
|
||||
|
||||
SourceRoot originalSources = new SourceRoot(originalFile);
|
||||
SourceRoot expectedSources = new SourceRoot(expectedFile);
|
||||
expectedSources.tryToParse();
|
||||
|
||||
// Generate the information
|
||||
new NotNullGenerator(originalSources).generate();
|
||||
|
||||
List<CompilationUnit> editedSourceCus = originalSources.getCompilationUnits();
|
||||
List<CompilationUnit> expectedSourcesCus = expectedSources.getCompilationUnits();
|
||||
assertEquals(expectedSourcesCus.size(), editedSourceCus.size());
|
||||
|
||||
// Check if all the files match the expected result
|
||||
for (int i = 0; i < editedSourceCus.size(); i++) {
|
||||
|
||||
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
|
||||
String expectedCode = printer.print(expectedSourcesCus.get(i));
|
||||
String editedCode = printer.print(editedSourceCus.get(i));
|
||||
|
||||
if (!expectedCode.equals(editedCode)) {
|
||||
System.out.println("Expected:");
|
||||
System.out.println("####");
|
||||
System.out.println(expectedSourcesCus.get(i));
|
||||
System.out.println("####");
|
||||
System.out.println("Actual:");
|
||||
System.out.println("####");
|
||||
System.out.println(editedSourceCus.get(i));
|
||||
System.out.println("####");
|
||||
fail("Actual code doesn't match with the expected code.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2024 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.generator.core.utils;
|
||||
|
||||
import static com.github.javaparser.generator.core.utils.CodeUtils.castValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.github.javaparser.StaticJavaParser;
|
||||
import com.github.javaparser.ast.type.PrimitiveType;
|
||||
import com.github.javaparser.ast.type.Type;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CodeUtilsTest {
|
||||
|
||||
private static final String RETURN_VALUE = "this";
|
||||
|
||||
@Test
|
||||
void castReturnValue_whenAValueMatchesTheExpectedTypeNoCastIsNeeded() {
|
||||
Type returnType = PrimitiveType.booleanType();
|
||||
Type valueType = PrimitiveType.booleanType();
|
||||
|
||||
assertEquals(RETURN_VALUE, castValue(RETURN_VALUE, returnType, valueType.asString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void castReturnValue_whenAValueIsNotAssignedByReturnShouldBeCasted() {
|
||||
Type returnType = StaticJavaParser.parseType("String");
|
||||
Type valueType = StaticJavaParser.parseType("Object");
|
||||
|
||||
assertEquals(
|
||||
String.format("(%s) %s", returnType, RETURN_VALUE),
|
||||
castValue(RETURN_VALUE, returnType, valueType.asString()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.example;
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Nullable;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
|
||||
class ConstructorParameterTest {
|
||||
|
||||
private final String a;
|
||||
|
||||
private final String b;
|
||||
|
||||
private final String c;
|
||||
|
||||
public ConstructorParameterTest(@NotNull String notNullString, @Nullable String nullableString, String otherString) {
|
||||
Preconditions.checkNotNull(notNullString, "Parameter notNullString can't be null.");
|
||||
this.a = notNullString;
|
||||
this.b = nullableString;
|
||||
this.c = otherString;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.
|
||||
*/
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
|
||||
class A {
|
||||
|
||||
public A(String a) {
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
|
||||
public B(@NotNull String c) {
|
||||
super("ok");
|
||||
Preconditions.checkNotNull(c, "Parameter c can't be null.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.
|
||||
*/
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
|
||||
class A {
|
||||
|
||||
public void method(@NotNull String notNull, @NotNull String secondNotNull) {
|
||||
Preconditions.checkNotNull(notNull, "Parameter notNull can't be null.");
|
||||
Preconditions.checkNotNull(secondNotNull, "Parameter secondNotNull can't be null.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.
|
||||
*/
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
|
||||
class A {
|
||||
|
||||
public void method(@NotNull String notNull) {
|
||||
Preconditions.checkNotNull(notNull, "Parameter notNull can't be null.");
|
||||
}
|
||||
|
||||
public void method(int age) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.example;
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Nullable;
|
||||
|
||||
class ConstructorParameterTest {
|
||||
|
||||
private final String a;
|
||||
private final String b;
|
||||
private final String c;
|
||||
|
||||
public ConstructorParameterTest(@NotNull String notNullString, @Nullable String nullableString,
|
||||
String otherString) {
|
||||
this.a = notNullString;
|
||||
this.b = nullableString;
|
||||
this.c = otherString;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.
|
||||
*/
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
|
||||
class A {
|
||||
public A(String a) {}
|
||||
}
|
||||
|
||||
class B {
|
||||
|
||||
public B(@NotNull String c) {
|
||||
super("ok");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.
|
||||
*/
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
|
||||
class A {
|
||||
|
||||
public void method(@NotNull String notNull, @NotNull String secondNotNull) {
|
||||
Preconditions.checkNotNull(notNull, "This was aan old message.");
|
||||
Preconditions.checkNotNull(secondNotNull, "Parameter secondNotNull can't be null.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
|
||||
* Copyright (C) 2011, 2013-2021 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.
|
||||
*/
|
||||
|
||||
import com.github.javaparser.quality.NotNull;
|
||||
import com.github.javaparser.quality.Preconditions;
|
||||
|
||||
class A {
|
||||
|
||||
public void method(@NotNull String notNull) {}
|
||||
|
||||
public void method(int age) {}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user