Improved: Fix or Silence various warnings

(OFBIZ-10701)

In order to detect potential issues early, the linting compiler option should be 
used by default and disabled with {{./gradlew -PXlint:none build}}.

Additionally it is important to reduce the number of warnings otherwise new 
warnings will remain unnoticed.

The warning related to the com.googlecode.concurrentlinkedhashmap dependency 
should resolves itself once we handle upgrade to Caffeine like proposed in 
OFBIZ-6747.

Thanks: Mathieu Lirzin

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1848673 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2018-12-11 13:33:49 +00:00
parent 13f8b82f51
commit bbfc62d47a
129 changed files with 346 additions and 572 deletions
@@ -32,7 +32,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import org.apache.ofbiz.base.component.ComponentConfig;
import org.apache.ofbiz.base.component.ComponentException;
import org.apache.ofbiz.base.component.ComponentLoaderConfig;
@@ -26,6 +26,7 @@ import org.apache.ofbiz.base.conversion.BooleanConverters;
import org.apache.ofbiz.base.conversion.Converter;
import org.apache.ofbiz.base.conversion.ConverterLoader;
import org.apache.ofbiz.base.conversion.Converters;
import org.apache.ofbiz.base.util.UtilGenerics;
import junit.framework.TestCase;
@@ -49,12 +50,13 @@ public class TestBooleanConverters extends TestCase {
assertEquals(label + " converted", Boolean.FALSE, converter.convert(falseSource));
}
@SuppressWarnings("unchecked")
public static <S> void assertToCollection(String label, S source) throws Exception {
Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), List.class);
Converter<S, ? extends Collection<S>> toList =
UtilGenerics.cast(Converters.getConverter(source.getClass(), List.class));
Collection<S> listResult = toList.convert(source);
assertEquals(label + " converted to List", source, listResult.toArray()[0]);
Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>) Converters.getConverter(source.getClass(), Set.class);
Converter<S, ? extends Collection<S>> toSet =
UtilGenerics.cast(Converters.getConverter(source.getClass(), Set.class));
Collection<S> setResult = toSet.convert(source);
assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
}
@@ -34,26 +34,9 @@ public class ComparableRangeTests extends GenericTestCaseBase {
super(name);
}
@SuppressWarnings("unchecked")
private static <L extends Comparable<L>, R extends Comparable<R>> void comparableRangeConstructorTest(L left, R right) {
new ComparableRange<>(left, left);
new ComparableRange<>(right, right);
IllegalArgumentException caught = null;
try {
new ComparableRange(left, right);
} catch (IllegalArgumentException e) {
caught = e;
} finally {
assertNotNull("expected exception", caught);
}
caught = null;
try {
new ComparableRange(right, left);
} catch (IllegalArgumentException e) {
caught = e;
} finally {
assertNotNull("expected exception", caught);
}
}
private static <T extends Comparable<T>, B extends Comparable<B>> void comparableRangeTest(String label, B bad, T a, T b, T c, T d) {
@@ -341,7 +341,6 @@ OUTER:
assertEquals(null, wanted, got);
}
@SuppressWarnings("unchecked")
public static void assertEquals(String msg, Object wanted, Object got) {
if (wanted instanceof List) {
assertEquals(msg, (List<?>) wanted, got);
@@ -359,7 +358,7 @@ OUTER:
} else if (got.getClass().isArray()) {
assertEqualsArrayArray(msg, wanted, got);
} else if (got instanceof List) {
assertEqualsArrayList(msg, wanted, (List) got);
assertEqualsArrayList(msg, wanted, (List<?>) got);
} else {
TestCase.assertEquals(msg, wanted, got);
}
@@ -374,6 +373,7 @@ OUTER:
return list;
}
@SafeVarargs
public static <T> List<T> list(T... list) {
return new ArrayList<>(Arrays.asList(list));
}
@@ -384,6 +384,7 @@ OUTER:
return set;
}
@SafeVarargs
public static <T> Set<T> set(T... list) {
return new HashSet<>(Arrays.asList(list));
}
@@ -231,7 +231,6 @@ public class HttpRequestFileUpload {
}
}
fos.flush();
fos.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
@@ -49,7 +49,7 @@ public class ObjectInputStream extends java.io.ObjectInputStream {
*/
@Override
protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
Class<?>[] cinterfaces = new Class[interfaces.length];
Class<?>[] cinterfaces = new Class<?>[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
cinterfaces[i] = classloader.loadClass(interfaces[i]);
}
@@ -19,6 +19,7 @@
package org.apache.ofbiz.base.util;
import java.io.Serializable;
import java.util.Objects;
import org.apache.ofbiz.base.lang.SourceMonitored;
import org.apache.ofbiz.base.lang.ThreadSafe;
@@ -296,6 +297,11 @@ public class TimeDuration implements Serializable, Comparable<TimeDuration> {
return cal;
}
@Override
public int hashCode() {
return Objects.hash(milliseconds, seconds, minutes, hours, days, months, years);
}
/** Returns a <code>TimeDuration</code> instance derived from an encoded
* <code>long</code> value. This method is intended to be used in tandem with the
* <code>toLong</code> method. <b>Note:</b> this
@@ -63,6 +63,7 @@ public class UtilCodec {
/**
* @deprecated Use {@link #sanitize(String,String)} instead
*/
@Deprecated
public String sanitize(String outString); // Only really useful with HTML, else it simply calls encode() method
public String sanitize(String outString, String contentTypeId); // Only really useful with HTML, else it simply calls encode() method
}
@@ -83,6 +84,7 @@ public class UtilCodec {
/**
* @deprecated Use {@link #sanitize(String,String)} instead
*/
@Deprecated
public String sanitize(String original) {
return sanitize(original, null);
}
@@ -144,6 +146,7 @@ public class UtilCodec {
/**
* @deprecated Use {@link #sanitize(String,String)} instead
*/
@Deprecated
public String sanitize(String original) {
return sanitize(original, null);
}
@@ -164,6 +167,7 @@ public class UtilCodec {
/**
* @deprecated Use {@link #sanitize(String,String)} instead
*/
@Deprecated
public String sanitize(String original) {
return sanitize(original, null);
}
@@ -192,6 +196,7 @@ public class UtilCodec {
/**
* @deprecated Use {@link #sanitize(String,String)} instead
*/
@Deprecated
public String sanitize(String original) {
return sanitize(original, null);
}
@@ -169,6 +169,7 @@ public final class UtilGenerics {
return map;
}
@SafeVarargs
@SuppressWarnings("hiding")
public static <K, Object> Map<K, Object> toMap(Class<K> keyType, Object... data) {
if (data == null) {
@@ -32,82 +32,24 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import org.apache.commons.io.IOUtils;
public final class UtilIO {
private static final Charset UTF8 = Charset.forName("UTF-8");
public static final String module = UtilIO.class.getName();
private UtilIO () {}
/** Copy an InputStream to an OutputStream, optionally closing either
* the input or the output.
*
* @param in the InputStream to copy from
* @param closeIn whether to close the input when the copy is done
* @param out the OutputStream to copy to
* @param closeOut whether to close the output when the copy is done
* @throws IOException if an error occurs
*/
public static void copy(InputStream in, boolean closeIn, OutputStream out, boolean closeOut) throws IOException {
try {
try {
IOUtils.copy(in, out);
} finally {
if (closeIn) {
IOUtils.closeQuietly(in);
}
}
} finally {
if (closeOut) {
IOUtils.closeQuietly(out);
}
}
}
/** Copy a Reader to a Writer, optionally closing either the input or
* the output.
/** Copy a Reader to an Appendable.
*
* @param reader the Reader to copy from
* @param closeIn whether to close the input when the copy is done
* @param writer the Writer to copy to
* @param closeOut whether to close the output when the copy is done
* @throws IOException if an error occurs
*/
public static void copy(Reader reader, boolean closeIn, Writer writer, boolean closeOut) throws IOException {
try {
try {
IOUtils.copy(reader, writer);
} finally {
if (closeIn) {
IOUtils.closeQuietly(reader);
}
}
} finally {
if (closeOut) {
IOUtils.closeQuietly(writer);
}
}
}
/** Copy a Reader to an Appendable, optionally closing the input.
*
* @param reader the Reader to copy from
* @param closeIn whether to close the input when the copy is done
* @param out the Appendable to copy to
* @throws IOException if an error occurs
*/
public static void copy(Reader reader, boolean closeIn, Appendable out) throws IOException {
try {
CharBuffer buffer = CharBuffer.allocate(4096);
while (reader.read(buffer) > 0) {
buffer.flip();
buffer.rewind();
out.append(buffer);
}
} finally {
if (closeIn) {
IOUtils.closeQuietly(reader);
}
public static void copy(Reader reader, Appendable out) throws IOException {
CharBuffer buffer = CharBuffer.allocate(4096);
while (reader.read(buffer) > 0) {
buffer.flip();
buffer.rewind();
out.append(buffer);
}
}
@@ -218,6 +218,7 @@ public final class UtilNumber {
* @return int - Rounding mode to pass to BigDecimal's methods. Defaults to BigDecimal.ROUND_HALF_UP
* @deprecated Use {@link #getRoundingMode(String,String)} instead
*/
@Deprecated
public static int getBigDecimalRoundingMode(String file, String property) {
return getRoundingMode(file, property).ordinal();
}
@@ -228,6 +229,7 @@ public final class UtilNumber {
* @return int - Rounding mode to pass to BigDecimal's methods. Defaults to BigDecimal.ROUND_HALF_UP
* @deprecated Use {@link #getRoundingMode(String)} instead
*/
@Deprecated
public static int getBigDecimalRoundingMode(String property) {
return getRoundingMode(arithmeticPropertiesFile, property).ordinal();
}
@@ -342,6 +344,7 @@ public final class UtilNumber {
* @deprecated Use {@link #toPercentString(Number number, int scale, RoundingMode roundingMode)} instead
*
*/
@Deprecated
public static String toPercentString(Number number, int scale, int roundingMode) {
// convert to BigDecimal
if (!(number instanceof BigDecimal)) {
@@ -566,8 +566,6 @@ public final class UtilProperties implements Serializable {
+"# The comments have been removed, you may still find them on the OFBiz repository... \n"
+"#");
}
propFile.close();
} catch (FileNotFoundException e) {
Debug.logInfo(e, "Unable to located the resource file.", module);
} catch (IOException e) {
@@ -43,8 +43,9 @@ import org.apache.ofbiz.base.util.string.UelUtil;
@SuppressWarnings("serial")
public final class FlexibleMapAccessor<T> implements Serializable, IsEmpty {
public static final String module = FlexibleMapAccessor.class.getName();
private static final UtilCache<String, FlexibleMapAccessor<?>> fmaCache = UtilCache.createUtilCache("flexibleMapAccessor.ExpressionCache");
private static final FlexibleMapAccessor nullFma = new FlexibleMapAccessor("");
private static final UtilCache<String, FlexibleMapAccessor<Object>> fmaCache =
UtilCache.createUtilCache("flexibleMapAccessor.ExpressionCache");
private static final FlexibleMapAccessor<?> nullFma = new FlexibleMapAccessor<>("");
private final boolean isEmpty;
private final String original;
@@ -84,15 +85,14 @@ public final class FlexibleMapAccessor<T> implements Serializable, IsEmpty {
* @param original The original String expression
* @return A FlexibleMapAccessor instance
*/
@SuppressWarnings("unchecked")
public static <T> FlexibleMapAccessor<T> getInstance(String original) {
if (UtilValidate.isEmpty(original) || "null".equals(original)) {
return nullFma;
return UtilGenerics.cast(nullFma);
}
FlexibleMapAccessor fma = fmaCache.get(original);
FlexibleMapAccessor<T> fma = UtilGenerics.cast(fmaCache.get(original));
if (fma == null) {
fmaCache.putIfAbsent(original, new FlexibleMapAccessor(original));
fma = fmaCache.get(original);
fmaCache.putIfAbsent(original, new FlexibleMapAccessor<>(original));
fma = UtilGenerics.cast(fmaCache.get(original));
}
return fma;
}
@@ -221,10 +221,10 @@ public final class FlexibleMapAccessor<T> implements Serializable, IsEmpty {
if (this == obj) {
return true;
}
try {
FlexibleMapAccessor that = (FlexibleMapAccessor) obj;
if (obj instanceof FlexibleMapAccessor) {
FlexibleMapAccessor<?> that = (FlexibleMapAccessor<?>) obj;
return UtilObject.equalsHelper(this.original, that.original);
} catch (Exception e) {}
}
return false;
}
@@ -23,6 +23,7 @@ import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
@@ -65,6 +66,11 @@ public abstract class GenericMap<K, V> implements Appender<StringBuilder>, Map<K
return values().contains(value);
}
@Override
public int hashCode() {
return Objects.hash(entrySet, keySet, values);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Map<?, ?>)) {
@@ -451,21 +451,6 @@ public class UelFunctions {
return label;
}
public static boolean urlExists(String str) {
boolean result = false;
try {
URL url = FlexibleLocation.resolveLocation(str);
if (url != null) {
try (InputStream is = url.openStream();) {
result = true;
}
}
} catch (IOException e) {
Debug.log(e, module);
}
return result;
}
public static Document readHtmlDocument(String str) {
Document document = null;
try {
@@ -83,7 +83,7 @@ public final class UelUtil {
* @param expectedType The expected object Class to return
* @return Result object
*/
public static Object evaluate(Map<String, ? extends Object> context, String expression, Class expectedType) {
public static Object evaluate(Map<String, ? extends Object> context, String expression, Class<?> expectedType) {
ELContext elContext = new ReadOnlyContext(context);
ValueExpression ve = exprFactory.createValueExpression(elContext, expression, expectedType);
return ve.getValue(elContext);
@@ -95,7 +95,7 @@ public final class UelUtil {
* @param expression UEL expression
* @param expectedType The expected object Class to set
*/
public static void setValue(Map<String, Object> context, String expression, Class expectedType, Object value) {
public static void setValue(Map<String, Object> context, String expression, Class<?> expectedType, Object value) {
if (Debug.verboseOn()) {
Debug.logVerbose("UelUtil.setValue invoked, expression = " + expression + ", value = " + value, module);
}
@@ -358,8 +358,8 @@ public final class UelUtil {
super(isReadOnly);
this.isReadOnly = isReadOnly;
}
@Override
@SuppressWarnings("unchecked")
public void setValue(ELContext context, Object base, Object property, Object val) {
if (context == null) {
throw new NullPointerException();
@@ -374,7 +374,7 @@ public final class UelUtil {
Debug.logVerbose("ExtendedListResolver.setValue adding List element: base = " + base + ", property = " + property + ", value = " + val, module);
}
context.setPropertyResolved(true);
List list = (List) base;
List<Object> list = UtilGenerics.cast(base);
list.add(val);
} else if (str.startsWith("insert@")) {
if (Debug.verboseOn()) {
@@ -383,7 +383,7 @@ public final class UelUtil {
context.setPropertyResolved(true);
String indexStr = str.replace("insert@", "");
int index = Integer.parseInt(indexStr);
List list = (List) base;
List<Object> list = UtilGenerics.cast(base);
try {
list.add(index, val);
} catch (UnsupportedOperationException ex) {
@@ -413,7 +413,7 @@ public final class UelUtil {
}
if (base != null && base instanceof LocalizedMap) {
context.setPropertyResolved(true);
LocalizedMap map = (LocalizedMap) base;
LocalizedMap<Object> map = (LocalizedMap<Object>) base;
Locale locale = null;
try {
VariableMapper vm = context.getVariableMapper();
@@ -436,11 +436,11 @@ public final class UelUtil {
}
locale = Locale.getDefault();
}
return resolveVariable(property.toString(), (Map) map, locale);
return resolveVariable(property.toString(), (Map<String, Object>) map, locale);
}
if (base != null && base instanceof Map && property instanceof String) {
context.setPropertyResolved(true);
return resolveVariable(property.toString(), (Map) base, null);
return resolveVariable(property.toString(), (Map<String, Object>) base, null);
}
return super.getValue(context, base, property);
}
@@ -223,6 +223,7 @@ public class FlexibleStringExpanderTests extends TestCase {
Debug.set(Debug.VERBOSE, wasVerbose);
}
@SuppressWarnings("serial")
public static class ThrowException extends Exception{
public Object getValue() throws Exception {
throw new Exception();
@@ -160,8 +160,7 @@ public class AssertTests extends TestCase {
fail("isNotInstanceOf (argument list) threw an exception - " + e);
}
try {
Map[] mapArray = {strMap};
Assert.isAssignableTo("foo", strArray, mapArray.getClass());
Assert.isAssignableTo("foo", strArray, Map[].class);
fail("isNotInstanceOf (argument list) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {}
}
@@ -18,6 +18,7 @@
*******************************************************************************/
package org.apache.ofbiz.base.util.test;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -28,7 +29,6 @@ import org.apache.ofbiz.base.lang.SourceMonitored;
import org.apache.ofbiz.base.test.GenericTestCaseBase;
import org.apache.ofbiz.base.util.GeneralRuntimeException;
import org.apache.ofbiz.base.util.StringUtil;
import org.apache.ofbiz.base.util.UtilGenerics;
@SourceMonitored
public class StringUtilTests extends GenericTestCaseBase {
@@ -48,7 +48,6 @@ public class StringUtilTests extends GenericTestCaseBase {
public void testStringUtil() throws Exception {
assertStaticHelperClass(StringUtil.class);
assertTrue("correct INSTANCE", StringUtil.INSTANCE instanceof StringUtil);
}
public void testInternString() {
@@ -151,12 +150,12 @@ public class StringUtilTests extends GenericTestCaseBase {
}
public void testCreateMap() {
List<String>[] badKeys = UtilGenerics.cast(new List[] {null, list("1"), list("2")});
List<String>[] badValues = UtilGenerics.cast(new List[] {list("one"), null, list("two", "extra")});
for (int i = 0; i < badKeys.length; i++) {
List<List<String>> badKeys = Arrays.asList(null, list("1"), list("2"));
List<List<String>> badValues = Arrays.asList(list("one"), null, list("two", "extra"));
for (int i = 0; i < badKeys.size(); i++) {
IllegalArgumentException caught = null;
try {
StringUtil.createMap(badKeys[i], badValues[i]);
StringUtil.createMap(badKeys.get(i), badValues.get(i));
} catch (IllegalArgumentException e) {
caught = e;
} finally {
@@ -122,7 +122,6 @@ public class TimeDurationTests extends GenericTestCaseBase {
assertEquals(label + ".equals(cal, string)", calDuration, stringDuration);
assertEquals(label + ".equals(string, cal)", stringDuration, calDuration);
assertEquals(label + ".equals(string, string)", stringDuration, stringDuration);
assertFalse(label + ".not-equals(string, this)", stringDuration.equals(TimeDurationTests.class));
if (lastString != null) {
assertFalse(label + ".not-equals(string, lastString)", stringDuration.equals(lastString));
}