diff --git a/pom.xml b/pom.xml index 88faf62fdb8..198ac0cf164 100644 --- a/pom.xml +++ b/pom.xml @@ -211,6 +211,7 @@ true spoon/support/visitor/clone/CloneBuilder.java + spoon/support/visitor/clone/CloneVisitor.java spoon/reflect/visitor/CtBiScannerDefault.java spoon/support/visitor/equals/EqualsVisitor.java spoon/support/visitor/replace/ReplacementVisitor.java diff --git a/spoon-pom/pom.xml b/spoon-pom/pom.xml index 6cf1e266838..b5e1c1902ed 100644 --- a/spoon-pom/pom.xml +++ b/spoon-pom/pom.xml @@ -282,6 +282,9 @@ src/main/java/** src/test/java/spoon/generating/clone/* + src/test/java/spoon/generating/meta/* + src/test/java/spoon/generating/replace/* + src/test/java/spoon/generating/scanner/* diff --git a/src/main/java/spoon/reflect/meta/impl/ModelRoleHandlers.java b/src/main/java/spoon/reflect/meta/impl/ModelRoleHandlers.java index 5d4433a6c0f..d466c33c267 100644 --- a/src/main/java/spoon/reflect/meta/impl/ModelRoleHandlers.java +++ b/src/main/java/spoon/reflect/meta/impl/ModelRoleHandlers.java @@ -118,11 +118,6 @@ * Contains implementations of {@link RoleHandler}s for all {@link CtRole}s of all model elements */ class ModelRoleHandlers { - private ModelRoleHandlers() { - } - - static final RoleHandler[] roleHandlers = new RoleHandler[]{ new CtTypeAccess_ACCESSED_TYPE_RoleHandler(), new CtClass_ANNONYMOUS_EXECUTABLE_RoleHandler(), new CtElement_ANNOTATION_RoleHandler(), new CtAnnotation_ANNOTATION_TYPE_RoleHandler(), new CtAbstractInvocation_ARGUMENT_RoleHandler(), new CtExecutableReference_ARGUMENT_TYPE_RoleHandler(), new CtAssignment_ASSIGNED_RoleHandler(), new CtRHSReceiver_ASSIGNMENT_RoleHandler(), new CtBodyHolder_BODY_RoleHandler(), new CtSynchronized_BODY_RoleHandler(), new CtIntersectionTypeReference_BOUND_RoleHandler(), new CtWildcardReference_BOUNDING_TYPE_RoleHandler(), new CtAbstractSwitch_CASE_RoleHandler(), new CtCase_CASE_KIND_RoleHandler(), new CtExpression_CAST_RoleHandler(), new CtTry_CATCH_RoleHandler(), new CtElement_COMMENT_RoleHandler(), new CtComment_COMMENT_CONTENT_RoleHandler(), new CtJavaDocTag_COMMENT_CONTENT_RoleHandler(), new CtJavaDoc_COMMENT_TAG_RoleHandler(), new CtComment_COMMENT_TYPE_RoleHandler(), new CtConstructor_COMPACT_CONSTRUCTOR_RoleHandler(), new CtAssert_CONDITION_RoleHandler(), new CtCase_CONDITION_RoleHandler(), new CtConditional_CONDITION_RoleHandler(), new CtIf_CONDITION_RoleHandler(), new CtClass_CONSTRUCTOR_RoleHandler(), new CtPackage_CONTAINED_TYPE_RoleHandler(), new CtCompilationUnit_DECLARED_IMPORT_RoleHandler(), new CtCompilationUnit_DECLARED_MODULE_RoleHandler(), new CtCompilationUnit_DECLARED_MODULE_REF_RoleHandler(), new CtCompilationUnit_DECLARED_TYPE_RoleHandler(), new CtCompilationUnit_DECLARED_TYPE_REF_RoleHandler(), new CtExecutableReference_DECLARING_TYPE_RoleHandler(), new CtFieldReference_DECLARING_TYPE_RoleHandler(), new CtTypeReference_DECLARING_TYPE_RoleHandler(), new CtAnnotationMethod_DEFAULT_EXPRESSION_RoleHandler(), new CtCase_DEFAULT_EXPRESSION_RoleHandler(), new CtVariable_DEFAULT_EXPRESSION_RoleHandler(), new CtNewArray_DIMENSION_RoleHandler(), new CtJavaDocTag_DOCUMENTATION_TYPE_RoleHandler(), new CtJavaDocTag_DOCUMENTATION_TYPE_REALNAME_RoleHandler(), new CtConditional_ELSE_RoleHandler(), new CtIf_ELSE_RoleHandler(), new CtModifiable_EMODIFIER_RoleHandler(), new CtAbstractInvocation_EXECUTABLE_REF_RoleHandler(), new CtExecutableReferenceExpression_EXECUTABLE_REF_RoleHandler(), new CtModule_EXPORTED_PACKAGE_RoleHandler(), new CtAbstractSwitch_EXPRESSION_RoleHandler(), new CtArrayAccess_EXPRESSION_RoleHandler(), new CtAssert_EXPRESSION_RoleHandler(), new CtCase_EXPRESSION_RoleHandler(), new CtDo_EXPRESSION_RoleHandler(), new CtFor_EXPRESSION_RoleHandler(), new CtForEach_EXPRESSION_RoleHandler(), new CtLambda_EXPRESSION_RoleHandler(), new CtNewArray_EXPRESSION_RoleHandler(), new CtReturn_EXPRESSION_RoleHandler(), new CtSynchronized_EXPRESSION_RoleHandler(), new CtThrow_EXPRESSION_RoleHandler(), new CtUnaryOperator_EXPRESSION_RoleHandler(), new CtWhile_EXPRESSION_RoleHandler(), new CtYieldStatement_EXPRESSION_RoleHandler(), new CtType_FIELD_RoleHandler(), new CtTry_FINALIZER_RoleHandler(), new CtForEach_FOREACH_VARIABLE_RoleHandler(), new CtFor_FOR_INIT_RoleHandler(), new CtFor_FOR_UPDATE_RoleHandler(), new CtProvidedService_IMPLEMENTATION_TYPE_RoleHandler(), new CtImport_IMPORT_REFERENCE_RoleHandler(), new CtType_INTERFACE_RoleHandler(), new CtTypeInformation_INTERFACE_RoleHandler(), new CtMethod_IS_DEFAULT_RoleHandler(), new CtFieldReference_IS_FINAL_RoleHandler(), new CtElement_IS_IMPLICIT_RoleHandler(), new CtLocalVariable_IS_INFERRED_RoleHandler(), new CtParameter_IS_INFERRED_RoleHandler(), new CtShadowable_IS_SHADOW_RoleHandler(), new CtExecutableReference_IS_STATIC_RoleHandler(), new CtFieldReference_IS_STATIC_RoleHandler(), new CtWildcardReference_IS_UPPER_RoleHandler(), new CtParameter_IS_VARARGS_RoleHandler(), new CtJavaDocTag_JAVADOC_TAG_VALUE_RoleHandler(), new CtStatement_LABEL_RoleHandler(), new CtBinaryOperator_LEFT_OPERAND_RoleHandler(), new CtLiteral_LITERAL_BASE_RoleHandler(), new CtType_METHOD_RoleHandler(), new CtModifiable_MODIFIER_RoleHandler(), new CtModule_MODIFIER_RoleHandler(), new CtModuleRequirement_MODIFIER_RoleHandler(), new CtTypeInformation_MODIFIER_RoleHandler(), new CtModule_MODULE_DIRECTIVE_RoleHandler(), new CtModuleRequirement_MODULE_REF_RoleHandler(), new CtPackageExport_MODULE_REF_RoleHandler(), new CtMultiTypedElement_MULTI_TYPE_RoleHandler(), new CtNamedElement_NAME_RoleHandler(), new CtReference_NAME_RoleHandler(), new CtNewClass_NESTED_TYPE_RoleHandler(), new CtType_NESTED_TYPE_RoleHandler(), new CtModule_OPENED_PACKAGE_RoleHandler(), new CtPackageExport_OPENED_PACKAGE_RoleHandler(), new CtBinaryOperator_OPERATOR_KIND_RoleHandler(), new CtOperatorAssignment_OPERATOR_KIND_RoleHandler(), new CtUnaryOperator_OPERATOR_KIND_RoleHandler(), new CtCompilationUnit_PACKAGE_DECLARATION_RoleHandler(), new CtPackageDeclaration_PACKAGE_REF_RoleHandler(), new CtPackageExport_PACKAGE_REF_RoleHandler(), new CtTypeReference_PACKAGE_REF_RoleHandler(), new CtCatch_PARAMETER_RoleHandler(), new CtExecutable_PARAMETER_RoleHandler(), new CtCasePattern_PATTERN_RoleHandler(), new CtRecordPattern_PATTERN_RoleHandler(), new CtSealable_PERMITTED_TYPE_RoleHandler(), new CtElement_POSITION_RoleHandler(), new CtModule_PROVIDED_SERVICE_RoleHandler(), new CtExecutable_RECEIVER_PARAMETER_RoleHandler(), new CtRecord_RECORD_COMPONENT_RoleHandler(), new CtModule_REQUIRED_MODULE_RoleHandler(), new CtBinaryOperator_RIGHT_OPERAND_RoleHandler(), new CtModule_SERVICE_TYPE_RoleHandler(), new CtProvidedService_SERVICE_TYPE_RoleHandler(), new CtUsedService_SERVICE_TYPE_RoleHandler(), new CtCodeSnippet_SNIPPET_RoleHandler(), new CtStatementList_STATEMENT_RoleHandler(), new CtModule_SUB_PACKAGE_RoleHandler(), new CtPackage_SUB_PACKAGE_RoleHandler(), new CtType_SUPER_TYPE_RoleHandler(), new CtTypeInformation_SUPER_TYPE_RoleHandler(), new CtTargetedExpression_TARGET_RoleHandler(), new CtLabelledFlowBreak_TARGET_LABEL_RoleHandler(), new CtConditional_THEN_RoleHandler(), new CtIf_THEN_RoleHandler(), new CtExecutable_THROWN_RoleHandler(), new CtTryWithResource_TRY_RESOURCE_RoleHandler(), new CtArrayTypeReference_TYPE_RoleHandler(), new CtExecutableReference_TYPE_RoleHandler(), new CtTypedElement_TYPE_RoleHandler(), new CtVariableReference_TYPE_RoleHandler(), new CtActualTypeContainer_TYPE_ARGUMENT_RoleHandler(), new CtType_TYPE_MEMBER_RoleHandler(), new CtFormalTypeDeclarer_TYPE_PARAMETER_RoleHandler(), new CtRecordPattern_TYPE_REF_RoleHandler(), new CtTypeMemberWildcardImportReference_TYPE_REF_RoleHandler(), new CtAnnotation_VALUE_RoleHandler(), new CtEnum_VALUE_RoleHandler(), new CtLiteral_VALUE_RoleHandler(), new CtTextBlock_VALUE_RoleHandler(), new CtTypePattern_VARIABLE_RoleHandler(), new CtVariableAccess_VARIABLE_RoleHandler() }; - static class CtVariableAccess_VARIABLE_RoleHandler extends SingleHandler> { private CtVariableAccess_VARIABLE_RoleHandler() { super(CtRole.VARIABLE, CtVariableAccess.class, CtVariableReference.class); @@ -2625,4 +2620,9 @@ public void setValue(T element, U value) { castTarget(element).setAccessedType(castValue(value)); } } + + private ModelRoleHandlers() { + } + + static final RoleHandler[] roleHandlers = new RoleHandler[]{ new CtTypeAccess_ACCESSED_TYPE_RoleHandler(), new CtClass_ANNONYMOUS_EXECUTABLE_RoleHandler(), new CtElement_ANNOTATION_RoleHandler(), new CtAnnotation_ANNOTATION_TYPE_RoleHandler(), new CtAbstractInvocation_ARGUMENT_RoleHandler(), new CtExecutableReference_ARGUMENT_TYPE_RoleHandler(), new CtAssignment_ASSIGNED_RoleHandler(), new CtRHSReceiver_ASSIGNMENT_RoleHandler(), new CtBodyHolder_BODY_RoleHandler(), new CtSynchronized_BODY_RoleHandler(), new CtIntersectionTypeReference_BOUND_RoleHandler(), new CtWildcardReference_BOUNDING_TYPE_RoleHandler(), new CtAbstractSwitch_CASE_RoleHandler(), new CtCase_CASE_KIND_RoleHandler(), new CtExpression_CAST_RoleHandler(), new CtTry_CATCH_RoleHandler(), new CtElement_COMMENT_RoleHandler(), new CtComment_COMMENT_CONTENT_RoleHandler(), new CtJavaDocTag_COMMENT_CONTENT_RoleHandler(), new CtJavaDoc_COMMENT_TAG_RoleHandler(), new CtComment_COMMENT_TYPE_RoleHandler(), new CtConstructor_COMPACT_CONSTRUCTOR_RoleHandler(), new CtAssert_CONDITION_RoleHandler(), new CtCase_CONDITION_RoleHandler(), new CtConditional_CONDITION_RoleHandler(), new CtIf_CONDITION_RoleHandler(), new CtClass_CONSTRUCTOR_RoleHandler(), new CtPackage_CONTAINED_TYPE_RoleHandler(), new CtCompilationUnit_DECLARED_IMPORT_RoleHandler(), new CtCompilationUnit_DECLARED_MODULE_RoleHandler(), new CtCompilationUnit_DECLARED_MODULE_REF_RoleHandler(), new CtCompilationUnit_DECLARED_TYPE_RoleHandler(), new CtCompilationUnit_DECLARED_TYPE_REF_RoleHandler(), new CtExecutableReference_DECLARING_TYPE_RoleHandler(), new CtFieldReference_DECLARING_TYPE_RoleHandler(), new CtTypeReference_DECLARING_TYPE_RoleHandler(), new CtAnnotationMethod_DEFAULT_EXPRESSION_RoleHandler(), new CtCase_DEFAULT_EXPRESSION_RoleHandler(), new CtVariable_DEFAULT_EXPRESSION_RoleHandler(), new CtNewArray_DIMENSION_RoleHandler(), new CtJavaDocTag_DOCUMENTATION_TYPE_RoleHandler(), new CtJavaDocTag_DOCUMENTATION_TYPE_REALNAME_RoleHandler(), new CtConditional_ELSE_RoleHandler(), new CtIf_ELSE_RoleHandler(), new CtModifiable_EMODIFIER_RoleHandler(), new CtAbstractInvocation_EXECUTABLE_REF_RoleHandler(), new CtExecutableReferenceExpression_EXECUTABLE_REF_RoleHandler(), new CtModule_EXPORTED_PACKAGE_RoleHandler(), new CtAbstractSwitch_EXPRESSION_RoleHandler(), new CtArrayAccess_EXPRESSION_RoleHandler(), new CtAssert_EXPRESSION_RoleHandler(), new CtCase_EXPRESSION_RoleHandler(), new CtDo_EXPRESSION_RoleHandler(), new CtFor_EXPRESSION_RoleHandler(), new CtForEach_EXPRESSION_RoleHandler(), new CtLambda_EXPRESSION_RoleHandler(), new CtNewArray_EXPRESSION_RoleHandler(), new CtReturn_EXPRESSION_RoleHandler(), new CtSynchronized_EXPRESSION_RoleHandler(), new CtThrow_EXPRESSION_RoleHandler(), new CtUnaryOperator_EXPRESSION_RoleHandler(), new CtWhile_EXPRESSION_RoleHandler(), new CtYieldStatement_EXPRESSION_RoleHandler(), new CtType_FIELD_RoleHandler(), new CtTry_FINALIZER_RoleHandler(), new CtForEach_FOREACH_VARIABLE_RoleHandler(), new CtFor_FOR_INIT_RoleHandler(), new CtFor_FOR_UPDATE_RoleHandler(), new CtProvidedService_IMPLEMENTATION_TYPE_RoleHandler(), new CtImport_IMPORT_REFERENCE_RoleHandler(), new CtType_INTERFACE_RoleHandler(), new CtTypeInformation_INTERFACE_RoleHandler(), new CtMethod_IS_DEFAULT_RoleHandler(), new CtFieldReference_IS_FINAL_RoleHandler(), new CtElement_IS_IMPLICIT_RoleHandler(), new CtLocalVariable_IS_INFERRED_RoleHandler(), new CtParameter_IS_INFERRED_RoleHandler(), new CtShadowable_IS_SHADOW_RoleHandler(), new CtExecutableReference_IS_STATIC_RoleHandler(), new CtFieldReference_IS_STATIC_RoleHandler(), new CtWildcardReference_IS_UPPER_RoleHandler(), new CtParameter_IS_VARARGS_RoleHandler(), new CtJavaDocTag_JAVADOC_TAG_VALUE_RoleHandler(), new CtStatement_LABEL_RoleHandler(), new CtBinaryOperator_LEFT_OPERAND_RoleHandler(), new CtLiteral_LITERAL_BASE_RoleHandler(), new CtType_METHOD_RoleHandler(), new CtModifiable_MODIFIER_RoleHandler(), new CtModule_MODIFIER_RoleHandler(), new CtModuleRequirement_MODIFIER_RoleHandler(), new CtTypeInformation_MODIFIER_RoleHandler(), new CtModule_MODULE_DIRECTIVE_RoleHandler(), new CtModuleRequirement_MODULE_REF_RoleHandler(), new CtPackageExport_MODULE_REF_RoleHandler(), new CtMultiTypedElement_MULTI_TYPE_RoleHandler(), new CtNamedElement_NAME_RoleHandler(), new CtReference_NAME_RoleHandler(), new CtNewClass_NESTED_TYPE_RoleHandler(), new CtType_NESTED_TYPE_RoleHandler(), new CtModule_OPENED_PACKAGE_RoleHandler(), new CtPackageExport_OPENED_PACKAGE_RoleHandler(), new CtBinaryOperator_OPERATOR_KIND_RoleHandler(), new CtOperatorAssignment_OPERATOR_KIND_RoleHandler(), new CtUnaryOperator_OPERATOR_KIND_RoleHandler(), new CtCompilationUnit_PACKAGE_DECLARATION_RoleHandler(), new CtPackageDeclaration_PACKAGE_REF_RoleHandler(), new CtPackageExport_PACKAGE_REF_RoleHandler(), new CtTypeReference_PACKAGE_REF_RoleHandler(), new CtCatch_PARAMETER_RoleHandler(), new CtExecutable_PARAMETER_RoleHandler(), new CtCasePattern_PATTERN_RoleHandler(), new CtRecordPattern_PATTERN_RoleHandler(), new CtSealable_PERMITTED_TYPE_RoleHandler(), new CtElement_POSITION_RoleHandler(), new CtModule_PROVIDED_SERVICE_RoleHandler(), new CtExecutable_RECEIVER_PARAMETER_RoleHandler(), new CtRecord_RECORD_COMPONENT_RoleHandler(), new CtModule_REQUIRED_MODULE_RoleHandler(), new CtBinaryOperator_RIGHT_OPERAND_RoleHandler(), new CtModule_SERVICE_TYPE_RoleHandler(), new CtProvidedService_SERVICE_TYPE_RoleHandler(), new CtUsedService_SERVICE_TYPE_RoleHandler(), new CtCodeSnippet_SNIPPET_RoleHandler(), new CtStatementList_STATEMENT_RoleHandler(), new CtModule_SUB_PACKAGE_RoleHandler(), new CtPackage_SUB_PACKAGE_RoleHandler(), new CtType_SUPER_TYPE_RoleHandler(), new CtTypeInformation_SUPER_TYPE_RoleHandler(), new CtTargetedExpression_TARGET_RoleHandler(), new CtLabelledFlowBreak_TARGET_LABEL_RoleHandler(), new CtConditional_THEN_RoleHandler(), new CtIf_THEN_RoleHandler(), new CtExecutable_THROWN_RoleHandler(), new CtTryWithResource_TRY_RESOURCE_RoleHandler(), new CtArrayTypeReference_TYPE_RoleHandler(), new CtExecutableReference_TYPE_RoleHandler(), new CtTypedElement_TYPE_RoleHandler(), new CtVariableReference_TYPE_RoleHandler(), new CtActualTypeContainer_TYPE_ARGUMENT_RoleHandler(), new CtType_TYPE_MEMBER_RoleHandler(), new CtFormalTypeDeclarer_TYPE_PARAMETER_RoleHandler(), new CtRecordPattern_TYPE_REF_RoleHandler(), new CtTypeMemberWildcardImportReference_TYPE_REF_RoleHandler(), new CtAnnotation_VALUE_RoleHandler(), new CtEnum_VALUE_RoleHandler(), new CtLiteral_VALUE_RoleHandler(), new CtTextBlock_VALUE_RoleHandler(), new CtTypePattern_VARIABLE_RoleHandler(), new CtVariableAccess_VARIABLE_RoleHandler() }; } diff --git a/src/main/java/spoon/reflect/visitor/CommentHelper.java b/src/main/java/spoon/reflect/visitor/CommentHelper.java index d88f6d7a924..4e6bbb4a104 100644 --- a/src/main/java/spoon/reflect/visitor/CommentHelper.java +++ b/src/main/java/spoon/reflect/visitor/CommentHelper.java @@ -9,6 +9,7 @@ import java.util.Collection; import java.util.function.Function; +import java.util.function.UnaryOperator; import java.util.stream.Stream; import spoon.reflect.code.CtComment; import spoon.reflect.code.CtJavaDoc; @@ -49,17 +50,31 @@ static void printComment(PrinterHelper printer, CtComment comment) { printer.write(DefaultJavaPrettyPrinter.INLINE_COMMENT_START); break; case BLOCK: - printer.write(DefaultJavaPrettyPrinter.BLOCK_COMMENT_START); + String commentStart = DefaultJavaPrettyPrinter.BLOCK_COMMENT_START; + if (printer.prefixBlockComments) { + commentStart = commentStart.stripTrailing(); + } + printer.write(commentStart); + if (printer.prefixBlockComments) { + printer.writeln(); + } break; } // content switch (commentType) { - case INLINE: - printer.write(content); - break; - default: + case INLINE -> printer.write(content); + case FILE, BLOCK -> { + UnaryOperator op; + if (printer.prefixBlockComments) { + op = s -> s.isEmpty() ? " *" : " * " + s; + } else { + op = s -> s; + } + printCommentContent(printer, comment, op); + } + case JAVADOC -> // per line suffix - printCommentContent(printer, comment, s -> { return (" * " + s).replaceAll(" *$", ""); }); + printCommentContent(printer, comment, s -> (" * " + s).replaceAll(" *$", "")); } // suffix switch (commentType) { @@ -81,7 +96,7 @@ static void printCommentContent(PrinterHelper printer, CtComment comment, Functi content.lines().forEach(line -> { if (commentType == CtComment.CommentType.BLOCK) { - printer.write(line); + printer.write(transfo.apply(line)); if (hasMoreThanOneElement(content.lines())) { printer.write(CtComment.LINE_SEPARATOR); } diff --git a/src/main/java/spoon/reflect/visitor/PrinterHelper.java b/src/main/java/spoon/reflect/visitor/PrinterHelper.java index 44140034d74..82072fde69c 100644 --- a/src/main/java/spoon/reflect/visitor/PrinterHelper.java +++ b/src/main/java/spoon/reflect/visitor/PrinterHelper.java @@ -64,6 +64,8 @@ public class PrinterHelper { */ private boolean lastCharWasCR = false; + boolean prefixBlockComments; + public PrinterHelper() { } @@ -298,4 +300,12 @@ public void writeSpace() { public void setShouldWriteTabs(boolean b) { this.shouldWriteTabs = b; } + + /** + * Sets whether lines in block comments should be prefixed by a {@code *}. + * @param prefixBlockComments whether block comments should be prefixed. + */ + public void setPrefixBlockComments(boolean prefixBlockComments) { + this.prefixBlockComments = prefixBlockComments; + } } diff --git a/src/test/java/spoon/generating/clone/CloneBuilderTemplate.java b/src/test/java/spoon/generating/clone/CloneBuilderTemplate.java index ef9e0e8f439..143f49c318e 100644 --- a/src/test/java/spoon/generating/clone/CloneBuilderTemplate.java +++ b/src/test/java/spoon/generating/clone/CloneBuilderTemplate.java @@ -7,7 +7,6 @@ */ package spoon.generating.clone; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import spoon.reflect.declaration.CtElement; diff --git a/src/test/java/spoon/generating/meta/ModelRoleHandlerTemplate.java b/src/test/java/spoon/generating/meta/ModelRoleHandlerTemplate.java index fa5a7d1caa6..9812dd33930 100644 --- a/src/test/java/spoon/generating/meta/ModelRoleHandlerTemplate.java +++ b/src/test/java/spoon/generating/meta/ModelRoleHandlerTemplate.java @@ -1,9 +1,9 @@ -/** +/* * SPDX-License-Identifier: (MIT OR CECILL-C) * - * Copyright (C) 2006-2019 INRIA and contributors + * Copyright (C) 2006-2023 INRIA and contributors * - * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.generating.meta; diff --git a/src/test/java/spoon/generating/meta/RoleHandlerTemplate.java b/src/test/java/spoon/generating/meta/RoleHandlerTemplate.java index 1a670f7a25d..d56e72f1c56 100644 --- a/src/test/java/spoon/generating/meta/RoleHandlerTemplate.java +++ b/src/test/java/spoon/generating/meta/RoleHandlerTemplate.java @@ -1,18 +1,9 @@ -/** - * Copyright (C) 2006-2018 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ +/* + * SPDX-License-Identifier: (MIT OR CECILL-C) * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * Copyright (C) 2006-2023 INRIA and contributors * - * This program 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 CeCILL-C License for more details. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.generating.meta; diff --git a/src/test/java/spoon/generating/replace/ReplaceScanner.java b/src/test/java/spoon/generating/replace/ReplaceScanner.java index e7b71d5401b..f575ea72ca3 100644 --- a/src/test/java/spoon/generating/replace/ReplaceScanner.java +++ b/src/test/java/spoon/generating/replace/ReplaceScanner.java @@ -1,9 +1,9 @@ /* * SPDX-License-Identifier: (MIT OR CECILL-C) * - * Copyright (C) 2006-2019 INRIA and contributors + * Copyright (C) 2006-2023 INRIA and contributors * - * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.generating.replace; diff --git a/src/test/java/spoon/generating/replace/ReplacementVisitor.java b/src/test/java/spoon/generating/replace/ReplacementVisitor.java index ac9a7a95c25..67a93a2f87d 100644 --- a/src/test/java/spoon/generating/replace/ReplacementVisitor.java +++ b/src/test/java/spoon/generating/replace/ReplacementVisitor.java @@ -1,9 +1,9 @@ /* * SPDX-License-Identifier: (MIT OR CECILL-C) * - * Copyright (C) 2006-2019 INRIA and contributors + * Copyright (C) 2006-2023 INRIA and contributors * - * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) of the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.generating.replace; diff --git a/src/test/java/spoon/generating/scanner/CtBiScannerTemplate.java b/src/test/java/spoon/generating/scanner/CtBiScannerTemplate.java index b268b4a12af..2ebcc5681aa 100644 --- a/src/test/java/spoon/generating/scanner/CtBiScannerTemplate.java +++ b/src/test/java/spoon/generating/scanner/CtBiScannerTemplate.java @@ -1,18 +1,9 @@ -/** - * Copyright (C) 2006-2018 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ - * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. +/* + * SPDX-License-Identifier: (MIT OR CECILL-C) * - * This program 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 CeCILL-C License for more details. + * Copyright (C) 2006-2023 INRIA and contributors * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.generating.scanner; diff --git a/src/test/java/spoon/generating/scanner/PeekElementTemplate.java b/src/test/java/spoon/generating/scanner/PeekElementTemplate.java index 3f506cf03b8..86649b797bc 100644 --- a/src/test/java/spoon/generating/scanner/PeekElementTemplate.java +++ b/src/test/java/spoon/generating/scanner/PeekElementTemplate.java @@ -1,18 +1,9 @@ -/** - * Copyright (C) 2006-2018 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ +/* + * SPDX-License-Identifier: (MIT OR CECILL-C) * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * Copyright (C) 2006-2023 INRIA and contributors * - * This program 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 CeCILL-C License for more details. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. + * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon. */ package spoon.generating.scanner; diff --git a/src/test/java/spoon/processing/CtGenerationTest.java b/src/test/java/spoon/processing/CtGenerationTest.java index 991d1675c9d..9b14c8b4cd0 100644 --- a/src/test/java/spoon/processing/CtGenerationTest.java +++ b/src/test/java/spoon/processing/CtGenerationTest.java @@ -9,8 +9,10 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import spoon.Launcher; +import spoon.compiler.Environment; import spoon.generating.CloneVisitorGenerator; import spoon.generating.CtBiScannerGenerator; import spoon.generating.ReplacementVisitorGenerator; @@ -19,13 +21,18 @@ import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtType; import spoon.reflect.visitor.CtBiScannerDefault; +import spoon.reflect.visitor.DefaultJavaPrettyPrinter; +import spoon.reflect.visitor.DefaultTokenWriter; import spoon.reflect.visitor.Filter; +import spoon.reflect.visitor.PrettyPrinter; +import spoon.reflect.visitor.PrinterHelper; import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -56,6 +63,7 @@ public void testGenerateReplacementVisitor() throws Exception { launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().useTabulations(true); + configurePrinter(launcher); launcher.setSourceOutputDirectory("./target/generated/"); // interfaces. launcher.addInputResource("./src/main/java/spoon/reflect/code"); @@ -81,17 +89,9 @@ public void testGenerateReplacementVisitor() throws Exception { // cp ./target/generated/spoon/support/visitor/replace/ReplacementVisitor.java ./src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java CtClass actual = build(new File(launcher.getModelBuilder().getSourceOutputDirectory() + "/spoon/support/visitor/replace/ReplacementVisitor.java")).Class().get("spoon.support.visitor.replace.ReplacementVisitor"); CtClass expected = build(new File("./src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java")).Class().get("spoon.support.visitor.replace.ReplacementVisitor"); - // checkstyle enforces a newline at the end of the file - appendNewLine(new File("./target/generated/spoon/support/visitor/replace/ReplacementVisitor.java")); assertThat(actual).isEqualTo(expected); } - private void appendNewLine(File file) throws IOException { - FileWriter writer = new FileWriter(file, true); - writer.append("\n"); - writer.close(); - } - @Test public void testGenerateCtBiScanner() { // contract: generates the biscanner that is used for equality checking @@ -99,6 +99,7 @@ public void testGenerateCtBiScanner() { launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().useTabulations(true); + configurePrinter(launcher); launcher.setSourceOutputDirectory("./target/generated/"); // interfaces. launcher.addInputResource("./src/main/java/spoon/reflect/code"); @@ -112,7 +113,6 @@ public void testGenerateCtBiScanner() { launcher.setOutputFilter(new RegexFilter("spoon.reflect.visitor.CtBiScannerDefault")); launcher.run(); - // cp ./target/generated/spoon/reflect/visitor/CtBiScannerDefault.java ./src/main/java/spoon/reflect/visitor/CtBiScannerDefault.java // we don't necessarily want to hard-wired the relation between CtScanner and CtBiScannerDefault.java // this can be done on an informed basis when important changes are made in the metamodel/scanner // and then we can have smaller clean tested pull requests to see the impact of the change @@ -128,6 +128,7 @@ public void testGenerateCloneVisitor() { launcher.getEnvironment().setNoClasspath(true); launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().useTabulations(true); + configurePrinter(launcher); launcher.setSourceOutputDirectory("./target/generated/"); // interfaces. launcher.addInputResource("./src/main/java/spoon/reflect/code"); @@ -166,6 +167,7 @@ public void testGenerateRoleHandler() { launcher.getEnvironment().setCommentEnabled(true); launcher.getEnvironment().setCopyResources(false); launcher.getEnvironment().useTabulations(true); + configurePrinter(launcher); launcher.setSourceOutputDirectory("./target/generated/"); // Spoon model interfaces launcher.addInputResource("./src/main/java/spoon/reflect/code"); @@ -194,6 +196,53 @@ public void testGenerateRoleHandler() { assertThat(actual).isEqualTo(expected); } + @Test + @Disabled("only meant to be run manually to make copying easier") + void copyGeneratedFiles() throws IOException { + copy( + "./target/generated/spoon/support/visitor/replace/ReplacementVisitor.java", + "./src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java" + ); + copy( + "./target/generated/spoon/reflect/visitor/CtBiScannerDefault.java", + "./src/main/java/spoon/reflect/visitor/CtBiScannerDefault.java" + ); + copy( + "./target/generated/spoon/support/visitor/clone/CloneBuilder.java", + "./src/main/java/spoon/support/visitor/clone/CloneBuilder.java" + ); + copy( + "./target/generated/spoon/support/visitor/clone/CloneVisitor.java", + "./src/main/java/spoon/support/visitor/clone/CloneVisitor.java" + ); + copy( + "./target/generated/spoon/reflect/meta/impl/ModelRoleHandlers.java", + "./src/main/java/spoon/reflect/meta/impl/ModelRoleHandlers.java" + ); + } + + private static void copy(String from, String to) throws IOException { + Files.copy(Path.of(from), Path.of(to), StandardCopyOption.REPLACE_EXISTING); + } + + private void configurePrinter(Launcher launcher) { + Environment environment = launcher.getEnvironment(); + environment.setPrettyPrinterCreator(new Supplier<>() { + @Override + public PrettyPrinter get() { + // we want to create a printer configured for the given environment, + // but we are who creates this printer - juggle around this StackOverflowError + environment.setPrettyPrinterCreator(null); + DefaultJavaPrettyPrinter printer = (DefaultJavaPrettyPrinter) environment.createPrettyPrinter(); + environment.setPrettyPrinterCreator(this); + PrinterHelper printerHelper = new PrinterHelper(environment); + printerHelper.setPrefixBlockComments(true); + printer.setPrinterTokenWriter(new DefaultTokenWriter(printerHelper)); + return printer; + } + }); + } + private class RegexFilter implements Filter> { private final Pattern regex;