diff options
author | Andrei Selkin <andreyselkin@gmail.com> | 2016-03-26 22:21:25 +0300 |
---|---|---|
committer | Roman Ivanov <ivanov-jr@mail.ru> | 2016-03-29 15:47:31 -0700 |
commit | 62c19a216f208a75158fd426523d23d1a98b70cb (patch) | |
tree | 15b6f56d5e6f3868f8b373ec9bb5ba2034f7a445 /src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding | |
parent | 55aa50d8f09d28521f0d0004ce475fa09082faea (diff) | |
download | checkstyle-62c19a216f208a75158fd426523d23d1a98b70cb.tar.gz |
Issue #3006: Add test inputs taken from Guava and Findbugs
Diffstat (limited to 'src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding')
-rw-r--r-- | src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableAssignedMultipleTimes.java | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableAssignedMultipleTimes.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableAssignedMultipleTimes.java index f7b126f22..fb9678189 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableAssignedMultipleTimes.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputFinalLocalVariableAssignedMultipleTimes.java @@ -1,5 +1,10 @@ package com.puppycrawl.tools.checkstyle.checks.coding; +import java.util.HashMap; +import java.util.SortedMap; + +import com.google.common.collect.Maps; + public class InputFinalLocalVariableAssignedMultipleTimes { void foo1() { @@ -111,4 +116,330 @@ public class InputFinalLocalVariableAssignedMultipleTimes { } } + + // Taken from findbugs + public static String foo9(String filePath, String project) { + String path = new String(filePath); // violation + String commonPath; + if (project != null) { + commonPath = ""; + String relativePath = ""; // violation + if (!relativePath.equals(path)) { + return relativePath; + } + } + commonPath = ""; + return commonPath; + } + + // Taken from findbugs + public int foo10(String factory1, String factory2) { + int result = 0; + String s1, s2; + switch (result) { + case 1: + s1 = "Java"; + s2 = "C#"; + break; + case 2: + s1 = "C++"; + s2 = "Pascal"; + ; + break; + case 3: + s1 = "Basic"; + s2 = "Angol"; + break; + case 4: + s1 = "JavaScript"; + s2 = "Go"; + break; + case 5: + default: + s1 = "F#"; + s2 = "Objective C"; + break; + } + if (s1 == null) { + s1 = "incorrect language"; + } + if (s2 == null) { + s2 = "incorrect language"; + } + result = s1.compareTo(s2); + + if (result == 0) { + switch (result) { + case 1: + s1 = "Java"; + s2 = "C#"; + break; + case 2: + default: + s1 = "C++"; + s2 = "C"; + break; + } + result = s1.compareTo(s2); + } + else if (result == 1) { + result = -8; + } + return result; + } + + // Taken from findbugs + public String foo11(final Object o, boolean getMinimal) { + + String n = System.lineSeparator(); + if (n != null) { + return n; + } + + try { + + String className; + int kind; // violation + boolean isParameterToInitMethodofAnonymousInnerClass = false; + boolean isSyntheticMethod = false; + if (o instanceof String || o instanceof Integer) { + + String m; // violation + if (o instanceof String) { + m = (String) o; + isSyntheticMethod = m.equals(""); + kind = 1; + className = this.getClass().getName(); + } + else if (o instanceof String) { + m = ""; + // Don't + isSyntheticMethod = m.equals(""); + className = this.getClass().getName(); + kind = 2; + if ("<init>".equals(m.toLowerCase())) { + final int i = className.lastIndexOf('$'); + if (i + 1 < className.length() && Character.isDigit(className.charAt(i + 1))) { + isParameterToInitMethodofAnonymousInnerClass = true; + } + } + } + else { + throw new IllegalStateException("impossible"); + } + + if (!m.equals("") && !"<init>".equals(m.toLowerCase())) { + final String c = "className"; + // get inherited annotation + String inheritedAnnotations = new String(); + if (c.charAt(1) > 0) { + + n = ""; + if (n != null) { + inheritedAnnotations += ""; + } + } + for (int i = 5; i < 10; i++) { + n = new String(""); + if (n != null) { + inheritedAnnotations += ""; + } + } + if (n == null) { + System.out.println("# of inherited annotations : " + inheritedAnnotations.length()); + } + if (!inheritedAnnotations.isEmpty()) { + if (inheritedAnnotations.length() == 1) { + return inheritedAnnotations; + } + if (!getMinimal) { + return inheritedAnnotations; + } + + String min = inheritedAnnotations; + if (min.length() == 0) { + inheritedAnnotations = null; + min = inheritedAnnotations; + } + return min; + } + // check to see if method is defined in this class; + // if not, on't consider default annotations + if (inheritedAnnotations == null) { + return null; + } + if (inheritedAnnotations.equals("")) { + System.out.println("looking for default annotations: " + " defines " + m); + } + } // if not static + } // associated with method + else if (o instanceof String) { + + className = ((String) o).getClass().getName(); + kind = 3; + } + else if (o instanceof String) { + assert false; + className = (String) o; + kind = 4; + } + else { + throw new IllegalArgumentException("Can't look up annotation for " + o.getClass().getName()); + } + + // <init> method parameters for inner classes don't inherit default + // annotations + // since some of them are synthetic + if (isParameterToInitMethodofAnonymousInnerClass) { + return null; + } + + // synthetic elements should not inherit default annotations + if (isSyntheticMethod) { + return null; + } + try { + final String c = new String(className); + + if (c != null && c.equals("")) { + return null; + } + } + catch (Exception e) { + assert true; + } + + // look for default annotation + n = new String(className); + if (n == null) { + System.out.println("Default annotation for " + kind + " is " + n); + } + if (n != null) { + return n; + } + + n = new String(className); + if (n == null) { + System.out.println("Default annotation for any is " + n); + } + if (n != null) { + return n; + } + + final int p = className.lastIndexOf('.'); + className = className.substring(0, p + 1) + "package-info"; + n = new String(className); + if (n == null) { + System.out.println("Default annotation for " + kind + " is " + n); + } + if (n != null) { + return n; + } + + n = new String(className); + if (n == null) { + System.out.println("Default annotation for any is " + n); + } + if (n != null) { + return n; + } + + return n; + } + catch (Exception e) { + System.out.println(e); + ; + return null; + } + + } + + // Taken from findbugs + private void foo12(Long start, Long end) { + HashMap<Object, Object> headMap; + if (end < Long.MAX_VALUE) { + headMap = Maps.newHashMap(); + Long tailEnd = 1L; + if (tailEnd != null) { + end = tailEnd; + } + if (!headMap.isEmpty()) { + tailEnd = (Long) headMap.get(headMap.size()); + if (tailEnd > end) { + end = tailEnd; + } + } + } + headMap = Maps.newHashMap(); + if (!headMap.isEmpty()) { + final int headStart = headMap.size(); + final Long headEnd = (Long) headMap.get(headStart); + if (headEnd >= start - 1) { + headMap.remove(headStart); + start = Long.valueOf(headStart); + } + } + headMap.clear(); + headMap.remove(end); + headMap.put(start, end); + } + + // Taken from Guava + public static int foo13(int p, int q, int mode) { + System.out.println(mode); + ; + if (q == 0) { + throw new ArithmeticException("/ by zero"); // for GWT + } + final int div = p / q; + final int rem = p - q * div; // equal to p % q + + if (rem == 0) { + return div; + } + + /* + * Normal Java division rounds towards 0, consistently with RoundingMode.DOWN. We just have to + * deal with the cases where rounding towards 0 is wrong, which typically depends on the sign of + * p / q. + * + * signum is 1 if p and q are both nonnegative or both negative, and -1 otherwise. + */ + final int signum = 1 | ((p ^ q) >> (Integer.SIZE - 1)); + boolean increment; // violation + switch (mode) { + case 1: + System.out.println(rem == 0); + // fall through + case 2: + increment = false; + break; + case 3: + increment = true; + break; + case 4: + increment = signum > 0; + break; + case 5: + increment = signum < 0; + break; + case 6: + case 7: + case 8: + final int absRem = 1; + final int cmpRemToHalfDivisor = absRem - (1 - absRem); + // subtracting two nonnegative ints can't overflow + // cmpRemToHalfDivisor has the same sign as compare(abs(rem), abs(q) / 2). + if (cmpRemToHalfDivisor == 0) { // exactly on the half mark + increment = (mode == 1) || (mode == 2 & (div & 1) != 0); + } + else { + increment = cmpRemToHalfDivisor > 0; // closer to the UP value + } + break; + default: + throw new AssertionError(); + } + return increment ? div + signum : div; + } } |