sonar优化

This commit is contained in:
redkale
2023-11-13 10:57:08 +08:00
parent 73411593b2
commit c9386a445b
15 changed files with 61 additions and 79 deletions

View File

@@ -156,7 +156,7 @@ public class LoggingFileHandler extends LoggingBaseHandler {
Files.move(logfile.toPath(), new File(logfile.getPath() + ".1").toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
} else {
if (logfile.exists() && logfile.length() < 1) {
logfile.delete();
Files.delete(logfile.toPath());
}
}
logstream = null;
@@ -174,7 +174,7 @@ public class LoggingFileHandler extends LoggingBaseHandler {
Files.move(logunusualfile.toPath(), new File(logunusualfile.getPath() + ".1").toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
} else {
if (logunusualfile.exists() && logunusualfile.length() < 1) {
logunusualfile.delete();
Files.delete(logunusualfile.toPath());
}
}
logunusualstream = null;

View File

@@ -972,12 +972,14 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
} catch (Exception ex) {
//do nothing
}
try {
register(type.getMethod("set" + bigColumn, field.getType()), entry);
} catch (Exception ex) {
//do nothing
if (field != null) {
try {
register(type.getMethod("set" + bigColumn, field.getType()), entry);
} catch (Exception ex) {
//do nothing
}
}
return field == null ? true : register(field, entry);
return field == null || register(field, entry);
}
public final <E> boolean register(final AccessibleObject field, final ConvertColumnEntry entry) {

View File

@@ -75,7 +75,7 @@ public class OptionalCoder<R extends Reader, W extends Writer, T> extends Simple
@Override
public void convertTo(W out, Optional<T> value) {
if (value == null || !value.isPresent()) {
if (!value.isPresent()) {
out.writeObjectNull(null);
return;
}

View File

@@ -109,12 +109,11 @@ public class BsonWriter extends Writer implements ByteTuple {
*/
protected int expand(int len) {
int newcount = count + len;
if (newcount <= content.length) {
return 0;
if (newcount > content.length) {
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
System.arraycopy(content, 0, newdata, 0, count);
this.content = newdata;
}
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
System.arraycopy(content, 0, newdata, 0, count);
this.content = newdata;
return 0;
}

View File

@@ -325,7 +325,7 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
if (element instanceof Field) {
return ((Field) element).getType();
}
return element == null ? null : ((Method) element).getReturnType();
return ((Method) element).getReturnType();
}
protected static String readGetSetFieldName(AccessibleObject element) {
@@ -333,9 +333,6 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
return ((Field) element).getName();
}
Method method = (Method) element;
if (method == null) {
return null;
}
String fname = method.getName();
if (!(fname.startsWith("is") && fname.length() > 2)
&& !(fname.startsWith("get") && fname.length() > 3)
@@ -376,8 +373,8 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
ConvertColumnEntry ref1 = factory.findRef(clazz, element);
final String fieldname = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(element) : ref1.name();
memberb.append(fieldname).append(',');
final Class fieldtype = readGetSetFieldType(element);
if (fieldtype != String.class && !fieldtype.isPrimitive()) {
final Class fieldType = readGetSetFieldType(element);
if (fieldType != String.class && !fieldType.isPrimitive()) {
if (mixedNames0 == null) {
mixedNames0 = new HashMap<>();
}
@@ -430,17 +427,17 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
fv.visitEnd();
fv = cw.visitField(ACC_PROTECTED + ACC_FINAL, fieldname + "FirstFieldBytes", "[B", null, null);
fv.visitEnd();
final Class fieldtype = readGetSetFieldType(element);
if (fieldtype != String.class && !fieldtype.isPrimitive()) {
final Class fieldType = readGetSetFieldType(element);
if (fieldType != String.class && !fieldType.isPrimitive()) {
fv = cw.visitField(ACC_PROTECTED, fieldname + "Encoder", encodeableDesc, null, null);
fv.visitEnd();
}
if (fieldtype == int.class) {
if (fieldType == int.class) {
intFieldCount++;
}
if (fieldtype == String.class && membersSize == 1 && readConvertSmallString(element) != null) {
if (fieldType == String.class && membersSize == 1 && readConvertSmallString(element) != null) {
onlyOneLatin1FieldObjectFlag = true;
} else if (fieldtype != short.class && fieldtype != int.class && fieldtype != long.class && !(fieldtype == String.class && readConvertSmallString(element) != null)) {
} else if (fieldType != short.class && fieldType != int.class && fieldType != long.class && !(fieldType == String.class && readConvertSmallString(element) != null)) {
onlyShotIntLongLatin1MoreFieldObjectFlag = false;
}
}
@@ -542,19 +539,19 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
elementIndex++;
AccessibleObject element1 = members.get(elementIndex);
ConvertColumnEntry ref1 = factory.findRef(clazz, element1);
final String fieldname1 = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(element1) : ref1.name();
final String fieldName1 = ref1 == null || ref1.name().isEmpty() ? readGetSetFieldName(element1) : ref1.name();
final Class fieldtype1 = readGetSetFieldType(element1);
elementIndex++;
AccessibleObject element2 = members.get(elementIndex);
ConvertColumnEntry ref2 = factory.findRef(clazz, element2);
final String fieldname2 = ref2 == null || ref2.name().isEmpty() ? readGetSetFieldName(element2) : ref2.name();
final String fieldName2 = ref2 == null || ref2.name().isEmpty() ? readGetSetFieldName(element2) : ref2.name();
final Class fieldtype2 = readGetSetFieldType(element2);
mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldname1 + "FirstFieldBytes", "[B");
mv.visitFieldInsn(GETFIELD, newDynName, fieldName1 + "FirstFieldBytes", "[B");
mv.visitVarInsn(ALOAD, 2); //String message = value.getMessage(); 加载 value
if (element1 instanceof Field) {
@@ -565,7 +562,7 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
maxLocals++;
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, fieldname2 + "CommaFieldBytes", "[B");
mv.visitFieldInsn(GETFIELD, newDynName, fieldName2 + "CommaFieldBytes", "[B");
mv.visitVarInsn(ALOAD, 2); //String message = value.getMessage(); 加载 value
if (element2 instanceof Field) {

View File

@@ -148,12 +148,11 @@ public class ProtobufWriter extends Writer implements ByteTuple {
protected int expand(int len) {
int newcount = count + len;
if (newcount <= content.length) {
return 0;
if (newcount > content.length) {
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
System.arraycopy(content, 0, newdata, 0, count);
this.content = newdata;
}
byte[] newdata = new byte[Math.max(content.length * 3 / 2, newcount)];
System.arraycopy(content, 0, newdata, 0, count);
this.content = newdata;
return 0;
}

View File

@@ -43,7 +43,7 @@ public class HttpMessageServlet extends MessageServlet {
protected void onError(Response response, MessageRecord message, Throwable t) {
if (message.getRespTopic() != null && !message.getRespTopic().isEmpty()) {
HttpMessageRequest request = ((HttpMessageResponse) response).request();
HttpMessageResponse.finishHttpResult(logger.isLoggable(Level.FINEST), response == null ? null : request.getRespConvert(),
HttpMessageResponse.finishHttpResult(logger.isLoggable(Level.FINEST), request == null ? null : request.getRespConvert(),
null, message, messageClient, message.getRespTopic(), new HttpResult().status(500));
}
}

View File

@@ -225,9 +225,7 @@ public abstract class MessageAgent implements Resourcable {
if (this.messageBaseProducer == null) {
messageProducerLock.lock();
try {
if (this.messageBaseProducer == null) {
startMessageProducer();
}
startMessageProducer();
} finally {
messageProducerLock.unlock();
}

View File

@@ -15,12 +15,6 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import org.redkale.cluster.ClusterRpcClient;
import org.redkale.convert.Convert;
import org.redkale.convert.json.JsonConvert;
import static org.redkale.mq.MessageRecord.CTYPE_HTTP_REQUEST;
import static org.redkale.mq.MessageRecord.CTYPE_HTTP_RESULT;
import org.redkale.net.http.HttpResult;
import org.redkale.net.http.HttpSimpleRequest;
import org.redkale.util.RedkaleException;
import org.redkale.util.Traces;
import org.redkale.util.Utility;
@@ -137,18 +131,6 @@ public class MessageClient implements ClusterRpcClient<MessageRecord, MessageRec
return new MessageRecord(seqid, ctype, topic, respTopic, traceid, content);
}
private byte ctype(Convert convert, Object bean) {
byte ctype = 0;
if (convert instanceof JsonConvert) {
if (bean instanceof HttpSimpleRequest) {
ctype = CTYPE_HTTP_REQUEST;
} else if (bean instanceof HttpResult) {
ctype = CTYPE_HTTP_RESULT;
}
}
return ctype;
}
public String getProtocol() {
return protocol;
}

View File

@@ -164,7 +164,7 @@ public abstract class ClientCodec<R extends ClientRequest, P extends ClientResul
});
}
} else {
workThread.runWork(() -> {
Utility.execute(() -> {
Traces.currentTraceid(request.traceid);
respFuture.complete(rs);
Traces.removeTraceid();
@@ -190,7 +190,7 @@ public abstract class ClientCodec<R extends ClientRequest, P extends ClientResul
});
}
} else {
workThread.runWork(() -> {
Utility.execute(() -> {
Traces.currentTraceid(request.traceid);
respFuture.completeExceptionally(exc);
Traces.removeTraceid();
@@ -217,7 +217,7 @@ public abstract class ClientCodec<R extends ClientRequest, P extends ClientResul
});
}
} else {
workThread.runWork(() -> {
Utility.execute(() -> {
Traces.currentTraceid(request.traceid);
respFuture.completeExceptionally(t);
Traces.removeTraceid();

View File

@@ -74,7 +74,9 @@ public class ClientFuture<R extends ClientRequest, T> extends CompletableFuture<
private void runTimeout() {
String traceid = request != null ? request.getTraceid() : null;
conn.removeRespFuture(request.getRequestid(), this);
if (request != null) {
conn.removeRespFuture(request.getRequestid(), this);
}
TimeoutException ex = new TimeoutException("client-request: " + request);
WorkThread workThread = null;
if (request != null) {

View File

@@ -52,7 +52,7 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
protected int writeTimeoutSeconds = 6;
protected HttpSimpleClient(ExecutorService workExecutor, AsyncGroup asyncGroup) {
super("Redkale-http-client", asyncGroup, new ClientAddress(new InetSocketAddress("127.0.0.1", 0)));
super("Redkale-http-client", asyncGroup, new ClientAddress(new InetSocketAddress("127.0.0.1", 0)));
this.workExecutor = workExecutor;
this.asyncGroup = asyncGroup;
}
@@ -417,7 +417,7 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
HttpResult result = this.responseResult;
try {
result.result(c.convertFrom(valueType, this.responseResult.getResult()));
if (workThread != null) {
if (workThread != null && workThread.getState() == Thread.State.RUNNABLE) {
workThread.runWork(() -> {
Traces.currentTraceid(traceid);
future.complete((HttpResult<T>) this.responseResult);
@@ -434,25 +434,25 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
});
}
} catch (Exception e) {
if (workThread != null) {
Utility.execute(() -> {
if (workThread != null && workThread.getState() == Thread.State.RUNNABLE) {
workThread.execute(() -> {
Traces.currentTraceid(traceid);
future.completeExceptionally(e);
});
} else if (workExecutor == null) {
} else if (workExecutor != null) {
workExecutor.execute(() -> {
Traces.currentTraceid(traceid);
future.completeExceptionally(e);
});
} else {
workThread.runWork(() -> {
Utility.execute(() -> {
Traces.currentTraceid(traceid);
future.completeExceptionally(e);
});
}
}
} else {
if (workThread != null) {
if (workThread != null && workThread.getState() == Thread.State.RUNNABLE) {
workThread.runWork(() -> {
Traces.currentTraceid(traceid);
future.complete((HttpResult<T>) this.responseResult);

View File

@@ -7,6 +7,7 @@ package org.redkale.net.http;
import java.io.*;
import java.nio.charset.*;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.atomic.*;
import java.util.logging.*;
@@ -170,7 +171,7 @@ public final class MultiContext {
}
boolean rs = part.save(max < 1 ? Long.MAX_VALUE : max, file);
if (!rs) {
file.delete();
Files.delete(file.toPath());
parent.delete();
} else {
tmpfile = file;

View File

@@ -28,6 +28,7 @@ import org.redkale.net.sncp.Sncp;
import org.redkale.service.*;
import org.redkale.source.Flipper;
import org.redkale.util.*;
import static org.redkale.util.Utility.isEmpty;
/**
* <p>
@@ -1653,17 +1654,18 @@ public final class Rest {
nonblockField.set(obj, parentNonBlocking == null ? true : parentNonBlocking);
return obj;
} catch (ClassNotFoundException e) {
//do nothing
} catch (Throwable e) {
e.printStackTrace();
}
//------------------------------------------------------------------------------
final String defmodulename = getWebModuleNameLowerCase(serviceType);
final String bigmodulename = getWebModuleName(serviceType);
final String defModuleName = getWebModuleNameLowerCase(serviceType);
final String bigModuleName = getWebModuleName(serviceType);
final String catalog = controller == null ? "" : controller.catalog();
if (!checkName(catalog)) {
throw new RestException(serviceType.getName() + " have illegal " + RestService.class.getSimpleName() + ".catalog, only 0-9 a-z A-Z _ cannot begin 0-9");
}
if (!checkName(defmodulename)) {
if (!checkName(defModuleName)) {
throw new RestException(serviceType.getName() + " have illegal " + RestService.class.getSimpleName() + ".value, only 0-9 a-z A-Z _ cannot begin 0-9");
}
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
@@ -1757,14 +1759,14 @@ public final class Rest {
}
retvalTypes.add(rtype);
if (mappings.length == 0) { //没有Mapping设置一个默认值
MappingEntry entry = new MappingEntry(serRpcOnly, methodidex, parentNonBlocking, null, bigmodulename, method);
MappingEntry entry = new MappingEntry(serRpcOnly, methodidex, parentNonBlocking, null, bigModuleName, method);
if (entrys.contains(entry)) {
throw new RestException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat");
}
entrys.add(entry);
} else {
for (RestMapping mapping : mappings) {
MappingEntry entry = new MappingEntry(serRpcOnly, methodidex, parentNonBlocking, mapping, defmodulename, method);
MappingEntry entry = new MappingEntry(serRpcOnly, methodidex, parentNonBlocking, mapping, defModuleName, method);
if (entrys.contains(entry)) {
throw new RestException(serviceType.getName() + " on " + method.getName() + " 's mapping(" + entry.name + ") is repeat");
}
@@ -1793,10 +1795,10 @@ public final class Rest {
break;
}
}
if (defmodulename.isEmpty() || (!pound && entrys.size() <= 2)) {
if (isEmpty(defModuleName) || (!pound && entrys.size() <= 2)) {
Set<String> startWiths = new HashSet<>();
for (MappingEntry entry : entrys) {
String suburl = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defmodulename.isEmpty() ? "" : (defmodulename + "/")) + entry.name;
String suburl = (isEmpty(catalog) ? "/" : ("/" + catalog + "/")) + (isEmpty(defModuleName) ? "" : (defModuleName + "/")) + entry.name;
if ("//".equals(suburl)) {
suburl = "/";
} else if (suburl.length() > 2 && suburl.endsWith("/")) {
@@ -1821,12 +1823,12 @@ public final class Rest {
urlpath = urlpath.substring(1);
}
} else {
urlpath = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defmodulename + "/*";
urlpath = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + defModuleName + "/*";
av1.visit(null, urlpath);
}
av1.visitEnd();
}
av0.visit("name", defmodulename);
av0.visit("name", defModuleName);
av0.visit("moduleid", moduleid);
av0.visit("repair", repair);
av0.visit("comment", comment);
@@ -2288,12 +2290,12 @@ public final class Rest {
av0.visitEnd();
}
av0 = mv.visitAnnotation(mappingDesc, true);
String url = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defmodulename.isEmpty() ? "" : (defmodulename + "/")) + entry.name + (reqpath ? "/" : "");
String url = (catalog.isEmpty() ? "/" : ("/" + catalog + "/")) + (defModuleName.isEmpty() ? "" : (defModuleName + "/")) + entry.name + (reqpath ? "/" : "");
if ("//".equals(url)) {
url = "/";
}
av0.visit("url", url);
av0.visit("name", (defmodulename.isEmpty() ? "" : (defmodulename + "_")) + entry.name);
av0.visit("name", (defModuleName.isEmpty() ? "" : (defModuleName + "_")) + entry.name);
av0.visit("example", entry.example);
av0.visit("rpcOnly", entry.rpcOnly);
av0.visit("auth", entry.auth);

View File

@@ -563,7 +563,7 @@ public final class EntityCache<T> {
return null;
}
private <V> Number getNumberResult(final Collection<T> entityList, final FilterFunc func, final Number defResult, final Class attrType, final Function<T, Number> attrFunc, final FilterNode node) {
private Number getNumberResult(final Collection<T> entityList, final FilterFunc func, final Number defResult, final Class attrType, final Function<T, Number> attrFunc, final FilterNode node) {
final Predicate<T> filter = node == null ? null : node.createPredicate(this);
Stream<T> stream = entityList.stream();
if (filter != null) {