Browse Source

Improve code quality of problematic classes.

Timur Sağlam 4 years ago
parent
commit
99529cc201

+ 90 - 82
jplag.frontend.csharp-6/src/main/java/de/jplag/csharp/grammar/CSharpPreprocessorParserBase.java

@@ -8,174 +8,182 @@ import org.antlr.v4.runtime.Parser;
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.antlr.v4.runtime.TokenStream;
 import org.antlr.v4.runtime.TokenStream;
 
 
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorConditionalContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorDeclarationContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorDiagnosticContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorLineContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorNullableContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorPragmaContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.PreprocessorRegionContext;
+import de.jplag.csharp.grammar.CSharpPreprocessorParser.Preprocessor_expressionContext;
+
 /**
 /**
  * This class was taken from <a href="https://github.com/antlr/grammars-v4/tree/master/csharp">antlr/grammars-v4</a>. It
  * This class was taken from <a href="https://github.com/antlr/grammars-v4/tree/master/csharp">antlr/grammars-v4</a>. It
  * was originally written by Ken Domino. Note that this class is licensed under Eclipse Public License - v 1.0.
  * was originally written by Ken Domino. Note that this class is licensed under Eclipse Public License - v 1.0.
  */
  */
-@SuppressWarnings("java:S100")
 abstract class CSharpPreprocessorParserBase extends Parser {
 abstract class CSharpPreprocessorParserBase extends Parser {
+    private static final String FALSE = Boolean.toString(false);
+    private static final String TRUE = Boolean.toString(true);
+    private static final String DEBUG = "DEBUG";
+
+    private Stack<Boolean> conditions = new Stack<>();
+    private HashSet<String> ConditionalSymbols = new HashSet<>();
+
     protected CSharpPreprocessorParserBase(TokenStream input) {
     protected CSharpPreprocessorParserBase(TokenStream input) {
         super(input);
         super(input);
         conditions.push(true);
         conditions.push(true);
-        ConditionalSymbols.add("DEBUG");
+        ConditionalSymbols.add(DEBUG);
     }
     }
 
 
-    Stack<Boolean> conditions = new Stack<>();
-    public HashSet<String> ConditionalSymbols = new HashSet<String>();
-
     protected Boolean AllConditions() {
     protected Boolean AllConditions() {
-        for (Boolean condition : conditions) {
-            if (!condition)
-                return false;
-        }
-        return true;
+        return conditions.stream().allMatch(it -> it);
     }
     }
 
 
     protected void OnPreprocessorDirectiveDefine() {
     protected void OnPreprocessorDirectiveDefine() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorDeclarationContext d = (CSharpPreprocessorParser.PreprocessorDeclarationContext) c;
-        ConditionalSymbols.add(d.CONDITIONAL_SYMBOL().getText());
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorDeclarationContext context = (PreprocessorDeclarationContext) ruleContext;
+        ConditionalSymbols.add(context.CONDITIONAL_SYMBOL().getText());
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveUndef() {
     protected void OnPreprocessorDirectiveUndef() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorDeclarationContext d = (CSharpPreprocessorParser.PreprocessorDeclarationContext) c;
-        ConditionalSymbols.remove(d.CONDITIONAL_SYMBOL().getText());
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorDeclarationContext context = (PreprocessorDeclarationContext) ruleContext;
+        ConditionalSymbols.remove(context.CONDITIONAL_SYMBOL().getText());
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveIf() {
     protected void OnPreprocessorDirectiveIf() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
-        d.value = d.expr.value.equals("true") && AllConditions();
-        conditions.push(d.expr.value.equals("true"));
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorConditionalContext context = (PreprocessorConditionalContext) ruleContext;
+        context.value = context.expr.value.equals(TRUE) && AllConditions();
+        conditions.push(context.expr.value.equals(TRUE));
     }
     }
 
 
     protected void OnPreprocessorDirectiveElif() {
     protected void OnPreprocessorDirectiveElif() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorConditionalContext context = (PreprocessorConditionalContext) ruleContext;
         if (Boolean.FALSE.equals(conditions.peek())) {
         if (Boolean.FALSE.equals(conditions.peek())) {
             conditions.pop();
             conditions.pop();
-            d.value = d.expr.value.equals("true") && AllConditions();
-            conditions.push(d.expr.value.equals("true"));
+            context.value = context.expr.value.equals(TRUE) && AllConditions();
+            conditions.push(context.expr.value.equals(TRUE));
         } else {
         } else {
-            d.value = false;
+            context.value = false;
         }
         }
     }
     }
 
 
     protected void OnPreprocessorDirectiveElse() {
     protected void OnPreprocessorDirectiveElse() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorConditionalContext context = (PreprocessorConditionalContext) ruleContext;
         if (Boolean.FALSE.equals(conditions.peek())) {
         if (Boolean.FALSE.equals(conditions.peek())) {
             conditions.pop();
             conditions.pop();
-            d.value = AllConditions();
+            context.value = AllConditions();
             conditions.push(true);
             conditions.push(true);
         } else {
         } else {
-            d.value = false;
+            context.value = false;
         }
         }
     }
     }
 
 
     protected void OnPreprocessorDirectiveEndif() {
     protected void OnPreprocessorDirectiveEndif() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorConditionalContext context = (PreprocessorConditionalContext) ruleContext;
         conditions.pop();
         conditions.pop();
-        d.value = conditions.peek();
+        context.value = conditions.peek();
     }
     }
 
 
     protected void OnPreprocessorDirectiveLine() {
     protected void OnPreprocessorDirectiveLine() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorLineContext d = (CSharpPreprocessorParser.PreprocessorLineContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorLineContext context = (PreprocessorLineContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveError() {
     protected void OnPreprocessorDirectiveError() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorDiagnosticContext d = (CSharpPreprocessorParser.PreprocessorDiagnosticContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorDiagnosticContext context = (PreprocessorDiagnosticContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveWarning() {
     protected void OnPreprocessorDirectiveWarning() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorDiagnosticContext d = (CSharpPreprocessorParser.PreprocessorDiagnosticContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorDiagnosticContext context = (PreprocessorDiagnosticContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveRegion() {
     protected void OnPreprocessorDirectiveRegion() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorRegionContext d = (CSharpPreprocessorParser.PreprocessorRegionContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorRegionContext context = (PreprocessorRegionContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveEndregion() {
     protected void OnPreprocessorDirectiveEndregion() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorRegionContext d = (CSharpPreprocessorParser.PreprocessorRegionContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorRegionContext context = (PreprocessorRegionContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectivePragma() {
     protected void OnPreprocessorDirectivePragma() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorPragmaContext d = (CSharpPreprocessorParser.PreprocessorPragmaContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorPragmaContext context = (PreprocessorPragmaContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorDirectiveNullable() {
     protected void OnPreprocessorDirectiveNullable() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.PreprocessorNullableContext d = (CSharpPreprocessorParser.PreprocessorNullableContext) c;
-        d.value = AllConditions();
+        ParserRuleContext ruleContext = this._ctx;
+        PreprocessorNullableContext context = (PreprocessorNullableContext) ruleContext;
+        context.value = AllConditions();
     }
     }
 
 
     protected void OnPreprocessorExpressionTrue() {
     protected void OnPreprocessorExpressionTrue() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = "true";
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = TRUE;
     }
     }
 
 
     protected void OnPreprocessorExpressionFalse() {
     protected void OnPreprocessorExpressionFalse() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = "false";
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = FALSE;
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalSymbol() {
     protected void OnPreprocessorExpressionConditionalSymbol() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = ConditionalSymbols.contains(d.CONDITIONAL_SYMBOL().getText()) ? "true" : "false";
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = ConditionalSymbols.contains(context.CONDITIONAL_SYMBOL().getText()) ? TRUE : FALSE;
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalOpenParens() {
     protected void OnPreprocessorExpressionConditionalOpenParens() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = d.expr.value;
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = context.expr.value;
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalBang() {
     protected void OnPreprocessorExpressionConditionalBang() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = d.expr.value.equals("true") ? "false" : "true";
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = context.expr.value.equals(TRUE) ? FALSE : TRUE;
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalEq() {
     protected void OnPreprocessorExpressionConditionalEq() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = (Objects.equals(d.expr1.value, d.expr2.value) ? "true" : "false");
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = (Objects.equals(context.expr1.value, context.expr2.value) ? TRUE : FALSE);
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalNe() {
     protected void OnPreprocessorExpressionConditionalNe() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = (!Objects.equals(d.expr1.value, d.expr2.value) ? "true" : "false");
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = (!Objects.equals(context.expr1.value, context.expr2.value) ? TRUE : FALSE);
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalAnd() {
     protected void OnPreprocessorExpressionConditionalAnd() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = (d.expr1.value.equals("true") && d.expr2.value.equals("true") ? "true" : "false");
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = (context.expr1.value.equals(TRUE) && context.expr2.value.equals(TRUE) ? TRUE : FALSE);
     }
     }
 
 
     protected void OnPreprocessorExpressionConditionalOr() {
     protected void OnPreprocessorExpressionConditionalOr() {
-        ParserRuleContext c = this._ctx;
-        CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
-        d.value = (d.expr1.value.equals("true") || d.expr2.value.equals("true") ? "true" : "false");
+        ParserRuleContext ruleContext = this._ctx;
+        Preprocessor_expressionContext context = (Preprocessor_expressionContext) ruleContext;
+        context.value = (context.expr1.value.equals(TRUE) || context.expr2.value.equals(TRUE) ? TRUE : FALSE);
     }
     }
 }
 }

+ 7 - 10
jplag/src/main/java/de/jplag/clustering/IntegerMapping.java

@@ -20,18 +20,15 @@ public class IntegerMapping<T> {
     }
     }
 
 
     /**
     /**
-     * @param value is added to the mapping (if not already present)
+     * @param key is added to the mapping (if not already present)
      * @return the associated integer
      * @return the associated integer
      */
      */
-    public int map(T value) {
-        Integer result = mapping.get(value);
-        if (result == null) {
-            int newIndex = size++;
-            mapping.put(value, newIndex);
-            backMapping.add(value);
-            return newIndex;
-        }
-        return result;
+    public int map(T key) {
+        mapping.computeIfAbsent(key, it -> {
+            backMapping.add(it);
+            return size++;
+        });
+        return mapping.get(key);
     }
     }
 
 
     /**
     /**