diff --git a/src/main/java/org/codehaus/mojo/antlr/AbstractAntlrMojo.java b/src/main/java/org/codehaus/mojo/antlr/AbstractAntlrMojo.java index cd964888..301ea829 100644 --- a/src/main/java/org/codehaus/mojo/antlr/AbstractAntlrMojo.java +++ b/src/main/java/org/codehaus/mojo/antlr/AbstractAntlrMojo.java @@ -26,7 +26,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.io.FilenameFilter; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; @@ -205,13 +204,12 @@ protected void executeAntlr() throws MojoExecutionException { validateParameters(); - Artifact antlrArtifact = locateAntlrArtifact(); - MetadataExtracter metadataExtracter = new MetadataExtracter(this, new Helper(antlrArtifact)); - XRef metadata = metadataExtracter.processMetadata(getGrammars()); + final Artifact antlrArtifact = locateAntlrArtifact(); + final MetadataExtracter metadataExtracter = new MetadataExtracter(this, new Helper(antlrArtifact)); + final XRef metadata = metadataExtracter.processMetadata(getGrammars()); - Iterator generationPlans = new GenerationPlanBuilder(this).buildGenerationPlans(metadata).iterator(); - while (generationPlans.hasNext()) { - final GenerationPlan plan = (GenerationPlan) generationPlans.next(); + for (final Object o : new GenerationPlanBuilder(this).buildGenerationPlans(metadata)) { + final GenerationPlan plan = (GenerationPlan) o; if (!plan.isOutOfDate()) { getLog().info("grammar [" + plan.getId() + "] was up-to-date; skipping"); continue; @@ -230,9 +228,8 @@ protected void executeAntlr() throws MojoExecutionException { protected final Artifact locateAntlrArtifact() throws NoAntlrDependencyDefinedException { Artifact antlrArtifact = null; if (project.getCompileArtifacts() != null) { - Iterator projectArtifacts = project.getCompileArtifacts().iterator(); - while (projectArtifacts.hasNext()) { - final Artifact artifact = (Artifact) projectArtifacts.next(); + for (final Object o : project.getCompileArtifacts()) { + final Artifact artifact = (Artifact) o; if ("antlr".equals(artifact.getGroupId()) && ("antlr".equals(artifact.getArtifactId()) || "antlr-all".equals(artifact.getArtifactId()))) { antlrArtifact = artifact; @@ -248,7 +245,7 @@ protected final Artifact locateAntlrArtifact() throws NoAntlrDependencyDefinedEx return antlrArtifact; } - protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) throws MojoExecutionException { + protected void performGeneration(final GenerationPlan plan, final Artifact antlrArtifact) throws MojoExecutionException { if (!plan.getGenerationDirectory().getParentFile().exists()) { plan.getGenerationDirectory().getParentFile().mkdirs(); } @@ -258,7 +255,7 @@ protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) th // Note: grammar file should be last // ---------------------------------------------------------------------- - List arguments = new LinkedList<>(); + final List arguments = new LinkedList<>(); addArgIf(arguments, debug, "-debug"); addArgIf(arguments, diagnostic, "-diagnostic"); addArgIf(arguments, trace, "-trace"); @@ -271,12 +268,12 @@ protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) th arguments.add("-o"); arguments.add(plan.getGenerationDirectory().getPath()); - if (plan.getCollectedSuperGrammarIds().size() > 0) { + if (!plan.getCollectedSuperGrammarIds().isEmpty()) { arguments.add("-glib"); - StringBuffer buffer = new StringBuffer(); - Iterator ids = plan.getCollectedSuperGrammarIds().iterator(); + final StringBuilder buffer = new StringBuilder(); + final Iterator ids = plan.getCollectedSuperGrammarIds().iterator(); while (ids.hasNext()) { - buffer.append(new File(sourceDirectory, (String) ids.next())); + buffer.append(new File(sourceDirectory, ids.next())); if (ids.hasNext()) { buffer.append(';'); } @@ -286,20 +283,20 @@ protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) th arguments.add(plan.getSource().getPath()); - String[] args = (String[]) arguments.toArray(new String[arguments.size()]); + final String[] args = arguments.toArray(new String[0]); if (plan.getImportVocabTokenTypesDirectory() != null && !plan.getImportVocabTokenTypesDirectory().equals(plan.getGenerationDirectory())) { // We need to spawn a new process to properly set up PWD - CommandLine commandLine = new CommandLine("java"); + final CommandLine commandLine = new CommandLine("java"); commandLine.addArgument("-classpath", false); commandLine.addArgument(generateClasspathForProcessSpawning(antlrArtifact), true); commandLine.addArgument("antlr.Tool", false); commandLine.addArguments(args, true); - DefaultExecutor executor = new DefaultExecutor(); + final DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(plan.getImportVocabTokenTypesDirectory()); try { executor.execute(commandLine); - } catch (IOException e) { + } catch (final IOException e) { getLog().warn("Error spawning process to execute antlr tool : " + e.getMessage()); } @@ -320,15 +317,15 @@ protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) th System.setProperty("user.dir", plan.getImportVocabTokenTypesDirectory().getPath()); } - PrintStream oldErr = System.err; + final PrintStream oldErr = System.err; - OutputStream errOS = new StringOutputStream(); - PrintStream err = new PrintStream(errOS); + final OutputStream errOS = new StringOutputStream(); + final PrintStream err = new PrintStream(errOS); System.setErr(err); try { - executeAntlrInIsolatedClassLoader((String[]) arguments.toArray(new String[0]), antlrArtifact); - } catch (RuntimeException e) { + executeAntlrInIsolatedClassLoader(arguments.toArray(new String[0]), antlrArtifact); + } catch (final RuntimeException e) { if (e.getMessage().contains("ANTLR Panic")) { // ANTLR-12 // Now basically every ANTLR version could set different message, how to handle in generic way? @@ -345,33 +342,33 @@ protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) th } System.setErr(oldErr); - System.err.println(errOS.toString()); + System.err.println(errOS); } } - private String generateClasspathForProcessSpawning(Artifact antlrArtifact) { + private String generateClasspathForProcessSpawning(final Artifact antlrArtifact) { // todo : is maven by itself enough for the generation??? return antlrArtifact.getFile().getPath(); } - private void executeAntlrInIsolatedClassLoader(String[] args, Artifact antlrArtifact) throws MojoExecutionException { - try (URLClassLoader classLoader = new URLClassLoader(new URL[] { antlrArtifact.getFile().toURI().toURL() }, getSystemClassLoader())) { + private void executeAntlrInIsolatedClassLoader(final String[] args, final Artifact antlrArtifact) throws MojoExecutionException { + try (final URLClassLoader classLoader = new URLClassLoader(new URL[] { antlrArtifact.getFile().toURI().toURL() }, getSystemClassLoader())) { classLoader.loadClass("antlr.Tool") - .getMethod("main", new Class[] { String[].class }) + .getMethod("main", String[].class) .invoke(null, new Object[] { args }); - } catch (MalformedURLException e) { + } catch (final MalformedURLException e) { throw new MojoExecutionException("Unable to resolve antlr:antlr artifact url", e); - } catch (ClassNotFoundException e) { + } catch (final ClassNotFoundException e) { throw new MojoExecutionException("could not locate antlr.Tool class"); - } catch (NoSuchMethodException e) { + } catch (final NoSuchMethodException e) { throw new MojoExecutionException("error locating antlt.Tool#main", e); - } catch (InvocationTargetException e) { + } catch (final InvocationTargetException e) { throw new MojoExecutionException("error perforing antlt.Tool#main", e.getTargetException()); - } catch (IllegalAccessException e) { + } catch (final IllegalAccessException e) { throw new MojoExecutionException("error perforing antlt.Tool#main", e); - } catch (IOException e1) { + } catch (final IOException e1) { e1.printStackTrace(); } } @@ -390,7 +387,7 @@ private void executeAntlrInIsolatedClassLoader(String[] args, Artifact antlrArti * @param b * @param value */ - protected static void addArgIf(List arguments, boolean b, String value) { + protected static void addArgIf(final List arguments, final boolean b, final String value) { if (b) { arguments.add(value); } @@ -402,19 +399,19 @@ protected static void addArgIf(List arguments, boolean b, String value) * @return generated file * @throws IOException */ - private File getGeneratedFile(String grammar, File outputDir) throws IOException { + private File getGeneratedFile(final String grammar, final File outputDir) throws IOException { String generatedFileName = null; String packageName = ""; - BufferedReader in = new BufferedReader(new FileReader(grammar)); + final BufferedReader in = new BufferedReader(new FileReader(grammar)); String line; while ((line = in.readLine()) != null) { line = line.trim(); - int extendsIndex = line.indexOf(" extends "); + final int extendsIndex = line.indexOf(" extends "); if (line.startsWith("class ") && extendsIndex > -1) { generatedFileName = line.substring(6, extendsIndex).trim(); @@ -433,7 +430,7 @@ private File getGeneratedFile(String grammar, File outputDir) throws IOException File genFile = null; - if ("".equals(packageName)) { + if (packageName.isEmpty()) { genFile = new File(outputDir, generatedFileName + ".java"); } else { String packagePath = packageName.replace('.', File.separatorChar); @@ -453,17 +450,16 @@ private File getGeneratedFile(String grammar, File outputDir) throws IOException */ private void validateParameters() throws MojoExecutionException { if ((isEmpty(grammars)) && ((grammarDefs == null) || (grammarDefs.length == 0))) { - StringBuffer msg = new StringBuffer(); - msg.append("Antlr plugin parameters are invalid/missing.").append('\n'); - msg.append("Inside the definition for plugin 'antlr-maven-plugin' specify the following:").append('\n'); - msg.append('\n'); - msg.append("").append('\n'); - msg.append(" VALUE").append('\n'); - msg.append("- OR - ").append('\n'); - msg.append(" VALUE").append('\n'); - msg.append("").append('\n'); - - throw new MojoExecutionException(msg.toString()); + final String msg = "Antlr plugin parameters are invalid/missing." + '\n' + + "Inside the definition for plugin 'antlr-maven-plugin' specify the following:" + '\n' + + '\n' + + "" + '\n' + + " VALUE" + '\n' + + "- OR - " + '\n' + + " VALUE" + '\n' + + "" + '\n'; + + throw new MojoExecutionException(msg); } } @@ -476,14 +472,14 @@ private void validateParameters() throws MojoExecutionException { * @return an array of grammar from grammars and grammarDefs variables */ private org.codehaus.mojo.antlr.options.Grammar[] getGrammars() { - List grammarList = new ArrayList<>(); - Set grammarSet = new HashSet<>(); + final List grammarList = new ArrayList<>(); + final Set grammarSet = new HashSet<>(); if (StringUtils.isNotEmpty(grammars)) { - StringTokenizer st = new StringTokenizer(grammars, ", "); + final StringTokenizer st = new StringTokenizer(grammars, ", "); while (st.hasMoreTokens()) { - String currentGrammar = st.nextToken().trim(); + final String currentGrammar = st.nextToken().trim(); if (StringUtils.isNotEmpty(currentGrammar)) { // Check if some pattern has been used @@ -494,19 +490,14 @@ private org.codehaus.mojo.antlr.options.Grammar[] getGrammars() { .replaceAll("\\?", "."); // Filter the source directory - String[] dir = sourceDirectory.list(new FilenameFilter() { - @Override - public boolean accept(File dir, String s) { - return Pattern.matches(transformedGrammar, s); - } - }); + final String[] dir = sourceDirectory.list((dir1, s) -> Pattern.matches(transformedGrammar, s)); if ((dir != null) && (dir.length != 0)) { - for (int i = 0; i < dir.length; i++) { + for (final String s : dir) { // Just add fles which are not in the set of files already seen. - if (!grammarSet.contains(dir[i])) { - org.codehaus.mojo.antlr.options.Grammar grammar = new org.codehaus.mojo.antlr.options.Grammar(); - grammar.setName(dir[i]); + if (!grammarSet.contains(s)) { + final org.codehaus.mojo.antlr.options.Grammar grammar = new org.codehaus.mojo.antlr.options.Grammar(); + grammar.setName(s); grammarList.add(grammar); } @@ -514,7 +505,7 @@ public boolean accept(File dir, String s) { } } else { if (!grammarSet.contains(currentGrammar)) { - org.codehaus.mojo.antlr.options.Grammar grammar = new org.codehaus.mojo.antlr.options.Grammar(); + final org.codehaus.mojo.antlr.options.Grammar grammar = new org.codehaus.mojo.antlr.options.Grammar(); grammar.setName(currentGrammar); grammarList.add(grammar); @@ -528,11 +519,11 @@ public boolean accept(File dir, String s) { grammarList.addAll(asList(grammarDefs)); } - return (org.codehaus.mojo.antlr.options.Grammar[]) grammarList.toArray(new org.codehaus.mojo.antlr.options.Grammar[0]); + return grammarList.toArray(new org.codehaus.mojo.antlr.options.Grammar[0]); } public static class NoAntlrDependencyDefinedException extends MojoExecutionException { - public NoAntlrDependencyDefinedException(String s) { + public NoAntlrDependencyDefinedException(final String s) { super(s); } } diff --git a/src/main/java/org/codehaus/mojo/antlr/AntlrHtmlReport.java b/src/main/java/org/codehaus/mojo/antlr/AntlrHtmlReport.java index a27e7ca7..f618c2b6 100644 --- a/src/main/java/org/codehaus/mojo/antlr/AntlrHtmlReport.java +++ b/src/main/java/org/codehaus/mojo/antlr/AntlrHtmlReport.java @@ -21,12 +21,10 @@ import java.io.File; import java.io.IOException; -import java.util.Iterator; import java.util.List; import java.util.Locale; import org.apache.maven.doxia.siterenderer.Renderer; -import org.apache.maven.doxia.siterenderer.RendererException; import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -91,7 +89,7 @@ public class AntlrHtmlReport extends AbstractAntlrMojo implements MavenReport { * @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale) */ @Override - public String getName(Locale locale) { + public String getName(final Locale locale) { if (StringUtils.isEmpty(name)) { return i18n.getString("antlr-report", locale, "report.name"); } @@ -103,7 +101,7 @@ public String getName(Locale locale) { * @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale) */ @Override - public String getDescription(Locale locale) { + public String getDescription(final Locale locale) { if (StringUtils.isEmpty(description)) { return i18n.getString("antlr-report", locale, "report.description"); } @@ -115,7 +113,7 @@ public String getDescription(Locale locale) { * @see org.codehaus.mojo.antlr.AbstractAntlrMojo#addArgs(java.util.List) */ @Override - protected void addArgs(List arguments) { + protected void addArgs(final List arguments) { // ---------------------------------------------------------------------- // See http://www.antlr2.org/doc/options.html#Command%20Line%20Options // ---------------------------------------------------------------------- @@ -127,16 +125,16 @@ protected void addArgs(List arguments) { * @see org.apache.maven.reporting.MavenReport#generate(org.codehaus.doxia.sink.Sink, java.util.Locale) */ @Override - public void generate(Sink sink, Locale locale) throws MavenReportException { + public void generate(final Sink sink, final Locale locale) throws MavenReportException { outputDirectory = getReportOutputDirectory(); try { executeAntlr(); - } catch (MojoExecutionException e) { + } catch (final MojoExecutionException e) { throw new MavenReportException("Antlr execution failed: " + e.getMessage(), e); } - AntlrRenderer r = new AntlrRenderer(sink, outputDirectory, i18n, Locale.ENGLISH); + final AntlrRenderer r = new AntlrRenderer(sink, outputDirectory, i18n, Locale.ENGLISH); r.render(); } @@ -188,7 +186,7 @@ public File getReportOutputDirectory() { * @see org.apache.maven.reporting.MavenReport#setReportOutputDirectory(java.io.File) */ @Override - public void setReportOutputDirectory(File reportOutputDirectory) { + public void setReportOutputDirectory(final File reportOutputDirectory) { if ((reportOutputDirectory != null) && (!reportOutputDirectory.getAbsolutePath().endsWith("antlr"))) { this.reportOutputDirectory = new File(reportOutputDirectory, "antlr"); } else { @@ -202,11 +200,11 @@ public void setReportOutputDirectory(File reportOutputDirectory) { @Override public void execute() throws MojoExecutionException, MojoFailureException { try { - SiteRendererSink sink = new SiteRendererSink( + final SiteRendererSink sink = new SiteRendererSink( new org.apache.maven.doxia.siterenderer.RenderingContext(getReportOutputDirectory(), getOutputName() + ".html")); generate(sink, Locale.getDefault()); - } catch (MavenReportException e) { + } catch (final MavenReportException e) { throw new MojoExecutionException("An error has occurred in " + getName(Locale.ENGLISH) + " report generation.", e); } } @@ -215,13 +213,13 @@ public void execute() throws MojoExecutionException, MojoFailureException { * Renderer report */ private static class AntlrRenderer extends AbstractMavenReportRenderer { - private File outputDirectory; + private final File outputDirectory; - private I18N i18n; + private final I18N i18n; - private Locale locale; + private final Locale locale; - AntlrRenderer(Sink sink, File outputDirectory, I18N i18n, Locale locale) { + AntlrRenderer(final Sink sink, final File outputDirectory, final I18N i18n, final Locale locale) { super(sink); this.outputDirectory = outputDirectory; @@ -253,14 +251,14 @@ public void renderBody() { startSection(i18n.getString("antlr-report", locale, "report.grammars.title")); try { - List htmlFiles = FileUtils.getFiles(outputDirectory, "**/*.html", "**/*index.html"); + final List htmlFiles = FileUtils.getFiles(outputDirectory, "**/*.html", "**/*index.html"); if (htmlFiles.isEmpty()) { sink.text(i18n.getString("antlr-report", locale, "report.grammars.noreport")); } else { sink.list(); - for (Iterator it = htmlFiles.iterator(); it.hasNext();) { - File current = (File) it.next(); + for (final Object htmlFile : htmlFiles) { + final File current = (File) htmlFile; sink.listItem(); sink.link(PathUtils.toRelative(outputDirectory, current.getAbsolutePath())); @@ -270,7 +268,7 @@ public void renderBody() { } sink.list_(); } - } catch (IOException e) { + } catch (final IOException e) { throw new RuntimeException("IOException: " + e.getMessage(), e); } diff --git a/src/main/java/org/codehaus/mojo/antlr/AntlrPlugin.java b/src/main/java/org/codehaus/mojo/antlr/AntlrPlugin.java index 77fb69b2..4802b3f4 100644 --- a/src/main/java/org/codehaus/mojo/antlr/AntlrPlugin.java +++ b/src/main/java/org/codehaus/mojo/antlr/AntlrPlugin.java @@ -59,7 +59,7 @@ public void execute() throws MojoExecutionException { /** * @see org.codehaus.mojo.antlr.AbstractAntlrMojo#addArgs(java.util.List) */ - protected void addArgs(List arguments) { + protected void addArgs(final List arguments) { // nop } } diff --git a/src/main/java/org/codehaus/mojo/antlr/Environment.java b/src/main/java/org/codehaus/mojo/antlr/Environment.java index 4e027533..d2035234 100644 --- a/src/main/java/org/codehaus/mojo/antlr/Environment.java +++ b/src/main/java/org/codehaus/mojo/antlr/Environment.java @@ -29,9 +29,9 @@ * @author Steve Ebersole */ public interface Environment { - public Log getLog(); + Log getLog(); - public File getSourceDirectory(); + File getSourceDirectory(); - public File getOutputDirectory(); + File getOutputDirectory(); } diff --git a/src/main/java/org/codehaus/mojo/antlr/metadata/Grammar.java b/src/main/java/org/codehaus/mojo/antlr/metadata/Grammar.java index 521bc574..6571c82e 100644 --- a/src/main/java/org/codehaus/mojo/antlr/metadata/Grammar.java +++ b/src/main/java/org/codehaus/mojo/antlr/metadata/Grammar.java @@ -39,7 +39,7 @@ public class Grammar { private String exportVocab; - public Grammar(GrammarFile grammarFile) { + public Grammar(final GrammarFile grammarFile) { this.grammarFile = grammarFile; grammarFile.addGrammar(this); } @@ -52,7 +52,7 @@ public String getClassName() { return className; } - public void setClassName(String className) { + public void setClassName(final String className) { this.className = className; } @@ -60,7 +60,7 @@ public String getSuperGrammarName() { return superGrammarName; } - public void setSuperGrammarName(String superGrammarName) { + public void setSuperGrammarName(final String superGrammarName) { this.superGrammarName = superGrammarName; } @@ -68,7 +68,7 @@ public String getImportVocab() { return importVocab; } - public void setImportVocab(String importVocab) { + public void setImportVocab(final String importVocab) { this.importVocab = importVocab; } @@ -76,7 +76,7 @@ public String getExportVocab() { return exportVocab; } - public void setExportVocab(String exportVocab) { + public void setExportVocab(final String exportVocab) { this.exportVocab = exportVocab; } diff --git a/src/main/java/org/codehaus/mojo/antlr/metadata/GrammarFile.java b/src/main/java/org/codehaus/mojo/antlr/metadata/GrammarFile.java index 02effe68..fd70adbc 100644 --- a/src/main/java/org/codehaus/mojo/antlr/metadata/GrammarFile.java +++ b/src/main/java/org/codehaus/mojo/antlr/metadata/GrammarFile.java @@ -34,9 +34,9 @@ public class GrammarFile { private final String[] glibs; private String packageName; - private List grammars = new ArrayList<>(); + private final List grammars = new ArrayList<>(); - public GrammarFile(String id, String fileName, String[] glibs) { + public GrammarFile(final String id, final String fileName, final String[] glibs) { this.id = id; this.fileName = fileName; this.glibs = glibs; @@ -58,11 +58,11 @@ public String getPackageName() { return packageName; } - void setPackageName(String packageName) { + void setPackageName(final String packageName) { this.packageName = packageName; } - void addGrammar(Grammar grammar) { + void addGrammar(final Grammar grammar) { grammars.add(grammar); } diff --git a/src/main/java/org/codehaus/mojo/antlr/metadata/MetadataExtracter.java b/src/main/java/org/codehaus/mojo/antlr/metadata/MetadataExtracter.java index 6f8eaa23..fce34071 100644 --- a/src/main/java/org/codehaus/mojo/antlr/metadata/MetadataExtracter.java +++ b/src/main/java/org/codehaus/mojo/antlr/metadata/MetadataExtracter.java @@ -31,50 +31,50 @@ public class MetadataExtracter { private final Environment environment; private final Class antlrHierarchyClass; - public MetadataExtracter(Environment environment, Helper helper) throws MojoExecutionException { + public MetadataExtracter(final Environment environment, final Helper helper) throws MojoExecutionException { this.environment = environment; this.helper = helper; this.antlrHierarchyClass = helper.getAntlrHierarchyClass(); } - public XRef processMetadata(org.codehaus.mojo.antlr.options.Grammar[] grammars) throws MojoExecutionException { - Object hierarchy; - Method readGrammarFileMethod; - Method getFileMethod; + public XRef processMetadata(final org.codehaus.mojo.antlr.options.Grammar[] grammars) throws MojoExecutionException { + final Object hierarchy; + final Method readGrammarFileMethod; + final Method getFileMethod; try { - Object antlrTool = helper.getAntlrToolClass().getDeclaredConstructor().newInstance(); + final Object antlrTool = helper.getAntlrToolClass().getDeclaredConstructor().newInstance(); hierarchy = antlrHierarchyClass.getConstructor(new Class[] { helper.getAntlrToolClass() }) - .newInstance(new Object[] { antlrTool }); + .newInstance(antlrTool); readGrammarFileMethod = antlrHierarchyClass.getMethod("readGrammarFile", Helper.STRING_ARG_SIGNATURE); getFileMethod = antlrHierarchyClass.getMethod("getFile", Helper.STRING_ARG_SIGNATURE); - } catch (Throwable t) { + } catch (final Throwable t) { throw new MojoExecutionException("Unable to instantiate Antlr preprocessor tool", causeToUse(t)); } - List files = new ArrayList<>(); - for (int i = 0; i < grammars.length; i++) { - String grammarName = grammars[i].getName().trim(); + final List files = new ArrayList<>(); + for (final org.codehaus.mojo.antlr.options.Grammar value : grammars) { + final String grammarName = value.getName().trim(); if (isEmpty(grammarName)) { environment.getLog().info("Empty grammar in the configuration; skipping."); continue; } - File grammar = new File(environment.getSourceDirectory(), grammarName); + final File grammar = new File(environment.getSourceDirectory(), grammarName); if (!grammar.exists()) { throw new MojoExecutionException("The grammar '" + grammar.getAbsolutePath() + "' doesnt exist."); } - String grammarFilePath = grammar.getPath(); - GrammarFile grammarFile = new GrammarFile( - grammarName, + final String grammarFilePath = grammar.getPath(); + final GrammarFile grammarFile = new GrammarFile( + grammarName, grammarFilePath, - isNotEmpty(grammars[i].getGlib()) ? split(grammars[i].getGlib(), ":,") : new String[0]); + isNotEmpty(value.getGlib()) ? split(value.getGlib(), ":,") : new String[0]); // :( antlr.preprocessor.GrammarFile's only access to package is through a protected field :( - try (BufferedReader in = new BufferedReader(new FileReader(grammar))) { - + try (final BufferedReader in = new BufferedReader(new FileReader(grammar))) { + String line; while ((line = in.readLine()) != null) { line = line.trim(); @@ -83,28 +83,27 @@ public XRef processMetadata(org.codehaus.mojo.antlr.options.Grammar[] grammars) break; } } - } - catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); } files.add(grammarFile); try { - readGrammarFileMethod.invoke(hierarchy, new Object[] { grammarFilePath }); - } catch (Throwable t) { + readGrammarFileMethod.invoke(hierarchy, grammarFilePath); + } catch (final Throwable t) { throw new MojoExecutionException("Unable to use Antlr preprocessor to read grammar file", causeToUse(t)); } } - XRef xref = new XRef(hierarchy); - for (GrammarFile gf : files) { - String grammarFilePath = gf.getFileName(); + final XRef xref = new XRef(hierarchy); + for (final GrammarFile gf : files) { + final String grammarFilePath = gf.getFileName(); try { - Object antlrGrammarFileDef = getFileMethod.invoke(hierarchy, new Object[] { grammarFilePath }); + final Object antlrGrammarFileDef = getFileMethod.invoke(hierarchy, grammarFilePath); intrepretMetadata(gf, antlrGrammarFileDef); xref.addGrammarFile(gf); - } catch (Throwable t) { + } catch (final Throwable t) { throw new MojoExecutionException("Unable to build grammar metadata", causeToUse(t)); } } @@ -112,50 +111,49 @@ public XRef processMetadata(org.codehaus.mojo.antlr.options.Grammar[] grammars) return xref; } - private void intrepretMetadata(GrammarFile grammarFile, Object antlrGrammarFileDef) throws MojoExecutionException { + private void intrepretMetadata(final GrammarFile grammarFile, final Object antlrGrammarFileDef) throws MojoExecutionException { try { - Object grammarsVector = helper.getAntlrGrammarFileClass() + final Object grammarsVector = helper.getAntlrGrammarFileClass() .getMethod("getGrammars", NO_ARG_SIGNATURE) .invoke(antlrGrammarFileDef, NO_ARGS); - @SuppressWarnings("unchecked") - Enumeration grammars = (Enumeration) + @SuppressWarnings("unchecked") final Enumeration grammars = (Enumeration) helper.getAntlrIndexedVectorClass() .getMethod("elements", NO_ARG_SIGNATURE) .invoke(grammarsVector, NO_ARGS); while (grammars.hasMoreElements()) { - Grammar grammar = new Grammar(grammarFile); + final Grammar grammar = new Grammar(grammarFile); intrepret(grammar, grammars.nextElement()); } - } catch (Throwable t) { + } catch (final Throwable t) { throw new MojoExecutionException("Error attempting to access grammars within grammar file", t); } } - private void intrepret(Grammar grammar, Object antlrGrammarDef) throws MojoExecutionException { + private void intrepret(final Grammar grammar, final Object antlrGrammarDef) throws MojoExecutionException { try { - Method getNameMethod = helper.getAntlrGrammarClass().getDeclaredMethod("getName", NO_ARG_SIGNATURE); + final Method getNameMethod = helper.getAntlrGrammarClass().getDeclaredMethod("getName", NO_ARG_SIGNATURE); getNameMethod.setAccessible(true); - String name = (String) getNameMethod.invoke(antlrGrammarDef, NO_ARGS); + final String name = (String) getNameMethod.invoke(antlrGrammarDef, NO_ARGS); grammar.setClassName(name); - Method getSuperGrammarNameMethod = helper.getAntlrGrammarClass().getMethod("getSuperGrammarName", NO_ARG_SIGNATURE); + final Method getSuperGrammarNameMethod = helper.getAntlrGrammarClass().getMethod("getSuperGrammarName", NO_ARG_SIGNATURE); getSuperGrammarNameMethod.setAccessible(true); - String superGrammarName = (String) getSuperGrammarNameMethod.invoke(antlrGrammarDef, NO_ARGS); + final String superGrammarName = (String) getSuperGrammarNameMethod.invoke(antlrGrammarDef, NO_ARGS); grammar.setSuperGrammarName(superGrammarName); - Method getOptionsMethod = helper.getAntlrGrammarClass().getMethod("getOptions", NO_ARG_SIGNATURE); + final Method getOptionsMethod = helper.getAntlrGrammarClass().getMethod("getOptions", NO_ARG_SIGNATURE); getOptionsMethod.setAccessible(true); - Object options = getOptionsMethod.invoke(antlrGrammarDef, NO_ARGS); + final Object options = getOptionsMethod.invoke(antlrGrammarDef, NO_ARGS); - Method getElementMethod = helper.getAntlrIndexedVectorClass().getMethod("getElement", new Class[] { Object.class }); + final Method getElementMethod = helper.getAntlrIndexedVectorClass().getMethod("getElement", Object.class); getElementMethod.setAccessible(true); - Method getRHSMethod = helper.getAntlrOptionClass().getMethod("getRHS", NO_ARG_SIGNATURE); + final Method getRHSMethod = helper.getAntlrOptionClass().getMethod("getRHS", NO_ARG_SIGNATURE); getRHSMethod.setAccessible(true); - Object importVocabOption = getElementMethod.invoke(options, new Object[] { "importVocab" }); + final Object importVocabOption = getElementMethod.invoke(options, "importVocab"); if (importVocabOption != null) { String importVocab = (String) getRHSMethod.invoke(importVocabOption, NO_ARGS); if (importVocab != null) { @@ -167,7 +165,7 @@ private void intrepret(Grammar grammar, Object antlrGrammarDef) throws MojoExecu } } - Object exportVocabOption = getElementMethod.invoke(options, new Object[] { "exportVocab" }); + final Object exportVocabOption = getElementMethod.invoke(options, "exportVocab"); if (exportVocabOption != null) { String exportVocab = (String) getRHSMethod.invoke(exportVocabOption, NO_ARGS); if (exportVocab != null) { @@ -178,12 +176,12 @@ private void intrepret(Grammar grammar, Object antlrGrammarDef) throws MojoExecu } grammar.setExportVocab(exportVocab); } - } catch (Throwable t) { + } catch (final Throwable t) { throw new MojoExecutionException("Error accessing Antlr grammar metadata", t); } } - private Throwable causeToUse(Throwable throwable) { + private Throwable causeToUse(final Throwable throwable) { if (throwable instanceof InvocationTargetException) { return ((InvocationTargetException) throwable).getTargetException(); } diff --git a/src/main/java/org/codehaus/mojo/antlr/metadata/XRef.java b/src/main/java/org/codehaus/mojo/antlr/metadata/XRef.java index 7effb2e4..8a7d204b 100644 --- a/src/main/java/org/codehaus/mojo/antlr/metadata/XRef.java +++ b/src/main/java/org/codehaus/mojo/antlr/metadata/XRef.java @@ -32,11 +32,11 @@ public class XRef { private final Object antlrHierarchy; - private LinkedHashMap filesById = new LinkedHashMap<>(); - private Map filesByExportVocab = new HashMap<>(); - private Map filesByClassName = new HashMap<>(); + private final LinkedHashMap filesById = new LinkedHashMap<>(); + private final Map filesByExportVocab = new HashMap<>(); + private final Map filesByClassName = new HashMap<>(); - public XRef(Object antlrHierarchy) { + public XRef(final Object antlrHierarchy) { this.antlrHierarchy = antlrHierarchy; } @@ -44,13 +44,13 @@ public Object getAntlrHierarchy() { return antlrHierarchy; } - void addGrammarFile(GrammarFile grammarFile) { + void addGrammarFile(final GrammarFile grammarFile) { filesById.put(grammarFile.getId(), grammarFile); - for (Grammar grammar : grammarFile.getGrammars()) { + for (final Grammar grammar : grammarFile.getGrammars()) { filesByClassName.put(grammar.getClassName(), grammarFile); if (grammar.getExportVocab() != null) { - GrammarFile old = filesByExportVocab.put(grammar.getExportVocab(), grammarFile); + final GrammarFile old = filesByExportVocab.put(grammar.getExportVocab(), grammarFile); if (old != null && old != grammarFile) { System.out.println("[WARNING] : multiple grammars defined the same exportVocab : " + grammar.getExportVocab()); } @@ -62,15 +62,15 @@ public Iterator iterateGrammarFiles() { return filesById.values().iterator(); } - public GrammarFile getGrammarFileById(String id) { - return (GrammarFile) filesById.get(id); + public GrammarFile getGrammarFileById(final String id) { + return filesById.get(id); } - public GrammarFile getGrammarFileByClassName(String className) { - return (GrammarFile) filesByClassName.get(className); + public GrammarFile getGrammarFileByClassName(final String className) { + return filesByClassName.get(className); } - public GrammarFile getGrammarFileByExportVocab(String exportVocab) { - return (GrammarFile) filesByExportVocab.get(exportVocab); + public GrammarFile getGrammarFileByExportVocab(final String exportVocab) { + return filesByExportVocab.get(exportVocab); } } diff --git a/src/main/java/org/codehaus/mojo/antlr/options/Grammar.java b/src/main/java/org/codehaus/mojo/antlr/options/Grammar.java index 13896f8e..7d330c1f 100644 --- a/src/main/java/org/codehaus/mojo/antlr/options/Grammar.java +++ b/src/main/java/org/codehaus/mojo/antlr/options/Grammar.java @@ -46,7 +46,7 @@ public String getGlib() { * * @param glib */ - public void setGlib(String glib) { + public void setGlib(final String glib) { this.glib = glib; } @@ -62,7 +62,7 @@ public String getName() { * * @param name */ - public void setName(String name) { + public void setName(final String name) { this.name = name; } @@ -71,7 +71,7 @@ public void setName(String name) { * * @param other */ - public boolean equals(Object other) { + public boolean equals(final Object other) { if (this == other) { return true; } @@ -80,7 +80,7 @@ public boolean equals(Object other) { return false; } - Grammar that = (Grammar) other; + final Grammar that = (Grammar) other; boolean result = true; result = result && (getName() == null ? that.getName() == null : getName().equals(that.getName())); result = result && (getGlib() == null ? that.getGlib() == null : getGlib().equals(that.getGlib())); @@ -102,18 +102,17 @@ public int hashCode() { * Method toString */ public java.lang.String toString() { - StringBuffer buf = new StringBuffer(); - buf.append("name = '"); - buf.append(getName() + "'"); - buf.append("\n"); - buf.append("glib = '"); - buf.append(getGlib() + "'"); - return buf.toString(); + final String buf = "name = '" + + getName() + "'" + + "\n" + + "glib = '" + + getGlib() + "'"; + return buf; } // -- java.lang.String toString() private String modelEncoding = "UTF-8"; - public void setModelEncoding(String modelEncoding) { + public void setModelEncoding(final String modelEncoding) { this.modelEncoding = modelEncoding; } diff --git a/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlan.java b/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlan.java index 067d415b..e44d0eb4 100644 --- a/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlan.java +++ b/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlan.java @@ -35,15 +35,15 @@ public class GenerationPlan { private File importVocabTokenTypesDirectory; private boolean outOfDate; - private LinkedHashSet collectedSuperGrammarIds = new LinkedHashSet<>(); + private final LinkedHashSet collectedSuperGrammarIds = new LinkedHashSet<>(); - GenerationPlan(String id, File source, File generationDirectory, String[] glibIds) { + GenerationPlan(final String id, final File source, final File generationDirectory, final String[] glibIds) { this.id = id; this.source = source; this.generationDirectory = generationDirectory; if (glibIds != null) { - for (int i = 0; i < glibIds.length; i++) { - addSuperGrammarId(glibIds[i]); + for (final String glibId : glibIds) { + addSuperGrammarId(glibId); } } } @@ -60,7 +60,7 @@ public File getGenerationDirectory() { return generationDirectory; } - void addSuperGrammarId(String id) { + void addSuperGrammarId(final String id) { collectedSuperGrammarIds.add(id); } @@ -72,7 +72,7 @@ public File getImportVocabTokenTypesDirectory() { return importVocabTokenTypesDirectory; } - void setImportVocabTokenTypesDirectory(File importVocabTokenTypesDirectory) { + void setImportVocabTokenTypesDirectory(final File importVocabTokenTypesDirectory) { this.importVocabTokenTypesDirectory = importVocabTokenTypesDirectory; } diff --git a/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlanBuilder.java b/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlanBuilder.java index 4831b0e1..065aca39 100644 --- a/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlanBuilder.java +++ b/src/main/java/org/codehaus/mojo/antlr/plan/GenerationPlanBuilder.java @@ -43,14 +43,14 @@ public class GenerationPlanBuilder { private XRef metadataXRef; - public GenerationPlanBuilder(Environment environment) { + public GenerationPlanBuilder(final Environment environment) { this.environment = environment; } public synchronized List buildGenerationPlans(XRef metadataXRef) { this.metadataXRef = metadataXRef; - Iterator grammarFiles = metadataXRef.iterateGrammarFiles(); + final Iterator grammarFiles = metadataXRef.iterateGrammarFiles(); while (grammarFiles.hasNext()) { final GrammarFile grammarFile = (GrammarFile) grammarFiles.next(); // NOTE : loacteOrBuildGenerationPlan populates the generationPlans map @@ -61,7 +61,7 @@ public synchronized List buildGenerationPlans(XRef metadataXRef) { return new ArrayList(generationPlans.values()); } - private GenerationPlan loacteOrBuildGenerationPlan(GrammarFile grammarFile) { + private GenerationPlan loacteOrBuildGenerationPlan(final GrammarFile grammarFile) { GenerationPlan generationPlan = (GenerationPlan) generationPlans.get(grammarFile.getId()); if (generationPlan == null) { generationPlan = buildGenerationPlan(grammarFile); @@ -69,14 +69,14 @@ private GenerationPlan loacteOrBuildGenerationPlan(GrammarFile grammarFile) { return generationPlan; } - private GenerationPlan buildGenerationPlan(GrammarFile grammarFile) { - File generationDirectory = StringUtils.isEmpty(grammarFile.getPackageName()) ? environment.getOutputDirectory() + private GenerationPlan buildGenerationPlan(final GrammarFile grammarFile) { + final File generationDirectory = StringUtils.isEmpty(grammarFile.getPackageName()) ? environment.getOutputDirectory() : new File(environment.getOutputDirectory(), grammarFile.getPackageName().replace('.', File.separatorChar)); - GenerationPlan generationPlan = new GenerationPlan(grammarFile.getId(), new File(grammarFile.getFileName()), generationDirectory, + final GenerationPlan generationPlan = new GenerationPlan(grammarFile.getId(), new File(grammarFile.getFileName()), generationDirectory, grammarFile.getGlibs()); - File leastRecentGrammarOutput = locateLeastRecentlyModifiedOutputFile(generationDirectory); + final File leastRecentGrammarOutput = locateLeastRecentlyModifiedOutputFile(generationDirectory); // see if the grammar is out-of-date by way super-grammars from user defined glib options for (int i = 0; i < grammarFile.getGlibs().length; i++) { @@ -89,9 +89,7 @@ private GenerationPlan buildGenerationPlan(GrammarFile grammarFile) { } } - Iterator grammars = grammarFile.getGrammars().iterator(); - while (grammars.hasNext()) { - final Grammar grammar = (Grammar) grammars.next(); + for (final Grammar grammar : grammarFile.getGrammars()) { final File generatedParserFile = new File(environment.getOutputDirectory(), grammar.determineGeneratedParserPath()); if (!generatedParserFile.exists()) { @@ -138,12 +136,12 @@ private GenerationPlan buildGenerationPlan(GrammarFile grammarFile) { return generationPlan; } - private static File locateLeastRecentlyModifiedOutputFile(File directory) { + private static File locateLeastRecentlyModifiedOutputFile(final File directory) { if (!directory.exists()) { return null; } - File[] contents = directory.listFiles(); + final File[] contents = directory.listFiles(); if (contents.length == 0) { return null; } diff --git a/src/main/java/org/codehaus/mojo/antlr/proxy/Helper.java b/src/main/java/org/codehaus/mojo/antlr/proxy/Helper.java index 0cec8396..2dd85090 100644 --- a/src/main/java/org/codehaus/mojo/antlr/proxy/Helper.java +++ b/src/main/java/org/codehaus/mojo/antlr/proxy/Helper.java @@ -48,10 +48,10 @@ public class Helper { private final Class antlrOptionClass; private final Class antlrIndexedVectorClass; - public Helper(Artifact antlrArtifact) throws MojoExecutionException { + public Helper(final Artifact antlrArtifact) throws MojoExecutionException { try { this.antlrClassLoader = new URLClassLoader(new URL[] { antlrArtifact.getFile().toURL() }, ClassLoader.getSystemClassLoader()); - } catch (MalformedURLException e) { + } catch (final MalformedURLException e) { throw new MojoExecutionException("Unable to resolve antlr:antlr artifact url", e); } @@ -64,10 +64,10 @@ public Helper(Artifact antlrArtifact) throws MojoExecutionException { antlrIndexedVectorClass = loadAntlrClass("antlr.collections.impl.IndexedVector"); } - private Class loadAntlrClass(String className) throws MojoExecutionException { + private Class loadAntlrClass(final String className) throws MojoExecutionException { try { return antlrClassLoader.loadClass(className); - } catch (ClassNotFoundException e) { + } catch (final ClassNotFoundException e) { throw new MojoExecutionException("could not load Antlr class :" + className, e); } } diff --git a/src/main/java/org/codehaus/mojo/antlr/proxy/Tool.java b/src/main/java/org/codehaus/mojo/antlr/proxy/Tool.java index 89ca859c..178814c0 100644 --- a/src/main/java/org/codehaus/mojo/antlr/proxy/Tool.java +++ b/src/main/java/org/codehaus/mojo/antlr/proxy/Tool.java @@ -32,10 +32,10 @@ public class Tool { private final Object antlrTool; - public Tool(Environment environment, Helper helper) throws MojoExecutionException { + public Tool(final Environment environment, final Helper helper) throws MojoExecutionException { try { antlrTool = helper.getAntlrToolClass().newInstance(); - } catch (Throwable t) { + } catch (final Throwable t) { throw new MojoExecutionException("Unable to instantiate antlr.Tool", t); } this.environment = environment;