sonar issues

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893847 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-10-03 09:11:35 +00:00
parent 1da3708e8b
commit acadd84b53
13 changed files with 44 additions and 29 deletions

View File

@ -104,8 +104,10 @@ import org.apache.poi.util.TempFile;
if (encryptedTempData != null) { if (encryptedTempData != null) {
encryptedTempData.dispose(); encryptedTempData.dispose();
} }
if (tempFile != null) { if (tempFile != null && tempFile.exists()) {
tempFile.delete(); if (!tempFile.delete()) {
LOG.atDebug().log("temp file was already deleted (probably due to previous call to close this resource)");
}
} }
} }
} }

View File

@ -826,7 +826,7 @@ public final class HemfPlusDraw {
@SuppressWarnings("squid:S2111") @SuppressWarnings("squid:S2111")
static double round10(double d) { static double round10(double d) {
return new BigDecimal(d).setScale(10, RoundingMode.HALF_UP).doubleValue(); return BigDecimal.valueOf(d).setScale(10, RoundingMode.HALF_UP).doubleValue();
} }
static int readRectS(LittleEndianInputStream leis, Rectangle2D bounds) { static int readRectS(LittleEndianInputStream leis, Rectangle2D bounds) {

View File

@ -17,6 +17,7 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -179,15 +180,19 @@ public final class EmbeddedObjectRefSubRecord extends SubRecord {
} }
private static Ptg readRefPtg(byte[] formulaRawBytes) { private static Ptg readRefPtg(byte[] formulaRawBytes) {
LittleEndianInput in = new LittleEndianInputStream(new UnsynchronizedByteArrayInputStream(formulaRawBytes)); try (LittleEndianInputStream in = new LittleEndianInputStream(
byte ptgSid = in.readByte(); new UnsynchronizedByteArrayInputStream(formulaRawBytes))) {
switch(ptgSid) { byte ptgSid = in.readByte();
case AreaPtg.sid: return new AreaPtg(in); switch(ptgSid) {
case Area3DPtg.sid: return new Area3DPtg(in); case AreaPtg.sid: return new AreaPtg(in);
case RefPtg.sid: return new RefPtg(in); case Area3DPtg.sid: return new Area3DPtg(in);
case Ref3DPtg.sid: return new Ref3DPtg(in); case RefPtg.sid: return new RefPtg(in);
case Ref3DPtg.sid: return new Ref3DPtg(in);
}
return null;
} catch (IOException e) {
throw new RuntimeException("Unexpected exception in readRefPtg", e);
} }
return null;
} }
private static byte[] readRawData(LittleEndianInput in, int size) { private static byte[] readRawData(LittleEndianInput in, int size) {

View File

@ -16,6 +16,8 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.poifs.filesystem; package org.apache.poi.poifs.filesystem;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.poifs.nio.FileBackedDataSource; import org.apache.poi.poifs.nio.FileBackedDataSource;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
@ -30,6 +32,7 @@ import java.io.IOException;
*/ */
@Beta @Beta
public class TempFilePOIFSFileSystem extends POIFSFileSystem { public class TempFilePOIFSFileSystem extends POIFSFileSystem {
private static Logger LOG = LogManager.getLogger(TempFilePOIFSFileSystem.class);
File tempFile; File tempFile;
@Override @Override
@ -44,7 +47,11 @@ public class TempFilePOIFSFileSystem extends POIFSFileSystem {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (tempFile != null && tempFile.exists()) tempFile.delete(); if (tempFile != null && tempFile.exists()) {
if (!tempFile.delete()) {
LOG.atDebug().log("temp file was already deleted (probably due to previous call to close this resource)");
}
}
super.close(); super.close();
} }

View File

@ -42,7 +42,7 @@ public class CellDateFormatter extends CellFormatter {
private static final Calendar EXCEL_EPOCH_CAL = private static final Calendar EXCEL_EPOCH_CAL =
LocaleUtil.getLocaleCalendar(1904, 0, 1); LocaleUtil.getLocaleCalendar(1904, 0, 1);
private static final double NUM_MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24; private static final int NUM_MILLISECONDS_IN_DAY = 1000 * 60 * 60 * 24;
private static /* final */ CellDateFormatter SIMPLE_DATE; private static /* final */ CellDateFormatter SIMPLE_DATE;

View File

@ -134,7 +134,7 @@ final class PercentRankExcFunction implements FreeRefFunction {
for (Double d : numbers) { for (Double d : numbers) {
if (d < x) lessThanCount++; if (d < x) lessThanCount++;
} }
BigDecimal result = new BigDecimal((double)(lessThanCount + 1) / (double)(numbers.size() + 1)); BigDecimal result = BigDecimal.valueOf((double)(lessThanCount + 1) / (double)(numbers.size() + 1));
return new NumberEval(PercentRank.round(result, significance)); return new NumberEval(PercentRank.round(result, significance));
} else { } else {
int intermediateSignificance = significance < 5 ? 8 : significance + 3; int intermediateSignificance = significance < 5 ? 8 : significance + 3;

View File

@ -75,17 +75,14 @@ public final class Fixed implements Function1Arg, Function2Arg, Function3Arg {
OperandResolver.getSingleValue( OperandResolver.getSingleValue(
numberParam, srcRowIndex, srcColumnIndex); numberParam, srcRowIndex, srcColumnIndex);
BigDecimal number = BigDecimal number =
new BigDecimal(OperandResolver.coerceValueToDouble(numberValueEval)); BigDecimal.valueOf(OperandResolver.coerceValueToDouble(numberValueEval));
ValueEval placesValueEval = ValueEval placesValueEval =
OperandResolver.getSingleValue( OperandResolver.getSingleValue(placesParam, srcRowIndex, srcColumnIndex);
placesParam, srcRowIndex, srcColumnIndex);
int places = OperandResolver.coerceValueToInt(placesValueEval); int places = OperandResolver.coerceValueToInt(placesValueEval);
ValueEval skipThousandsSeparatorValueEval = ValueEval skipThousandsSeparatorValueEval =
OperandResolver.getSingleValue( OperandResolver.getSingleValue(skipThousandsSeparatorParam, srcRowIndex, srcColumnIndex);
skipThousandsSeparatorParam, srcRowIndex, srcColumnIndex);
Boolean skipThousandsSeparator = Boolean skipThousandsSeparator =
OperandResolver.coerceValueToBoolean( OperandResolver.coerceValueToBoolean(skipThousandsSeparatorValueEval, false);
skipThousandsSeparatorValueEval, false);
// Round number to respective places. // Round number to respective places.
number = number.setScale(places, RoundingMode.HALF_UP); number = number.setScale(places, RoundingMode.HALF_UP);

View File

@ -123,8 +123,12 @@ public final class PercentRank implements Function {
if (greaterThanCount == numbers.size() || lessThanCount == numbers.size()) { if (greaterThanCount == numbers.size() || lessThanCount == numbers.size()) {
return ErrorEval.NA; return ErrorEval.NA;
} }
BigDecimal result = new BigDecimal((double)lessThanCount / (double)(lessThanCount + greaterThanCount)); if (lessThanCount + greaterThanCount == 0) {
return new NumberEval(round(result, significance)); return new NumberEval(0);
} else {
BigDecimal result = BigDecimal.valueOf((double)lessThanCount / (double)(lessThanCount + greaterThanCount));
return new NumberEval(round(result, significance));
}
} else { } else {
int intermediateSignificance = significance < 5 ? 8 : significance + 3; int intermediateSignificance = significance < 5 ? 8 : significance + 3;
ValueEval belowRank = calculateRank(numbers, closestMatchBelow, intermediateSignificance, false); ValueEval belowRank = calculateRank(numbers, closestMatchBelow, intermediateSignificance, false);
@ -145,7 +149,7 @@ public final class PercentRank implements Function {
double diff = closestMatchAbove - closestMatchBelow; double diff = closestMatchAbove - closestMatchBelow;
double pos = x - closestMatchBelow; double pos = x - closestMatchBelow;
BigDecimal rankDiff = new BigDecimal(NumberToTextConverter.toText(aboveRank.getNumberValue() - belowRank.getNumberValue())); BigDecimal rankDiff = new BigDecimal(NumberToTextConverter.toText(aboveRank.getNumberValue() - belowRank.getNumberValue()));
BigDecimal result = new BigDecimal(belowRank.getNumberValue()).add(rankDiff.multiply(new BigDecimal(pos / diff))); BigDecimal result = BigDecimal.valueOf(belowRank.getNumberValue()).add(rankDiff.multiply(BigDecimal.valueOf(pos / diff)));
return new NumberEval(round(result, significance)); return new NumberEval(round(result, significance));
} }

View File

@ -685,7 +685,7 @@ public class DataFormatter {
private static final Pattern endsWithCommas = Pattern.compile("(,+)$"); private static final Pattern endsWithCommas = Pattern.compile("(,+)$");
private final BigDecimal divider; private final BigDecimal divider;
private static final BigDecimal ONE_THOUSAND = new BigDecimal(1000); private static final BigDecimal ONE_THOUSAND = BigDecimal.valueOf(1000);
private final DecimalFormat df; private final DecimalFormat df;
private static String trimTrailingCommas(String s) { private static String trimTrailingCommas(String s) {
return s.replaceAll(",+$", ""); return s.replaceAll(",+$", "");

View File

@ -411,7 +411,7 @@ public class DateUtil {
return null; return null;
} }
BigDecimal bd = new BigDecimal(date); BigDecimal bd = BigDecimal.valueOf(date);
int wholeDays = bd.intValue(); int wholeDays = bd.intValue();

View File

@ -79,7 +79,7 @@ public class ExcelGeneralNumberFormat extends Format {
// numeric characters, with the decimal point counting as a numeric // numeric characters, with the decimal point counting as a numeric
// character". We know there is a decimal point, so limit to 10 digits. // character". We know there is a decimal point, so limit to 10 digits.
// https://support.microsoft.com/en-us/kb/65903 // https://support.microsoft.com/en-us/kb/65903
final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue(); final double rounded = BigDecimal.valueOf(value).round(TO_10_SF).doubleValue();
return decimalFormat.format(rounded, toAppendTo, pos); return decimalFormat.format(rounded, toAppendTo, pos);
} }

View File

@ -107,7 +107,7 @@ public class FractionFormat extends Format {
@SuppressWarnings("squid:S2111") @SuppressWarnings("squid:S2111")
public String format(Number num) { public String format(Number num) {
final BigDecimal doubleValue = new BigDecimal(num.doubleValue()); final BigDecimal doubleValue = BigDecimal.valueOf(num.doubleValue());
final boolean isNeg = doubleValue.compareTo(BigDecimal.ZERO) < 0; final boolean isNeg = doubleValue.compareTo(BigDecimal.ZERO) < 0;

View File

@ -225,7 +225,7 @@ final class NormalisedDecimal {
return _fractionalPart - other._fractionalPart; return _fractionalPart - other._fractionalPart;
} }
public BigDecimal getFractionalPart() { public BigDecimal getFractionalPart() {
return new BigDecimal(_fractionalPart).divide(BD_2_POW_24); return BigDecimal.valueOf(_fractionalPart).divide(BD_2_POW_24);
} }
private String getFractionalDigits() { private String getFractionalDigits() {