dogfood, whitespaces

This commit is contained in:
Andreas Dangel
2015-03-24 22:48:01 +01:00
parent 056b2fbaea
commit 61e8412afe
8 changed files with 912 additions and 919 deletions

View File

@ -81,7 +81,6 @@ public class PMDParameters {
private static final char SEPARATOR = '=';
public Properties convert(String value) {
Properties properties = new Properties();
int indexOfSeparator = value.indexOf(SEPARATOR);
if (indexOfSeparator < 0) {
throw new ParameterException(
@ -89,6 +88,7 @@ public class PMDParameters {
}
String propertyName = value.substring(0, indexOfSeparator);
String propertyValue = value.substring(indexOfSeparator + 1);
Properties properties = new Properties();
properties.put(propertyName, propertyValue);
return properties;
}

View File

@ -12,44 +12,50 @@ import java.util.List;
*/
public abstract class AbstractTokenizer implements Tokenizer {
//FIXME depending on subclasses to assign local vars is rather fragile - better to make private and setup via explicit hook methods
protected List<String> stringToken; // List<String>, should be set by sub classes
protected List<String> ignorableCharacter; // List<String>, should be set by sub classes
// FIXME:Maybe an array of 'char' would be better for performance ?
protected List<String> ignorableStmt; // List<String>, should be set by sub classes
protected char oneLineCommentChar = '#'; // Most script languages ( shell, ruby, python,...) use this symbol for comment line
// FIXME depending on subclasses to assign local vars is rather fragile -
// better to make private and setup via explicit hook methods
private List<String> code;
private int lineNumber = 0;
private String currentLine;
protected List<String> stringToken; // List<String>, should be set by sub
// classes
protected List<String> ignorableCharacter; // List<String>, should be set by
// sub classes
// FIXME:Maybe an array of 'char'
// would be better for
// performance ?
protected List<String> ignorableStmt; // List<String>, should be set by sub
// classes
protected char oneLineCommentChar = '#'; // Most script languages ( shell,
// ruby, python,...) use this
// symbol for comment line
protected boolean spanMultipleLinesString = true; // Most languages do, so default is true
protected Character spanMultipleLinesLineContinuationCharacter = null;
private List<String> code;
private int lineNumber = 0;
private String currentLine;
private boolean downcaseString = true;
protected boolean spanMultipleLinesString = true; // Most languages do, so
// default is true
protected Character spanMultipleLinesLineContinuationCharacter = null;
private boolean downcaseString = true;
public void tokenize(SourceCode tokens, Tokens tokenEntries) {
code = tokens.getCode();
for ( lineNumber = 0; lineNumber < code.size(); lineNumber++ ) {
currentLine = code.get(lineNumber);
for (lineNumber = 0; lineNumber < code.size(); lineNumber++) {
currentLine = code.get(lineNumber);
int loc = 0;
while ( loc < currentLine.length() ) {
while (loc < currentLine.length()) {
StringBuilder token = new StringBuilder();
loc = getTokenFromLine(token,loc);
loc = getTokenFromLine(token, loc);
if (token.length() > 0 && !isIgnorableString(token.toString())) {
if (downcaseString) {
token = new StringBuilder(token.toString().toLowerCase());
}
// need to re-think how to link this
// if ( CPD.debugEnable ) {
// System.out.println("Token added:" + token.toString());
// }
tokenEntries.add(new TokenEntry(token.toString(),
tokens.getFileName(),
lineNumber)
);
// need to re-think how to link this
// if ( CPD.debugEnable ) {
// System.out.println("Token added:" + token.toString());
// }
tokenEntries.add(new TokenEntry(token.toString(), tokens.getFileName(), lineNumber));
}
}
@ -69,7 +75,8 @@ public abstract class AbstractTokenizer implements Tokenizer {
}
} else if (isString(tok)) {
if (token.length() > 0) {
return j; // we need to now parse the string as a separate token.
return j; // we need to now parse the string as a
// separate token.
} else {
// we are at the start of a string
return parseString(token, j, tok);
@ -91,66 +98,67 @@ public abstract class AbstractTokenizer implements Tokenizer {
boolean escaped = false;
boolean done = false;
char tok = ' '; // this will be replaced.
while ((loc < currentLine.length()) && ! done) {
while (loc < currentLine.length() && !done) {
tok = currentLine.charAt(loc);
if (escaped && tok == stringDelimiter) { // Found an escaped string
escaped = false;
} else if (tok == stringDelimiter && (token.length() > 0)) { // We are done, we found the end of the string...
} else if (tok == stringDelimiter && token.length() > 0) {
// We are done, we found the end of the string...
done = true;
} else if (tok == '\\') { // Found an escaped char
escaped = true;
} else { // Adding char...
} else { // Adding char...
escaped = false;
}
//Adding char to String:" + token.toString());
// Adding char to String:" + token.toString());
token.append(tok);
loc++;
}
// Handling multiple lines string
if ( ! done && // ... we didn't find the end of the string
loc >= currentLine.length() && // ... we have reach the end of the line ( the String is incomplete, for the moment at least)
spanMultipleLinesString && // ... the language allow multiple line span Strings
lineNumber < code.size() - 1 // ... there is still more lines to parse
) {
// removes last character, if it is the line continuation (e.g. backslash) character
if (!done && // ... we didn't find the end of the string
loc >= currentLine.length() && // ... we have reach the end of
// the line ( the String is
// incomplete, for the moment at
// least)
spanMultipleLinesString && // ... the language allow multiple
// line span Strings
lineNumber < code.size() - 1 // ... there is still more lines to
// parse
) {
// removes last character, if it is the line continuation (e.g.
// backslash) character
if (spanMultipleLinesLineContinuationCharacter != null && token.length() > 0
&& token.charAt(token.length() - 1) == spanMultipleLinesLineContinuationCharacter.charValue()) {
token.deleteCharAt(token.length() - 1);
}
// parsing new line
currentLine = code.get(++lineNumber);
// Warning : recursive call !
loc = parseString(token, 0, stringDelimiter);
// parsing new line
currentLine = code.get(++lineNumber);
// Warning : recursive call !
loc = parseString(token, 0, stringDelimiter);
}
return loc + 1;
}
private boolean ignoreCharacter(char tok)
{
return ignorableCharacter.contains(String.valueOf(tok));
private boolean ignoreCharacter(char tok) {
return ignorableCharacter.contains(String.valueOf(tok));
}
private boolean isString(char tok)
{
return stringToken.contains(String.valueOf(tok));
private boolean isString(char tok) {
return stringToken.contains(String.valueOf(tok));
}
private boolean isComment(char tok)
{
private boolean isComment(char tok) {
return tok == oneLineCommentChar;
}
private int getCommentToken(StringBuilder token, int loc)
{
while (loc < currentLine.length())
{
private int getCommentToken(StringBuilder token, int loc) {
while (loc < currentLine.length()) {
token.append(currentLine.charAt(loc++));
}
return loc;
}
private boolean isIgnorableString(String token)
{
return ignorableStmt.contains(token);
private boolean isIgnorableString(String token) {
return ignorableStmt.contains(token);
}
}

View File

@ -15,7 +15,7 @@ import java.util.ServiceLoader;
*/
public final class LanguageRegistry {
private static LanguageRegistry instance;
private static LanguageRegistry instance = new LanguageRegistry();
private Map<String, Language> languages;
@ -27,10 +27,7 @@ public final class LanguageRegistry {
}
}
protected static LanguageRegistry getInstance() {
if(instance == null) {
instance = new LanguageRegistry();
}
public static LanguageRegistry getInstance() {
return instance;
}
@ -57,13 +54,13 @@ public final class LanguageRegistry {
public static LanguageVersion findLanguageVersionByTerseName(String terseName) {
String version = null;
if(terseName.contains(" ")) {
if (terseName.contains(" ")) {
version = terseName.substring(terseName.lastIndexOf(' ') + 1);
terseName = terseName.substring(0, terseName.lastIndexOf(' '));
}
Language language = findLanguageByTerseName(terseName);
if(language != null) {
if(version == null) {
if (language != null) {
if (version == null) {
return language.getDefaultVersion();
} else {
return language.getVersion(version);
@ -84,8 +81,8 @@ public final class LanguageRegistry {
public static List<LanguageVersion> findAllVersions() {
List<LanguageVersion> versions = new ArrayList<LanguageVersion>();
for(Language language : getLanguages()) {
for(LanguageVersion languageVersion : language.getVersions()) {
for (Language language : getLanguages()) {
for (LanguageVersion languageVersion : language.getVersions()) {
versions.add(languageVersion);
}
}
@ -94,6 +91,7 @@ public final class LanguageRegistry {
/**
* A utility method to find the Languages which have Rule support.
*
* @return A List of Languages with Rule support.
*/
public static List<Language> findWithRuleSupport() {
@ -124,7 +122,7 @@ public final class LanguageRegistry {
StringBuilder builder = new StringBuilder();
builder.append(languageVersions.get(0).getTerseName());
for (int i=1; i<languageVersions.size(); i++) {
for (int i = 1; i < languageVersions.size(); i++) {
builder.append(", ").append(languageVersions.get(i).getTerseName());
}
return builder.toString();

View File

@ -44,43 +44,45 @@ public class NodeType {
// TODO - throw statements?
public static final int THROW_STATEMENT = 70;
//Poor Man's Enum until we convert the class to real enum
// Poor Man's Enum until we convert the class to real enum
private static final Map<Integer, String> TYPE_MAP = new HashMap<Integer, String>();
static {
TYPE_MAP.put(NodeType.IF_EXPR, "IF_EXPR");
TYPE_MAP.put(NodeType.IF_LAST_STATEMENT, "IF_LAST_STATEMENT");
TYPE_MAP.put(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, "IF_LAST_STATEMENT_WITHOUT_ELSE");
TYPE_MAP.put(NodeType.ELSE_LAST_STATEMENT, "ELSE_LAST_STATEMENT");
TYPE_MAP.put(NodeType.WHILE_LAST_STATEMENT, "WHILE_LAST_STATEMENT");
TYPE_MAP.put(NodeType.WHILE_EXPR, "WHILE_EXPR");
TYPE_MAP.put(NodeType.SWITCH_START, "SWITCH_START");
TYPE_MAP.put(NodeType.CASE_LAST_STATEMENT, "CASE_LAST_STATEMENT");
TYPE_MAP.put(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, "SWITCH_LAST_DEFAULT_STATEMENT");
TYPE_MAP.put(NodeType.SWITCH_END, "SWITCH_END");
TYPE_MAP.put(NodeType.FOR_INIT, "FOR_INIT");
TYPE_MAP.put(NodeType.FOR_EXPR, "FOR_EXPR");
TYPE_MAP.put(NodeType.FOR_UPDATE, "FOR_UPDATE");
TYPE_MAP.put(NodeType.FOR_BEFORE_FIRST_STATEMENT, "FOR_BEFORE_FIRST_STATEMENT");
TYPE_MAP.put(NodeType.FOR_END, "FOR_END");
TYPE_MAP.put(NodeType.DO_BEFORE_FIRST_STATEMENT, "DO_BEFORE_FIRST_STATEMENT");
TYPE_MAP.put(NodeType.DO_EXPR, "DO_EXPR");
TYPE_MAP.put(NodeType.RETURN_STATEMENT, "RETURN_STATEMENT");
TYPE_MAP.put(NodeType.BREAK_STATEMENT, "BREAK_STATEMENT");
TYPE_MAP.put(NodeType.CONTINUE_STATEMENT, "CONTINUE_STATEMENT");
TYPE_MAP.put(NodeType.LABEL_STATEMENT, "LABEL_STATEMENT");
TYPE_MAP.put(NodeType.LABEL_LAST_STATEMENT, "LABEL_END");
TYPE_MAP.put(NodeType.THROW_STATEMENT, "THROW_STATEMENT");
TYPE_MAP.put(NodeType.IF_EXPR, "IF_EXPR");
TYPE_MAP.put(NodeType.IF_LAST_STATEMENT, "IF_LAST_STATEMENT");
TYPE_MAP.put(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, "IF_LAST_STATEMENT_WITHOUT_ELSE");
TYPE_MAP.put(NodeType.ELSE_LAST_STATEMENT, "ELSE_LAST_STATEMENT");
TYPE_MAP.put(NodeType.WHILE_LAST_STATEMENT, "WHILE_LAST_STATEMENT");
TYPE_MAP.put(NodeType.WHILE_EXPR, "WHILE_EXPR");
TYPE_MAP.put(NodeType.SWITCH_START, "SWITCH_START");
TYPE_MAP.put(NodeType.CASE_LAST_STATEMENT, "CASE_LAST_STATEMENT");
TYPE_MAP.put(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, "SWITCH_LAST_DEFAULT_STATEMENT");
TYPE_MAP.put(NodeType.SWITCH_END, "SWITCH_END");
TYPE_MAP.put(NodeType.FOR_INIT, "FOR_INIT");
TYPE_MAP.put(NodeType.FOR_EXPR, "FOR_EXPR");
TYPE_MAP.put(NodeType.FOR_UPDATE, "FOR_UPDATE");
TYPE_MAP.put(NodeType.FOR_BEFORE_FIRST_STATEMENT, "FOR_BEFORE_FIRST_STATEMENT");
TYPE_MAP.put(NodeType.FOR_END, "FOR_END");
TYPE_MAP.put(NodeType.DO_BEFORE_FIRST_STATEMENT, "DO_BEFORE_FIRST_STATEMENT");
TYPE_MAP.put(NodeType.DO_EXPR, "DO_EXPR");
TYPE_MAP.put(NodeType.RETURN_STATEMENT, "RETURN_STATEMENT");
TYPE_MAP.put(NodeType.BREAK_STATEMENT, "BREAK_STATEMENT");
TYPE_MAP.put(NodeType.CONTINUE_STATEMENT, "CONTINUE_STATEMENT");
TYPE_MAP.put(NodeType.LABEL_STATEMENT, "LABEL_STATEMENT");
TYPE_MAP.put(NodeType.LABEL_LAST_STATEMENT, "LABEL_END");
TYPE_MAP.put(NodeType.THROW_STATEMENT, "THROW_STATEMENT");
}
public static Map<Integer, String> getTypeMap() {
return TYPE_MAP;
}
return TYPE_MAP;
}
public static String stringFromType(int intype) {
if(-1 == intype) { return "<ROOT>" ; }
if (!TYPE_MAP.containsKey(intype) ) {
throw new RuntimeException("Couldn't find NodeType type id " + intype);
}
return TYPE_MAP.get(intype);
}
if (-1 == intype) {
return "<ROOT>";
}
if (!TYPE_MAP.containsKey(intype)) {
throw new RuntimeException("Couldn't find NodeType type id " + intype);
}
return TYPE_MAP.get(intype);
}
}

View File

@ -183,7 +183,9 @@ public class SequenceChecker {
* StackObjects, preventing infinite loops within the SequenceChecker.
*/
int maximumIterations = this.bracesList.size() * this.bracesList.size();
for (int i = 0, l = 0; i < this.bracesList.size(); l++, i++) {
int l = -1;
for (int i = 0; i < this.bracesList.size(); i++) {
l++;
StackObject so = bracesList.get(i);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("Processing bracesList(l,i)=(" + l + "," + i + ") of Type "
@ -196,7 +198,9 @@ public class SequenceChecker {
// Attempt to get to this StackObject's NodeType from the current
// State
aktStatus = this.aktStatus.step(so.getType());
LOGGER.finest("Transition aktStatus=" + aktStatus);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("Transition aktStatus=" + aktStatus);
}
if (aktStatus == null) { // Not a valid Node from the current State
if (lookAhead) {