1.Annotation referencing a non-empty enum array returns an empty array

Environment when first discovered: JDK 1.8

First discovery project: APIJSON

Test cases:

Public enum RequestRole {/** Unregistered, unidentified user */ UNKNOWN, /** Registered user */ LOGIN, /** Contact, Must be logged in */ CONTACT, /** CIRCLE member (CONTACT + OWNER), must be logged in */ CIRCLE, /** OWNER, must be logged in */ OWNER, /** administrator, must be logged in */ ADMIN; // It seems that no matter what you do, the external reference is null. And if the position in the annotation is not first, it will cause the annotated class to import from other classes. // Although the direct print display is normal, Public static final RequestRole[] ALL = {RequestRole.UNKNOWN}; public static final RequestRole[] ALL = {RequestRole. //values(); Unprecedented; // all public static final RequestRole[] unprecedented; Unprecedented = new RequestRole[] {OWNER, ADMIN}; } public static final String[] NAMES = { UNKNOWN.name(), LOGIN.name(), CONTACT.name(), CIRCLE.name(), OWNER.name(), ADMIN.name() }; } @MethodAccess( GETS = RequestRole.ALL, HEADS = RequestRole.HIGHS ) public class Verify { } public class DemoVerifier { // <TableName, <METHOD, allowRoles>> // <User, <GET, [OWNER, ADMIN]>> public static final Map<String, Map<RequestMethod, RequestRole[]>> ACCESS_MAP; Static {// ACCESS_MAP = new HashMap<String, Map<RequestMethod, RequestRole[]>>(); ACCESS_MAP.put(Verify.class.getSimpleName(), getAccessMap(Verify.class.getAnnotation(MethodAccess.class))); } public static HashMap<RequestMethod, RequestRole[]> getAccessMap(MethodAccess access) { if (access == null) { return null; } HashMap<RequestMethod, RequestRole[]> map = new HashMap<>(); map.put(GET, access.GET()); map.put(HEAD, access.HEAD()); map.put(GETS, access.GETS()); map.put(HEADS, access.HEADS()); map.put(POST, access.POST()); map.put(PUT, access.PUT()); map.put(DELETE, access.DELETE()); return map; }}Copy the code

Solution:

Instead of abstracting array constants like ALL,HIGHTS, etc., hardcode concrete values wherever they are used.

 

2.ArrayList can pass a List of non-specified generics through the constructor and fail on get

Environment when first discovered: JDK 1.7

First discovery project: APIJSON

Test cases:

JSONArray arr = new JSONArray(); //com.alibaba.fastjson.JSONArray
arr.add("s");

List<Long> list = new ArrayList<>(arr); 
list.get(0); //throw new IllegalArgumentExceptionCopy the code

Solution:

1. Use Open JDK8 instead

2. Update the JDK

Note: the subsequent tests cannot be repeated.

 

3. Basic types can be assigned to null in ternary expressions

Environment when first discovered: JDK 1.7

Test cases:

int i = true ? null : 0; //Exception in thread "main" java.lang.NullPointerExceptionCopy the code

First discovery project: ZBLibrary

Solution:

When assigning a 3-element expression to an underlying type, NULL is first converted to the default value of the underlying type.

 

Finally, two more issues that are not bugs, but are prone to causing programming bugs:

1. Local variables and global variables with the same name can be compiled in a method, and run normally.

public class Test { int val; @Override public String toString() { val = 1; String val = ""; return super.toString(); }}Copy the code

If there is a long gap between two variables and other code, it is very likely that developers will confuse the two, leading to logical cognitive errors that can lead to problematic code being written or changed.

Solution:

Search for local variables before naming them, making sure there are no declared global variables with the same name.

 

Gson uses TypeToken to convert List<T> to data not of type T. Gson uses TypeToken to convert List<T> to data of type T.


        String json = "[1, '2', 'a']";
        Type type = new TypeToken<Integer>(){}.getType();
        Gson gson = new Gson();
        List<Integer> list = gson.fromJson(json, type);
        
        Integer i = list == null || list.isEmpty() ? null : list.get(0); //Exception cannot cast String to Integer
Copy the code

Solution:

1. Manually check that the data in the list conforms to the generic T

Switch to another statically checked type of library, such as Fastjson.