Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some minor issues discovered by PMD plugins #14969

Open
wants to merge 5 commits into
base: 3.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.apache.dubbo.common.utils.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
Expand Down Expand Up @@ -844,7 +844,7 @@ public static byte[] getMD5(byte[] source) {
* @return MD5 byte array.
*/
public static byte[] getMD5(File file) throws IOException {
InputStream is = new FileInputStream(file);
InputStream is = Files.newInputStream(file.toPath());
try {
return getMD5(is);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static <T> T getAttribute(Annotation annotation, String... attributeNames) throw
}

// exclude string attribute default is empty
if ((attribute instanceof String) && ((String) attribute).length() == 0) {
if ((attribute instanceof String) && ((String) attribute).isEmpty()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public static Object compatibleTypeConvert(Object value, Class<?> type) {
return new BigDecimal(string);
}
if (type == Short.class || type == short.class) {
return new Short(string);
return Short.valueOf(string);
}
if (type == Integer.class || type == int.class) {
return new Integer(string);
return Integer.valueOf(string);
}
if (type == Long.class || type == long.class) {
return new Long(string);
return Long.valueOf(string);
}
if (type == Double.class || type == double.class) {
return new Double(string);
Expand All @@ -95,7 +95,7 @@ public static Object compatibleTypeConvert(Object value, Class<?> type) {
return new Float(string);
}
if (type == Byte.class || type == byte.class) {
return new Byte(string);
return Byte.valueOf(string);
}
if (type == Boolean.class || type == boolean.class) {
return Boolean.valueOf(string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -36,6 +35,7 @@
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -168,7 +168,7 @@ public static String[] readLines(File file) throws IOException {
return new String[0];
}

return readLines(new FileInputStream(file));
return readLines(Files.newInputStream(file.toPath()));
}

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ public static void writeLines(File file, String[] lines) throws IOException {
if (file == null) {
throw new IOException("File is null.");
}
writeLines(new FileOutputStream(file), lines);
writeLines(Files.newOutputStream(file.toPath()), lines);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ protected void resolveFile() {
resolveFile = userResolveFile.getAbsolutePath();
}
}
if (resolveFile != null && resolveFile.length() > 0) {
if (resolveFile != null && !resolveFile.isEmpty()) {
Properties properties = new RegexProperties();
try (FileInputStream fis = new FileInputStream(resolveFile)) {
properties.load(fis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -50,10 +51,10 @@ public class ResourceFilter implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {
String config = filterConfig.getInitParameter("resources");
if (config != null && config.length() > 0) {
if (config != null && !config.isEmpty()) {
String[] configs = Constants.COMMA_SPLIT_PATTERN.split(config);
for (String c : configs) {
if (c != null && c.length() > 0) {
if (c != null && !c.isEmpty()) {
c = c.replace('\\', '/');
if (c.endsWith("/")) {
c = c.substring(0, c.length() - 1);
Expand Down Expand Up @@ -118,7 +119,7 @@ private boolean isFile(String path) {

private long getLastModified(String uri) {
for (String resource : resources) {
if (resource != null && resource.length() > 0) {
if (resource != null && !resource.isEmpty()) {
String path = resource + uri;
if (isFile(path)) {
File file = new File(path);
Expand All @@ -136,7 +137,7 @@ private InputStream getInputStream(String uri) {
String path = resource + uri;
try {
if (isFile(path)) {
return new FileInputStream(path);
return Files.newInputStream(Paths.get(path));
} else if (path.startsWith(CLASSPATH_PREFIX)) {
return Thread.currentThread()
.getContextClassLoader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import org.apache.dubbo.rpc.model.ApplicationModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
Expand Down Expand Up @@ -233,7 +233,7 @@ private void doSaveProperties(long version) {

void loadProperties() {
if (file != null && file.exists()) {
try (InputStream in = new FileInputStream(file)) {
try (InputStream in = Files.newInputStream(file.toPath())) {
properties.load(in);
if (logger.isInfoEnabled()) {
logger.info("Load service store file " + file + ", data: " + properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static Type[] getReturnTypes(Invocation invocation) {

public static Long getInvocationId(Invocation inv) {
String id = inv.getAttachment(ID_KEY);
return id == null ? null : new Long(id);
return id == null ? null : Long.valueOf(id);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.IOUtils;

/**
* Unpack the downloaded zookeeper binary archive.
Expand All @@ -52,7 +52,7 @@ private void unpack(ZookeeperContext context, int clientPort) throws DubboTestEx
File sourceFile = context.getSourceFile().toFile();
Path targetPath = Paths.get(context.getSourceFile().getParent().toString(), String.valueOf(clientPort));
// check if it's unpacked.
if (targetPath.toFile() != null && targetPath.toFile().isDirectory()) {
if (targetPath.toFile().isDirectory()) {
logger.info(String.format("The file has been unpacked, target path:%s", targetPath.toString()));
return;
}
Expand Down Expand Up @@ -87,28 +87,31 @@ protected void doInitialize(ZookeeperContext context) throws DubboTestException
// get the file name, just like apache-zookeeper-{version}-bin
// the version we maybe unknown if the zookeeper archive binary file is copied by user self.
Path parentPath = Paths.get(context.getSourceFile().getParent().toString(), String.valueOf(clientPort));
if (!Files.exists(parentPath)
|| !parentPath.toFile().isDirectory()
|| parentPath.toFile().listFiles().length != 1) {
if (!Files.exists(parentPath) || !parentPath.toFile().isDirectory()) {
throw new IllegalStateException("There is something wrong in unpacked file!");
}

File[] files = parentPath.toFile().listFiles();
if (files == null || files.length != 1) {
throw new IllegalStateException("There is something wrong in unpacked file!");
}
// rename directory
File sourceFile = parentPath.toFile().listFiles()[0];
File targetFile = Paths.get(parentPath.toString(), context.getUnpackedDirectory())
.toFile();
File sourceFile = files[0];
File targetFile = Paths.get(parentPath.toString(), context.getUnpackedDirectory()).toFile();
sourceFile.renameTo(targetFile);
if (!Files.exists(targetFile.toPath()) || !targetFile.isDirectory()) {
throw new IllegalStateException(String.format(
"Failed to rename the directory. source directory: %s, target directory: %s",
sourceFile.toPath().toString(), targetFile.toPath().toString()));
throw new IllegalStateException(String.format("Failed to rename the directory. source directory: %s, target directory: %s", sourceFile.toPath(), targetFile.toPath()));
}
// get the bin path
Path zookeeperBin = Paths.get(targetFile.toString(), "bin");
// update file permission
for (File file : zookeeperBin.toFile().listFiles()) {
file.setExecutable(true, false);
file.setReadable(true, false);
file.setWritable(false, false);
File[] zookeeperBinFiles = zookeeperBin.toFile().listFiles();
if (zookeeperBinFiles != null) {
for (File file : zookeeperBinFiles) {
file.setExecutable(true, false);
file.setReadable(true, false);
file.setWritable(false, false);
}
}
}
}
Expand Down
Loading