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: 🐛 Bug fix for error when casting to string #732

Merged
merged 1 commit into from
Feb 10, 2024
Merged
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
32 changes: 19 additions & 13 deletions src/main/java/com/ly/doc/utils/JavaFieldUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
*/
package com.ly.doc.utils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ly.doc.constants.DocAnnotationConstants;
import com.ly.doc.constants.DocGlobalConstants;
import com.ly.doc.constants.DocValidatorAnnotationEnum;
Expand All @@ -37,11 +32,22 @@
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.expression.AnnotationValue;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author yu 2019/12/21.
*/
public class JavaFieldUtil {

/**
* public static final
*/
private static final int PUBLIC_STATIC_FINAL = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;

/**
* @param fields list of fields
* @return boolean
Expand Down Expand Up @@ -155,9 +161,7 @@ public static String getJsrComment(ClassLoader classLoader, List<JavaAnnotation>
if (sb.length() < 1) {
return DocGlobalConstants.EMPTY;
}
StringBuilder finalSb = new StringBuilder();
finalSb.append("\nValidate[").append(sb).append("]");
return finalSb.toString();
return "\nValidate[" + sb + "]";
}


Expand All @@ -172,7 +176,7 @@ public static String convertToSimpleTypeName(String str) {
* @param classLoader classLoader
* @param javaClass class
* @param fieldName field name
* @return
* @return Obtain value of constants field
*/
public static String getConstantsFieldValue(ClassLoader classLoader, JavaClass javaClass, String fieldName) {
try {
Expand All @@ -184,15 +188,17 @@ public static String getConstantsFieldValue(ClassLoader classLoader, JavaClass j
}
Field[] fields = c.getDeclaredFields();
for (Field f : fields) {
// 25 is static field
if (f.getModifiers() != 25) {
// if not have public static final
if ((f.getModifiers() & PUBLIC_STATIC_FINAL) != PUBLIC_STATIC_FINAL) {
continue;
}
// if not match field name
if (!f.getName().equals(fieldName)) {
continue;
}
String constantValue = (String) f.get(null);
return constantValue;
// get value
Object constantValue = f.get(null);
return null == constantValue ? null : String.valueOf(constantValue);
}
} catch (ClassNotFoundException | IllegalAccessException e) {
return null;
Expand Down
Loading