Skip to content

Commit

Permalink
enhance prefer string constant's equals to string variable's
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Sep 23, 2023
1 parent bca0953 commit b42e4cf
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MethodWriter method(int modifiers, String name, Class returnType, Class[]

static String getTypeName(Class type) {
Package pkg = type.getPackage();
if (pkg != null && pkg.getName().equals("java.lang") && !type.isArray()) {
if (pkg != null && "java.lang".equals(pkg.getName()) && !type.isArray()) {
return type.getSimpleName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void toString(StringBuilder buf) {
buf.append("\tpublic ");
}

if (name.equals("<init>")) {
if ("<init>".equals(name)) {
buf.append(classWriter.name);
} else {
buf.append(getTypeName(returnType)).append(' ').append(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private void findRelatedReferences() {
break;
}
} else if (parameterCount == 1) {
ignored = methodName.equals("equals");
ignored = "equals".equals(methodName);
}
if (ignored) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ private static Op genReadFieldValueList(
}

OpName for_i;
if (fieldName.equals("i")) {
if ("i".equals(fieldName)) {
for_i = var("j");
} else {
for_i = var("i");
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ public LocalDateTime readLocalDateTime() {
return LocalDateTime.ofInstant(instant, context.getZoneId());
}

if (str.equals("0000-00-00 00:00:00")) {
if ("0000-00-00 00:00:00".equals(str)) {
wasNull = true;
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected MethodCollector(int ignoreCount, int paramCount) {

protected void visitLocalVariable(String name, int index) {
if (index >= ignoreCount && index < ignoreCount + paramCount) {
if (!name.equals("arg" + currentParameter)) {
if (!("arg" + currentParameter).equals(name)) {
debugInfoPresent = true;
}
result.append(',');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void getBeanInfo(BeanInfo beanInfo, Class<?> objectClass) {
Class mixInSource = provider.mixInCache.get(objectClass);
if (mixInSource == null) {
String typeName = objectClass.getName();
if (typeName.equals("org.apache.commons.lang3.tuple.Triple")) {
if ("org.apache.commons.lang3.tuple.Triple".equals(typeName)) {
provider.mixIn(objectClass, mixInSource = ApacheLang3Support.TripleMixIn.class);
}
}
Expand Down Expand Up @@ -326,7 +326,7 @@ private void processJacksonJsonSubTypes(BeanInfo beanInfo, Annotation annotation
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("value")) {
if ("value".equals(name)) {
Object[] value = (Object[]) result;
if (value.length != 0) {
beanInfo.seeAlso = new Class[value.length];
Expand All @@ -349,7 +349,7 @@ private void processJacksonJsonDeserializer(BeanInfo beanInfo, Annotation annota
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("using")) {
if ("using".equals(name)) {
Class using = processUsing((Class) result);
if (using != null) {
beanInfo.deserializer = using;
Expand All @@ -367,7 +367,7 @@ private void processJacksonJsonTypeInfo(BeanInfo beanInfo, Annotation annotation
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("property")) {
if ("property".equals(name)) {
String value = (String) result;
if (!value.isEmpty()) {
beanInfo.typeKey = value;
Expand Down Expand Up @@ -520,7 +520,7 @@ void getBeanInfo1x(BeanInfo beanInfo, Annotation annotation) {
Class<? extends Annotation> builderAnnotationClass = builderAnnotation.annotationType();
String builderAnnotationName = builderAnnotationClass.getName();

if (builderAnnotationName.equals("com.alibaba.fastjson.annotation.JSONPOJOBuilder")) {
if ("com.alibaba.fastjson.annotation.JSONPOJOBuilder".equals(builderAnnotationName)) {
getBeanInfo1xJSONPOJOBuilder(beanInfo, builderClass, builderAnnotation, builderAnnotationClass);
} else {
JSONBuilder jsonBuilder = findAnnotation(builderClass, JSONBuilder.class);
Expand Down Expand Up @@ -572,7 +572,7 @@ private void processSeeAlsoAnnotation(BeanInfo beanInfo, Class<?> objectClass) {
Class mixInSource = provider.mixInCache.get(objectClass);
if (mixInSource == null) {
String typeName = objectClass.getName();
if (typeName.equals("org.apache.commons.lang3.tuple.Triple")) {
if ("org.apache.commons.lang3.tuple.Triple".equals(typeName)) {
provider.mixIn(objectClass, mixInSource = ApacheLang3Support.TripleMixIn.class);
}
}
Expand All @@ -592,7 +592,7 @@ private void processSeeAlsoAnnotation(BeanInfo beanInfo, Annotation[] annotation
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("typeName")) {
if ("typeName".equals(name)) {
String typeName = (String) result;
if (!typeName.isEmpty()) {
beanInfo.typeName = typeName;
Expand Down Expand Up @@ -1003,7 +1003,7 @@ private void processJacksonJsonProperty(FieldInfo fieldInfo, Annotation annotati
}
case "access": {
String access = ((Enum) result).name();
fieldInfo.ignore = access.equals("READ_ONLY");
fieldInfo.ignore = "READ_ONLY".equals(access);
break;
}
case "required":
Expand All @@ -1027,7 +1027,7 @@ private void processJacksonJsonAlias(FieldInfo fieldInfo, Annotation annotation)
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("value")) {
if ("value".equals(name)) {
String[] values = (String[]) result;
if (values.length != 0) {
fieldInfo.alternateNames = values;
Expand Down Expand Up @@ -1289,7 +1289,7 @@ private void getCreator(BeanInfo beanInfo, Class<?> objectClass, Constructor con
creatorMethod = true;
BeanUtils.annotationMethods(annotationType, m1 -> {
try {
if (m1.getName().equals("parameterNames")) {
if ("parameterNames".equals(m1.getName())) {
String[] createParameterNames = (String[]) m1.invoke(annotation);
if (createParameterNames.length != 0) {
beanInfo.createParameterNames = createParameterNames;
Expand Down Expand Up @@ -1331,7 +1331,7 @@ private void getCreator(BeanInfo beanInfo, Class<?> objectClass, Method method)

String methodName = method.getName();
if (objectClass.isEnum()) {
if (methodName.equals("values")) {
if ("values".equals(methodName)) {
return;
}
}
Expand All @@ -1352,7 +1352,7 @@ private void getCreator(BeanInfo beanInfo, Class<?> objectClass, Method method)
creatorMethod = true;
BeanUtils.annotationMethods(annotationType, m1 -> {
try {
if (m1.getName().equals("parameterNames")) {
if ("parameterNames".equals(m1.getName())) {
String[] createParameterNames = (String[]) m1.invoke(annotation);
if (createParameterNames.length != 0) {
beanInfo.createParameterNames = createParameterNames;
Expand All @@ -1368,7 +1368,7 @@ private void getCreator(BeanInfo beanInfo, Class<?> objectClass, Method method)
creatorMethod = true;
BeanUtils.annotationMethods(annotationType, m1 -> {
try {
if (m1.getName().equals("parameterNames")) {
if ("parameterNames".equals(m1.getName())) {
String[] createParameterNames = (String[]) m1.invoke(annotation);
if (createParameterNames.length != 0) {
beanInfo.createParameterNames = createParameterNames;
Expand Down Expand Up @@ -1605,7 +1605,7 @@ public ObjectReader getObjectReader(ObjectReaderProvider provider, Type type) {
if (mixin == null) {
mixin = TypeUtils.loadClass(internalMixin);
if (mixin == null) {
if (internalMixin.equals("org.springframework.security.jackson2.SimpleGrantedAuthorityMixin")) {
if ("org.springframework.security.jackson2.SimpleGrantedAuthorityMixin".equals(internalMixin)) {
mixin = TypeUtils.loadClass("com.alibaba.fastjson2.internal.mixin.spring.SimpleGrantedAuthorityMixin");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ private ObjectReader getObjectReaderInternal(Type objectType, boolean fieldBased

String className = objectClass.getName();
if (!fieldBased) {
if (className.equals("com.google.common.collect.ArrayListMultimap")) {
if ("com.google.common.collect.ArrayListMultimap".equals(className)) {
objectReader = ObjectReaderImplMap.of(null, objectClass, 0);
}
}
Expand All @@ -816,7 +816,7 @@ private ObjectReader getObjectReaderInternal(Type objectType, boolean fieldBased
Annotation[] annotations = objectClass.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
jsonCompiled = annotationType.getName().equals("com.alibaba.fastjson2.annotation.JSONCompiled");
jsonCompiled = "com.alibaba.fastjson2.annotation.JSONCompiled".equals(annotationType.getName());
}
if (jsonCompiled) {
String codeGenClassName = objectClass.getName() + "_FASTJOSNReader";
Expand Down
26 changes: 13 additions & 13 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static void declaredFields(Class objectClass, Consumer<Field> fieldConsum
if (superClass != null
&& superClass != Object.class
) {
protobufMessageV3 = superClass.getName().equals("com.google.protobuf.GeneratedMessageV3");
protobufMessageV3 = "com.google.protobuf.GeneratedMessageV3".equals(superClass.getName());
if (!protobufMessageV3) {
declaredFields(superClass, fieldConsumer);
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public static void declaredFields(Class objectClass, Consumer<Field> fieldConsum
String fieldName = field.getName();
Class<?> fieldClass = field.getType();
if ("cardsmap_".equals(fieldName)
&& fieldClass.getName().equals("com.google.protobuf.MapField")) {
&& "com.google.protobuf.MapField".equals(fieldClass.getName())) {
return;
}
}
Expand Down Expand Up @@ -724,7 +724,7 @@ public static Member getEnumValueField(Class enumClass, ObjectCodecProvider mixi
}

String methodName = method.getName();
if (methodName.equals("values")) {
if ("values".equals(methodName)) {
continue;
}

Expand Down Expand Up @@ -844,7 +844,7 @@ boolean record = isRecord(objectClass);
methodCache.putIfAbsent(objectClass, methods);
}

boolean protobufMessageV3 = superClass != null && superClass.getName().equals("com.google.protobuf.GeneratedMessageV3");
boolean protobufMessageV3 = superClass != null && "com.google.protobuf.GeneratedMessageV3".equals(superClass.getName());

for (Method method : methods) {
int paramType = method.getParameterCount();
Expand Down Expand Up @@ -893,7 +893,7 @@ boolean record = isRecord(objectClass);

if (protobufMessageV3) {
if ((methodName.endsWith("Type") || methodName.endsWith("Bytes"))
&& returnClass.getName().equals("com.google.protobuf.ByteString")) {
&& "com.google.protobuf.ByteString".equals(returnClass.getName())) {
continue;
}
}
Expand Down Expand Up @@ -1203,9 +1203,9 @@ public static Field getField(Class objectClass, Method method) {

Field field = fields[0] != null ? fields[0] : fields[1];
if (Throwable.class.isAssignableFrom(objectClass)) {
if (returnType == String.class && (field == null && methodName.equals("getMessage") || field == null && methodName.equals("getLocalizedMessage"))) {
if (returnType == String.class && (field == null && "getMessage".equals(methodName) || field == null && "getLocalizedMessage".equals(methodName))) {
field = getDeclaredField(objectClass, "detailMessage");
} else if (returnType == Throwable[].class && methodName.equals("getSuppressed")) {
} else if (returnType == Throwable[].class && "getSuppressed".equals(methodName)) {
field = getDeclaredField(objectClass, "suppressedExceptions");
}
}
Expand Down Expand Up @@ -2334,7 +2334,7 @@ public static void processJacksonJsonIgnore(FieldInfo fieldInfo, Annotation anno
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("value")) {
if ("value".equals(name)) {
fieldInfo.ignore = (boolean) (Boolean) result;
}
} catch (Throwable ignored) {
Expand Down Expand Up @@ -2623,7 +2623,7 @@ public static void processJacksonJsonFormat(FieldInfo fieldInfo, Annotation anno
}
case "shape": {
String shape = ((Enum) result).name();
if (shape.equals("STRING")) {
if ("STRING".equals(shape)) {
fieldInfo.features |= JSONWriter.Feature.WriteNonStringValueAsString.mask;
}
break;
Expand All @@ -2643,7 +2643,7 @@ public static void processJacksonJsonFormat(BeanInfo beanInfo, Annotation annota
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("pattern")) {
if ("pattern".equals(name)) {
String pattern = (String) result;
if (pattern.length() != 0) {
beanInfo.format = pattern;
Expand All @@ -2661,7 +2661,7 @@ public static void processJacksonJsonInclude(BeanInfo beanInfo, Annotation annot
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("value")) {
if ("value".equals(name)) {
String include = ((Enum) result).name();
switch (include) {
case "ALWAYS":
Expand All @@ -2686,7 +2686,7 @@ public static void processJacksonJsonTypeName(BeanInfo beanInfo, Annotation anno
String name = m.getName();
try {
Object result = m.invoke(annotation);
if (name.equals("value")) {
if ("value".equals(name)) {
String value = (String) result;
if (!value.isEmpty()) {
beanInfo.typeName = value;
Expand Down Expand Up @@ -2780,7 +2780,7 @@ public static boolean isExtendedMap(Class objectClass) {
if (Modifier.isStatic(modifiers)
|| Modifier.isTransient(modifiers)
|| field.getDeclaringClass().isAssignableFrom(superclass)
|| field.getName().equals("this$0")
|| "this$0".equals(field.getName())
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, T

public static Function createConvertFunction(Class objectClass) {
String instanceTypeName = objectClass.getName();
if (instanceTypeName.equals("com.google.common.collect.ArrayListMultimap")) {
if ("com.google.common.collect.ArrayListMultimap".equals(instanceTypeName)) {
if (CLASS_ARRAYLIST_MULTI_MAP == null) {
CLASS_ARRAYLIST_MULTI_MAP = objectClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class JDKUtils {
try {
String jmvName = System.getProperty("java.vm.name");
openj9 = jmvName.contains("OpenJ9");
android = jmvName.equals("Dalvik");
android = "Dalvik".equals(jmvName);
graal = System.getProperty("org.graalvm.nativeimage.imagecode") != null;
if (openj9 || android || graal) {
FIELD_STRING_VALUE_ERROR = true;
Expand Down
Loading

0 comments on commit b42e4cf

Please sign in to comment.