Enable spotbugs in mdsal-binding-generator-util 06/76906/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 11 Oct 2018 18:43:43 +0000 (20:43 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 11 Oct 2018 18:43:43 +0000 (20:43 +0200)
This fixes up the issues reported and flips enforcement to on.

Change-Id: I2f9d96aff277864f7e1edc1be69d45610947a250
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-util/pom.xml
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/BindingGeneratorUtil.java
binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/mdsal/binding/model/util/Types.java

index d9539bc984556584cf1c3fa95387c50c9cff72f8..b6e1bd5cef5ce07b44a69a6ffda930e2ab938b42 100644 (file)
                     <propertyExpansion>checkstyle.violationSeverity=error</propertyExpansion>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>com.github.spotbugs</groupId>
+                <artifactId>spotbugs-maven-plugin</artifactId>
+                <configuration>
+                    <failOnError>true</failOnError>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 </project>
index 91b4223b81ea9af1c56a5cc16344351c930aa4f9..8b23035eb97bbb03b9f0c13f179d4cbd16ed3201 100644 (file)
@@ -162,88 +162,14 @@ public final class BindingGeneratorUtil {
         return BindingMapping.normalizePackageName(builder.toString());
     }
 
-    /**
-     * Converts string <code>token</code> to the cammel case format.
-     *
-     * @param token string which should be converted to the cammel case format
-     * @param uppercase boolean value which says whether the first character of the <code>token</code> should be
-     *                  upper-cased or not
-     * @return string in the camel case format
-     * @throws IllegalArgumentException
-     *             <ul>
-     *             <li>if <code>token</code> without white spaces is empty</li>
-     *             <li>if <code>token</code> equals null</li>
-     *             </ul>
-     */
-    private static String parseToCamelCase(final String token, final boolean uppercase) {
-        if (token == null) {
-            throw new IllegalArgumentException("Name can not be null");
-        }
-
-        String correctStr = DOT_MATCHER.removeFrom(token.trim());
-        if (correctStr.isEmpty()) {
-            throw new IllegalArgumentException("Name can not be empty");
-        }
-
-        correctStr = replaceWithCamelCase(correctStr, ' ');
-        correctStr = replaceWithCamelCase(correctStr, '-');
-        correctStr = replaceWithCamelCase(correctStr, '_');
-
-        char firstChar = correctStr.charAt(0);
-        firstChar = uppercase ? Character.toUpperCase(firstChar) : Character.toLowerCase(firstChar);
-
-        if (firstChar >= '0' && firstChar <= '9') {
-            return '_' + correctStr;
-        } else {
-            return firstChar + correctStr.substring(1);
-        }
-    }
-
-    /**
-     * Replaces all the occurrences of the <code>removalChar</code> in the
-     * <code>text</code> with empty string and converts following character to
-     * upper case.
-     *
-     * @param text
-     *            string with source text which should be converted
-     * @param removalChar
-     *            character which is sought in the <code>text</code>
-     * @return string which doesn't contain <code>removalChar</code> and has
-     *         following characters converted to upper case
-     * @throws IllegalArgumentException
-     *             if the length of the returning string has length 0
-     */
-    private static String replaceWithCamelCase(final String text, final char removalChar) {
-        int toBeRemovedPos = text.indexOf(removalChar);
-        if (toBeRemovedPos == -1) {
-            return text;
-        }
-
-        final StringBuilder sb = new StringBuilder(text);
-        final String toBeRemoved = String.valueOf(removalChar);
-        do {
-            sb.replace(toBeRemovedPos, toBeRemovedPos + 1, "");
-            // check if 'toBeRemoved' character is not the only character in
-            // 'text'
-            if (sb.length() == 0) {
-                throw new IllegalArgumentException("The resulting string can not be empty");
-            }
-            final char replacement = Character.toUpperCase(sb.charAt(toBeRemovedPos));
-            sb.setCharAt(toBeRemovedPos, replacement);
-            toBeRemovedPos = sb.indexOf(toBeRemoved);
-        } while (toBeRemovedPos != -1);
-
-        return sb.toString();
-    }
-
     private static <T> Iterable<T> sortedCollection(final Comparator<? super T> comparator, final Collection<T> input) {
-        if (input.size() > 1) {
-            final List<T> ret = new ArrayList<>(input);
-            ret.sort(comparator);
-            return ret;
-        } else {
+        if (input.size() <= 1) {
             return input;
         }
+
+        final List<T> ret = new ArrayList<>(input);
+        ret.sort(comparator);
+        return ret;
     }
 
     private static final ThreadLocal<MessageDigest> SHA1_MD = ThreadLocal.withInitial(() -> {
index 07311981d2d7dba92a2bc82906e9d963f978aa16..c52c95001d84e65212c8f851a4426c54763c0553 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.mdsal.binding.model.util;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.Preconditions;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -297,7 +296,7 @@ public final class Types {
          */
         BaseTypeWithRestrictionsImpl(final JavaTypeName identifier, final Restrictions restrictions) {
             super(identifier);
-            this.restrictions = Preconditions.checkNotNull(restrictions);
+            this.restrictions = requireNonNull(restrictions);
         }
 
         @Override
@@ -368,41 +367,10 @@ public final class Types {
     }
 
     private static final class DefaultRestrictions<T extends Number & Comparable<T>> implements Restrictions {
-        private final T min;
-        private final T max;
         private final RangeConstraint<?> rangeConstraint;
 
-        private DefaultRestrictions(final T min, final T max) {
-            this.min = Preconditions.checkNotNull(min);
-            this.max = Preconditions.checkNotNull(max);
-
-            this.rangeConstraint = new RangeConstraint<T>() {
-
-                @Override
-                public Optional<String> getErrorAppTag() {
-                    return Optional.empty();
-                }
-
-                @Override
-                public Optional<String> getErrorMessage() {
-                    return Optional.empty();
-                }
-
-                @Override
-                public Optional<String> getDescription() {
-                    return Optional.empty();
-                }
-
-                @Override
-                public Optional<String> getReference() {
-                    return Optional.empty();
-                }
-
-                @Override
-                public RangeSet<T> getAllowedRanges() {
-                    return ImmutableRangeSet.of(Range.closed(min, max));
-                }
-            };
+        DefaultRestrictions(final T min, final T max) {
+            this.rangeConstraint = new DefaultRangeConstraint<>(min, max);
         }
 
         @Override
@@ -425,4 +393,39 @@ public final class Types {
             return Optional.empty();
         }
     }
+
+    private static final class DefaultRangeConstraint<T extends Number & Comparable<T>> implements RangeConstraint<T> {
+        private final T min;
+        private final T max;
+
+        DefaultRangeConstraint(final T min, final T max) {
+            this.min = requireNonNull(min);
+            this.max = requireNonNull(max);
+        }
+
+        @Override
+        public Optional<String> getErrorAppTag() {
+            return Optional.empty();
+        }
+
+        @Override
+        public Optional<String> getErrorMessage() {
+            return Optional.empty();
+        }
+
+        @Override
+        public Optional<String> getDescription() {
+            return Optional.empty();
+        }
+
+        @Override
+        public Optional<String> getReference() {
+            return Optional.empty();
+        }
+
+        @Override
+        public RangeSet<T> getAllowedRanges() {
+            return ImmutableRangeSet.of(Range.closed(min, max));
+        }
+    }
 }