From f6d5d565d1a45ca22403f918485afe07f3e2e654 Mon Sep 17 00:00:00 2001 From: Brian Remedios Date: Sat, 24 Sep 2011 09:32:02 +0000 Subject: [PATCH] restore static var, tighten property validation for AvoidDuplicateLiteralsRule git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7336 51baf565-9d33-0410-a72c-fc3788e3496d --- .../sourceforge/pmd/cpd/LanguageFactory.java | 4 +- .../strings/AvoidDuplicateLiteralsRule.java | 56 ++++++++++++++----- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/cpd/LanguageFactory.java b/pmd/src/net/sourceforge/pmd/cpd/LanguageFactory.java index 5e630e02d8..0be48e15ef 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/LanguageFactory.java +++ b/pmd/src/net/sourceforge/pmd/cpd/LanguageFactory.java @@ -7,7 +7,9 @@ import java.util.Properties; public class LanguageFactory { -// public static String[] supportedLanguages = new String[]{"java", "jsp", "cpp", "c", "php", "ruby","fortran", "ecmascript","cs" }; + // TODO derive and provide this at runtime instead, used by outside IDEs + public static String[] supportedLanguages = new String[]{"java", "jsp", "cpp", "c", "php", "ruby","fortran", "ecmascript","cs" }; + private static final String SUFFIX = "Language"; public static final String EXTENSION = "extension"; public static final String BY_EXTENSION = "by_extension"; diff --git a/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java b/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java index 1dfe14d6f8..82163df27a 100644 --- a/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java +++ b/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.rule.strings; import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; @@ -15,14 +16,18 @@ import java.util.List; import java.util.Map; import java.util.Set; +import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTLiteral; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.lang.rule.properties.BooleanProperty; import net.sourceforge.pmd.lang.rule.properties.CharacterProperty; +import net.sourceforge.pmd.lang.rule.properties.FileProperty; import net.sourceforge.pmd.lang.rule.properties.IntegerProperty; import net.sourceforge.pmd.lang.rule.properties.StringProperty; +import net.sourceforge.pmd.util.IOUtil; +import net.sourceforge.pmd.util.StringUtil; public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { @@ -36,13 +41,13 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { "Skip literals within annotations", false, 2.0f); public static final StringProperty EXCEPTION_LIST_DESCRIPTOR = new StringProperty("exceptionList", - "Strings in that list are skipped", null, 3.0f); + "Strings to ignore", null, 3.0f); public static final CharacterProperty SEPARATOR_DESCRIPTOR = new CharacterProperty("separator", - "Exception list separator", ',', 4.0f); + "Ignore list separator", ',', 4.0f); - public static final StringProperty EXCEPTION_FILE_DESCRIPTOR = new StringProperty("exceptionfile", - "File containing strings to skip (one string per line), only used if exceptionlist is not set", null, 5.0f); + public static final FileProperty EXCEPTION_FILE_DESCRIPTOR = new FileProperty("exceptionfile", + "File containing strings to skip (one string per line), only used if ignore list is not set", null, 5.0f); public static class ExceptionParser { @@ -94,6 +99,10 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { definePropertyDescriptor(EXCEPTION_FILE_DESCRIPTOR); } + private LineNumberReader getLineReader() throws FileNotFoundException { + return new LineNumberReader(new BufferedReader(new FileReader(getProperty(EXCEPTION_FILE_DESCRIPTOR)))); + } + @Override public Object visit(ASTCompilationUnit node, Object data) { literals.clear(); @@ -105,8 +114,7 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { exceptions = new HashSet(); LineNumberReader reader = null; try { - reader = new LineNumberReader(new BufferedReader(new FileReader(new File( - getProperty(EXCEPTION_FILE_DESCRIPTOR))))); + reader = getLineReader(); String line; while ((line = reader.readLine()) != null) { exceptions.add(line); @@ -114,13 +122,7 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { } catch (IOException ioe) { ioe.printStackTrace(); } finally { - try { - if (reader != null) { - reader.close(); - } - } catch (IOException ioe) { - ioe.printStackTrace(); - } + IOUtil.closeQuietly(reader); } } @@ -174,4 +176,32 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { return data; } + + private static String checkFile(File file) { + + if (!file.exists()) return "File '" + file.getName() + "' does not exist"; + if (!file.canRead()) return "File '" + file.getName() + "' cannot be read"; + if (file.length() == 0) return "File '" + file.getName() + "' is empty"; + + return null; + } + + /** + * @see Rule#dysfunctionReason() + */ + public String dysfunctionReason() { + + File file = getProperty(EXCEPTION_FILE_DESCRIPTOR); + if (file != null) { + String issue = checkFile(file); + if (issue != null) return issue; + + String ignores = getProperty(EXCEPTION_LIST_DESCRIPTOR); + if (StringUtil.isNotEmpty(ignores)) { + return "Cannot reference external file AND local values"; + } + } + + return null; + } }