From 36f3905611e8310673f7813408f37fde1656df6b Mon Sep 17 00:00:00 2001 From: Andrew Bwogi Date: Thu, 14 Jan 2021 10:28:31 +0100 Subject: [PATCH 1/5] refactor spoon-core to use slf4j instead of log4j-api --- pom.xml | 15 +++----- src/main/java/spoon/Launcher.java | 10 ++--- src/main/java/spoon/MavenLauncher.java | 6 +-- src/main/java/spoon/compiler/Environment.java | 2 +- .../spoon/processing/AbstractProcessor.java | 2 +- .../visitor/DefaultJavaPrettyPrinter.java | 6 +-- .../visitor/ModelConsistencyChecker.java | 2 +- .../support/RuntimeProcessingManager.java | 2 +- .../spoon/support/StandardEnvironment.java | 34 ++++++++++++----- .../java/spoon/support/compiler/SpoonPom.java | 2 +- .../compiler/jdt/JDTBasedSpoonCompiler.java | 2 +- .../compiler/jdt/JDTCommentBuilder.java | 6 +-- .../support/compiler/jdt/JDTTreeBuilder.java | 6 +-- .../reflect/declaration/CtElementImpl.java | 6 +-- src/test/java/spoon/test/logging/LogTest.java | 37 ++++++------------- .../DefaultPrettyPrinterTest.java | 2 +- .../processing/processors/TestProcessor.java | 2 +- src/test/resources/logback-test.xml | 2 + 18 files changed, 71 insertions(+), 73 deletions(-) create mode 100644 src/test/resources/logback-test.xml diff --git a/pom.xml b/pom.xml index 798aa71b10c..7d0fe25598b 100644 --- a/pom.xml +++ b/pom.xml @@ -68,18 +68,13 @@ jsap 2.1 + - - org.apache.logging.log4j - log4j-api - 2.14.0 + org.slf4j + slf4j-api + 1.7.30 - - org.apache.logging.log4j - log4j-to-slf4j - 2.14.0 - - + commons-io commons-io 2.8.0 diff --git a/src/main/java/spoon/Launcher.java b/src/main/java/spoon/Launcher.java index ff80ff00928..306ec7a325d 100644 --- a/src/main/java/spoon/Launcher.java +++ b/src/main/java/spoon/Launcher.java @@ -17,9 +17,9 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.event.Level; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.SpoonModelBuilder.InputType; import spoon.compiler.Environment; import spoon.compiler.SpoonResource; @@ -540,7 +540,7 @@ protected void processArguments() { protected void reportClassPathMode() { String cpmode = jsapActualArgs.getString("cpmode").toUpperCase(); - Launcher.LOGGER.info("Running in " + cpmode + " mode (doc: http://spoon.gforge.inria.fr/launcher.html)."); + factory.getEnvironment().report(null, Level.INFO, "Running in " + cpmode + " mode (doc: http://spoon.gforge.inria.fr/launcher.html)."); } /** @@ -591,7 +591,7 @@ protected JSAPResult parseArgs() { /** * A default logger to be used by Spoon. */ - public static final Logger LOGGER = LogManager.getLogger(); + public static final Logger LOGGER = LoggerFactory.getLogger(Launcher.class); /** * Creates a new Spoon Java compiler in order to process and compile Java diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index f0319123b50..4c31e59aece 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -7,8 +7,8 @@ */ package spoon; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.support.compiler.SpoonPom; import java.io.File; @@ -23,7 +23,7 @@ public class MavenLauncher extends Launcher { private SOURCE_TYPE sourceType; private SpoonPom model; private boolean forceRefresh = false; - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LoggerFactory.getLogger(MavenLauncher.class); /** * @return SpoonPom corresponding to the pom file used by the launcher diff --git a/src/main/java/spoon/compiler/Environment.java b/src/main/java/spoon/compiler/Environment.java index af4bd2ac410..169d455f73b 100644 --- a/src/main/java/spoon/compiler/Environment.java +++ b/src/main/java/spoon/compiler/Environment.java @@ -7,7 +7,7 @@ */ package spoon.compiler; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.OutputType; import spoon.compiler.builder.EncodingProvider; import spoon.processing.FileGenerator; diff --git a/src/main/java/spoon/processing/AbstractProcessor.java b/src/main/java/spoon/processing/AbstractProcessor.java index 26823ed7b75..684e03bbfa0 100644 --- a/src/main/java/spoon/processing/AbstractProcessor.java +++ b/src/main/java/spoon/processing/AbstractProcessor.java @@ -7,7 +7,7 @@ */ package spoon.processing; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.Launcher; import spoon.compiler.Environment; import spoon.reflect.declaration.CtElement; diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 1f3e2478186..354ae519832 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -7,8 +7,8 @@ */ package spoon.reflect.visitor; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.SpoonException; import spoon.compiler.Environment; import spoon.experimental.CtUnresolvedImport; @@ -244,7 +244,7 @@ public DefaultJavaPrettyPrinter setLineSeparator(String lineSeparator) { } - protected static final Logger LOGGER = LogManager.getLogger(); + protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultJavaPrettyPrinter.class);; public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!"; /** * Prints an element. This method shall be called by the toString() method of an element. diff --git a/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java b/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java index e873adfdfe9..4c322256dfc 100644 --- a/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java +++ b/src/main/java/spoon/reflect/visitor/ModelConsistencyChecker.java @@ -7,7 +7,7 @@ */ package spoon.reflect.visitor; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.compiler.Environment; import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtNamedElement; diff --git a/src/main/java/spoon/support/RuntimeProcessingManager.java b/src/main/java/spoon/support/RuntimeProcessingManager.java index beaaf73f23f..ef66617d1df 100644 --- a/src/main/java/spoon/support/RuntimeProcessingManager.java +++ b/src/main/java/spoon/support/RuntimeProcessingManager.java @@ -7,7 +7,7 @@ */ package spoon.support; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.processing.ProcessInterruption; import spoon.processing.ProcessingManager; import spoon.processing.Processor; diff --git a/src/main/java/spoon/support/StandardEnvironment.java b/src/main/java/spoon/support/StandardEnvironment.java index fdc5a1502b3..1a52ace096a 100644 --- a/src/main/java/spoon/support/StandardEnvironment.java +++ b/src/main/java/spoon/support/StandardEnvironment.java @@ -8,8 +8,8 @@ package spoon.support; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; +import org.slf4j.event.Level; +import org.slf4j.Logger; import spoon.Launcher; import spoon.OutputType; import spoon.SpoonException; @@ -98,7 +98,7 @@ public void setPrettyPrintingMode(PRETTY_PRINTING_MODE prettyPrintingMode) { private transient Logger logger = Launcher.LOGGER; - private Level level = Level.OFF; + private Level level = Level.ERROR; private boolean shouldCompile = false; @@ -193,7 +193,13 @@ private Level toLevel(String level) { if (level == null || level.isEmpty()) { throw new SpoonException("Wrong level given at Spoon."); } - return Level.toLevel(level, Level.ALL); + Level levelEnum; + try { + levelEnum = Level.valueOf(level); + } catch (IllegalArgumentException e) { + return Level.TRACE; + } + return levelEnum; } @Override @@ -273,14 +279,24 @@ public void report(Processor processor, Level level, String message) { } private void print(String message, Level messageLevel) { - if (messageLevel.isMoreSpecificThan(this.level)) { - logger.log(messageLevel, message); + if (messageLevel.toInt() >= this.level.toInt()) { + switch (messageLevel) { + case ERROR: logger.error(message); + break; + case WARN: logger.warn(message); + break; + case INFO: logger.info(message); + break; + case DEBUG: logger.debug(message); + break; + case TRACE: logger.trace(message); + } } } - /** - * This method should be called to report the end of the processing. - */ + /** + * This method should be called to report the end of the processing. + */ @Override public void reportEnd() { print("end of processing: ", Level.INFO); diff --git a/src/main/java/spoon/support/compiler/SpoonPom.java b/src/main/java/spoon/support/compiler/SpoonPom.java index b4a7307826a..bd3510acb21 100644 --- a/src/main/java/spoon/support/compiler/SpoonPom.java +++ b/src/main/java/spoon/support/compiler/SpoonPom.java @@ -7,7 +7,7 @@ */ package spoon.support.compiler; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.apache.maven.model.Build; import org.apache.maven.model.BuildBase; import org.apache.maven.model.Model; diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java index e92bb0afe17..51c684efe41 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java @@ -8,7 +8,7 @@ package spoon.support.compiler.jdt; import org.apache.commons.io.IOUtils; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import org.eclipse.jdt.core.compiler.CategorizedProblem; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.internal.compiler.ASTVisitor; diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index 2cbcf8f5e8f..c1e63bdcbb0 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -7,10 +7,10 @@ */ package spoon.support.compiler.jdt; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; +import org.slf4j.LoggerFactory; import spoon.SpoonException; import spoon.reflect.code.CtAbstractSwitch; import spoon.reflect.code.CtBinaryOperator; @@ -71,7 +71,7 @@ */ public class JDTCommentBuilder { - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LoggerFactory.getLogger(JDTCommentBuilder.class);; private final CompilationUnitDeclaration declarationUnit; private CompilationUnit spoonUnit; diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 60175548659..dec579ae9a6 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -7,8 +7,7 @@ */ package spoon.support.compiler.jdt; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ASTVisitor; import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression; @@ -113,6 +112,7 @@ import org.eclipse.jdt.internal.compiler.lookup.Scope; import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; import org.eclipse.jdt.internal.compiler.lookup.VariableBinding; +import org.slf4j.LoggerFactory; import spoon.SpoonException; import spoon.reflect.code.BinaryOperatorKind; import spoon.reflect.code.CtArrayAccess; @@ -187,7 +187,7 @@ public static Logger getLogger() { return LOGGER; } - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LoggerFactory.getLogger(JDTTreeBuilder.class);; public PositionBuilder getPositionBuilder() { return position; diff --git a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java index c3a215ee5a2..26be19c5d9d 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java @@ -7,8 +7,8 @@ */ package spoon.support.reflect.declaration; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import spoon.reflect.ModelElementContainerDefaultCapacities; import spoon.reflect.annotations.MetamodelPropertyField; import spoon.reflect.code.CtBlock; @@ -74,7 +74,7 @@ */ public abstract class CtElementImpl implements CtElement, Serializable { private static final long serialVersionUID = 1L; - protected static final Logger LOGGER = LogManager.getLogger(); + protected static final Logger LOGGER = LoggerFactory.getLogger(CtElementImpl.class);; public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!"; private static final Factory DEFAULT_FACTORY = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment()); diff --git a/src/test/java/spoon/test/logging/LogTest.java b/src/test/java/spoon/test/logging/LogTest.java index 22f07a90a72..e21e52ae2d3 100644 --- a/src/test/java/spoon/test/logging/LogTest.java +++ b/src/test/java/spoon/test/logging/LogTest.java @@ -16,14 +16,11 @@ */ package spoon.test.logging; -import org.apache.commons.lang3.reflect.FieldUtils; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; import org.junit.Test; import org.junit.experimental.runners.Enclosed; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; -import org.mockito.ArgumentCaptor; +import org.slf4j.event.Level; import spoon.Launcher; import spoon.MavenLauncher; import uk.org.lidalia.slf4jtest.TestLogger; @@ -34,8 +31,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; @RunWith(Enclosed.class) public class LogTest { @@ -44,13 +39,11 @@ public class LogTest { public static class ParameterizedTest { @Parameterized.Parameters public static Collection data() { - return Arrays.asList(new Object[][]{ - {Level.ALL, 5}, - {Level.DEBUG, 5}, - {Level.INFO, 1}, - {Level.WARN, 0}, - {Level.ERROR, 0}, - {Level.OFF, 0} + return Arrays.asList(new Object[][] { + {Level.DEBUG, 6 }, + {Level.INFO, 2 }, + {Level.WARN, 0 }, + {Level.ERROR, 0 } }); } @@ -61,18 +54,11 @@ public static Collection data() { public int nbLogMessagesMinimum; @Test - public void testAllLevelsForLogs() throws Exception { + public void testAllLevelsForLogs() { + final TestLogger logger = TestLoggerFactory.getTestLogger(Launcher.class); final Launcher launcher = new Launcher(); - - Logger logger = mock(Logger.class); - FieldUtils.writeField(launcher.getEnvironment(), "logger", logger, true); - - ArgumentCaptor valueCaptureMessage = ArgumentCaptor.forClass(String.class); - ArgumentCaptor valueCaptureLevel = ArgumentCaptor.forClass(Level.class); - - doNothing().when(logger).log(valueCaptureLevel.capture(), valueCaptureMessage.capture()); - - launcher.setArgs(new String[]{ + logger.clear(); + launcher.setArgs(new String[] { "-i", "./src/test/java/spoon/test/logging", "--level", level.toString() }); @@ -85,8 +71,7 @@ public void testAllLevelsForLogs() throws Exception { assertEquals(level, launcher.getFactory().getEnvironment().getLevel()); // contract: the number of messages increases with the log level - // System.out.println(level+ " " + valueCaptureMessage.getAllValues().size()); - assertTrue(valueCaptureMessage.getAllValues().size() >= nbLogMessagesMinimum); + assertTrue(logger.getLoggingEvents().size() >= nbLogMessagesMinimum); } } diff --git a/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java b/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java index a09f9e959f8..12325f0cc4e 100644 --- a/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java +++ b/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java @@ -18,7 +18,7 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.apache.logging.log4j.Logger; +import org.slf4j.Logger; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.model.Profile; diff --git a/src/test/java/spoon/test/processing/processors/TestProcessor.java b/src/test/java/spoon/test/processing/processors/TestProcessor.java index 0c26f939353..c3ee3dfa511 100644 --- a/src/test/java/spoon/test/processing/processors/TestProcessor.java +++ b/src/test/java/spoon/test/processing/processors/TestProcessor.java @@ -18,7 +18,7 @@ import java.util.Date; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import spoon.processing.AbstractProcessor; import spoon.reflect.cu.CompilationUnit; import spoon.reflect.declaration.CtClass; diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 00000000000..2f3f3a747dc --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file From 68a4829f9f7e08e4c55cab195888f464e8b06582 Mon Sep 17 00:00:00 2001 From: Andrew Bwogi Date: Fri, 15 Jan 2021 16:40:14 +0100 Subject: [PATCH 2/5] remove log4j.properties --- spoon-pom/pom.xml | 20 +------------------ src/main/resources/log4j.properties | 16 --------------- .../spoon/test/processing/ProcessingTest.java | 2 +- 3 files changed, 2 insertions(+), 36 deletions(-) delete mode 100644 src/main/resources/log4j.properties diff --git a/spoon-pom/pom.xml b/spoon-pom/pom.xml index 6dca595b0a6..7d746edb3c9 100644 --- a/spoon-pom/pom.xml +++ b/spoon-pom/pom.xml @@ -34,7 +34,7 @@ - + org.junit.vintage @@ -102,24 +102,6 @@ clean install - - - - src/main/resources - true - - log4j.properties - - - - src/main/resources - false - - log4j.properties - - - - org.apache.maven.plugins diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index 56a819d9565..00000000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,16 +0,0 @@ -log4j.rootLogger=info, stdout -log4j.logger.spoon=debug, R - -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout - -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %c - %m%n - -log4j.appender.R=org.apache.log4j.RollingFileAppender -log4j.appender.R.File=${spoon.log.path} - -log4j.appender.R.MaxFileSize=1000KB -log4j.appender.R.MaxBackupIndex=3 - -log4j.appender.R.layout=org.apache.log4j.PatternLayout -log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p %c - %m%n diff --git a/src/test/java/spoon/test/processing/ProcessingTest.java b/src/test/java/spoon/test/processing/ProcessingTest.java index 6d6df8450ff..8f82261e93a 100644 --- a/src/test/java/spoon/test/processing/ProcessingTest.java +++ b/src/test/java/spoon/test/processing/ProcessingTest.java @@ -18,7 +18,7 @@ import com.google.common.io.Files; import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.Level; +import org.slf4j.event.Level; import org.junit.Test; import spoon.Launcher; import spoon.SpoonException; From 5b0c5cf04bd8e00bb970ad02dd8dd888d0a071de Mon Sep 17 00:00:00 2001 From: Andrew Bwogi Date: Fri, 15 Jan 2021 17:11:25 +0100 Subject: [PATCH 3/5] fix comments and imports --- .../spoon/support/StandardEnvironment.java | 6 +-- .../compiler/jdt/JDTCommentBuilder.java | 43 ++----------------- src/test/resources/logback-test.xml | 2 +- 3 files changed, 8 insertions(+), 43 deletions(-) diff --git a/src/main/java/spoon/support/StandardEnvironment.java b/src/main/java/spoon/support/StandardEnvironment.java index 1a52ace096a..2a0fd376065 100644 --- a/src/main/java/spoon/support/StandardEnvironment.java +++ b/src/main/java/spoon/support/StandardEnvironment.java @@ -294,9 +294,9 @@ private void print(String message, Level messageLevel) { } } - /** - * This method should be called to report the end of the processing. - */ + /** + * This method should be called to report the end of the processing. + */ @Override public void reportEnd() { print("end of processing: ", Level.INFO); diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index c1e63bdcbb0..248961b4be8 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -7,59 +7,24 @@ */ package spoon.support.compiler.jdt; -import org.slf4j.Logger; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import spoon.SpoonException; -import spoon.reflect.code.CtAbstractSwitch; -import spoon.reflect.code.CtBinaryOperator; -import spoon.reflect.code.CtBlock; -import spoon.reflect.code.CtBodyHolder; -import spoon.reflect.code.CtCase; -import spoon.reflect.code.CtCatch; -import spoon.reflect.code.CtComment; -import spoon.reflect.code.CtConditional; -import spoon.reflect.code.CtIf; -import spoon.reflect.code.CtLambda; -import spoon.reflect.code.CtNewArray; -import spoon.reflect.code.CtStatement; -import spoon.reflect.code.CtStatementList; -import spoon.reflect.code.CtSwitch; -import spoon.reflect.code.CtSwitchExpression; +import spoon.reflect.code.*; import spoon.reflect.cu.CompilationUnit; import spoon.reflect.cu.SourcePosition; import spoon.reflect.cu.position.BodyHolderSourcePosition; import spoon.reflect.cu.position.DeclarationSourcePosition; -import spoon.reflect.declaration.CtAnnotation; -import spoon.reflect.declaration.CtAnnotationType; -import spoon.reflect.declaration.CtAnonymousExecutable; -import spoon.reflect.declaration.CtClass; -import spoon.reflect.declaration.CtCompilationUnit; -import spoon.reflect.declaration.CtConstructor; -import spoon.reflect.declaration.CtElement; -import spoon.reflect.declaration.CtField; -import spoon.reflect.declaration.CtImport; -import spoon.reflect.declaration.CtInterface; -import spoon.reflect.declaration.CtMethod; -import spoon.reflect.declaration.CtModule; -import spoon.reflect.declaration.CtPackageDeclaration; -import spoon.reflect.declaration.CtParameter; -import spoon.reflect.declaration.CtType; -import spoon.reflect.declaration.CtTypeMember; -import spoon.reflect.declaration.CtVariable; -import spoon.reflect.declaration.ParentNotInitializedException; +import spoon.reflect.declaration.*; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtReference; import spoon.reflect.visitor.CtInheritanceScanner; import spoon.reflect.visitor.DefaultJavaPrettyPrinter; import spoon.reflect.visitor.EarlyTerminatingScanner; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; +import java.io.*; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collection; diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 2f3f3a747dc..e505519e6d5 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -1,2 +1,2 @@ - + \ No newline at end of file From 42394db523ceb1f1f3c4e12a01a61d1860d7f3e1 Mon Sep 17 00:00:00 2001 From: Andrew Bwogi Date: Mon, 18 Jan 2021 14:50:38 +0100 Subject: [PATCH 4/5] remove general imports --- .../compiler/jdt/JDTCommentBuilder.java | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java index 248961b4be8..a29cede54b6 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java @@ -7,25 +7,61 @@ */ package spoon.support.compiler.jdt; -import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; -import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; import spoon.SpoonException; -import spoon.reflect.code.*; +import spoon.reflect.code.CtAbstractSwitch; +import spoon.reflect.code.CtBinaryOperator; +import spoon.reflect.code.CtBlock; +import spoon.reflect.code.CtBodyHolder; +import spoon.reflect.code.CtCase; +import spoon.reflect.code.CtCatch; +import spoon.reflect.code.CtComment; +import spoon.reflect.code.CtConditional; +import spoon.reflect.code.CtIf; +import spoon.reflect.code.CtLambda; +import spoon.reflect.code.CtNewArray; +import spoon.reflect.code.CtStatement; +import spoon.reflect.code.CtStatementList; +import spoon.reflect.code.CtSwitch; +import spoon.reflect.code.CtSwitchExpression; import spoon.reflect.cu.CompilationUnit; import spoon.reflect.cu.SourcePosition; import spoon.reflect.cu.position.BodyHolderSourcePosition; import spoon.reflect.cu.position.DeclarationSourcePosition; -import spoon.reflect.declaration.*; +import spoon.reflect.declaration.CtAnnotation; +import spoon.reflect.declaration.CtAnnotationType; +import spoon.reflect.declaration.CtAnonymousExecutable; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtCompilationUnit; +import spoon.reflect.declaration.CtConstructor; +import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.CtField; +import spoon.reflect.declaration.CtImport; +import spoon.reflect.declaration.CtInterface; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtModule; +import spoon.reflect.declaration.CtPackageDeclaration; +import spoon.reflect.declaration.CtParameter; +import spoon.reflect.declaration.CtType; +import spoon.reflect.declaration.CtTypeMember; +import spoon.reflect.declaration.CtVariable; +import spoon.reflect.declaration.ParentNotInitializedException; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtReference; import spoon.reflect.visitor.CtInheritanceScanner; import spoon.reflect.visitor.DefaultJavaPrettyPrinter; import spoon.reflect.visitor.EarlyTerminatingScanner; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -36,7 +72,7 @@ */ public class JDTCommentBuilder { - private static final Logger LOGGER = LoggerFactory.getLogger(JDTCommentBuilder.class);; + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final CompilationUnitDeclaration declarationUnit; private CompilationUnit spoonUnit; From 60d4b195146a65721ee387a058a56741e395f696 Mon Sep 17 00:00:00 2001 From: Andrew Bwogi Date: Mon, 18 Jan 2021 14:51:38 +0100 Subject: [PATCH 5/5] use class lookup for logger initialization --- src/main/java/spoon/Launcher.java | 3 ++- src/main/java/spoon/MavenLauncher.java | 3 ++- .../java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java | 3 ++- src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java | 4 +++- .../java/spoon/support/reflect/declaration/CtElementImpl.java | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/spoon/Launcher.java b/src/main/java/spoon/Launcher.java index 306ec7a325d..2f4043114b3 100644 --- a/src/main/java/spoon/Launcher.java +++ b/src/main/java/spoon/Launcher.java @@ -46,6 +46,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.lang.invoke.MethodHandles; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; @@ -591,7 +592,7 @@ protected JSAPResult parseArgs() { /** * A default logger to be used by Spoon. */ - public static final Logger LOGGER = LoggerFactory.getLogger(Launcher.class); + public static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); /** * Creates a new Spoon Java compiler in order to process and compile Java diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 4c31e59aece..70cdb1a7c24 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -12,6 +12,7 @@ import spoon.support.compiler.SpoonPom; import java.io.File; +import java.lang.invoke.MethodHandles; import java.util.List; import java.util.regex.Pattern; @@ -23,7 +24,7 @@ public class MavenLauncher extends Launcher { private SOURCE_TYPE sourceType; private SpoonPom model; private boolean forceRefresh = false; - private static final Logger LOGGER = LoggerFactory.getLogger(MavenLauncher.class); + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); /** * @return SpoonPom corresponding to the pom file used by the launcher diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 354ae519832..c820ba06330 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -120,6 +120,7 @@ import spoon.support.util.ModelList; import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -244,7 +245,7 @@ public DefaultJavaPrettyPrinter setLineSeparator(String lineSeparator) { } - protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultJavaPrettyPrinter.class);; + protected static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!"; /** * Prints an element. This method shall be called by the toString() method of an element. diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index dec579ae9a6..819265d192a 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -159,6 +159,8 @@ import spoon.support.compiler.jdt.ContextBuilder.CastInfo; import spoon.support.reflect.CtExtendedModifier; +import java.lang.invoke.MethodHandles; + import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getBinaryOperatorKind; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getModifiers; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getUnaryOperator; @@ -187,7 +189,7 @@ public static Logger getLogger() { return LOGGER; } - private static final Logger LOGGER = LoggerFactory.getLogger(JDTTreeBuilder.class);; + private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public PositionBuilder getPositionBuilder() { return position; diff --git a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java index 26be19c5d9d..6ad66527913 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java @@ -57,6 +57,7 @@ import java.io.Serializable; import java.lang.annotation.Annotation; +import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -74,7 +75,7 @@ */ public abstract class CtElementImpl implements CtElement, Serializable { private static final long serialVersionUID = 1L; - protected static final Logger LOGGER = LoggerFactory.getLogger(CtElementImpl.class);; + protected static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!"; private static final Factory DEFAULT_FACTORY = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());