@Test public void testParseRequiredType() { assertEquals(Long.class, Parsers.parseType("java.lang.Long", null)); assertNull(Parsers.parseType("java.lang.Comparable<Long>", null)); Type type = Parsers.parseType("java.lang.Comparable<java.lang.Long>", null); assertTrue(type instanceof ParameterizedType); ParameterizedType parameterizedType = (ParameterizedType) type; assertEquals(Comparable.class, parameterizedType.getRawType()); assertEquals(1, parameterizedType.getActualTypeArguments().length); assertEquals(Long.class, parameterizedType.getActualTypeArguments()[0]); type = Parsers.parseType("java.util.Map<java.lang.String,java.lang.Boolean>", null); assertTrue(type instanceof ParameterizedType); parameterizedType = (ParameterizedType) type; assertEquals(Map.class, parameterizedType.getRawType()); assertEquals(2, parameterizedType.getActualTypeArguments().length); assertEquals(String.class, parameterizedType.getActualTypeArguments()[0]); assertEquals(Boolean.class, parameterizedType.getActualTypeArguments()[1]); type = Parsers.parseType("java.util.Map<java.lang.String,java.util.List<java.lang.Integer>>", null); assertTrue(type instanceof ParameterizedType); parameterizedType = (ParameterizedType) type; assertEquals(Map.class, parameterizedType.getRawType()); assertEquals(2, parameterizedType.getActualTypeArguments().length); assertEquals(String.class, parameterizedType.getActualTypeArguments()[0]); Type nestedType = parameterizedType.getActualTypeArguments()[1]; assertTrue(nestedType instanceof ParameterizedType); ParameterizedType nestedParameterizedType = (ParameterizedType) nestedType; assertEquals(List.class, nestedParameterizedType.getRawType()); assertEquals(1, nestedParameterizedType.getActualTypeArguments().length); assertEquals(Integer.class, nestedParameterizedType.getActualTypeArguments()[0]); type = Parsers.parseType("java.lang.Comparable<? extends java.lang.Number>", null); assertTrue(type instanceof ParameterizedType); parameterizedType = (ParameterizedType) type; assertEquals(1, parameterizedType.getActualTypeArguments().length); Type typeArgument = parameterizedType.getActualTypeArguments()[0]; assertTrue(typeArgument instanceof WildcardType); WildcardType wildcardType = (WildcardType) typeArgument; assertEquals(1, wildcardType.getUpperBounds().length); assertEquals(0, wildcardType.getLowerBounds().length); assertEquals(Number.class, wildcardType.getUpperBounds()[0]); type = Parsers.parseType("java.lang.Comparable<? super java.lang.Integer>", null); assertTrue(type instanceof ParameterizedType); parameterizedType = (ParameterizedType) type; assertEquals(1, parameterizedType.getActualTypeArguments().length); typeArgument = parameterizedType.getActualTypeArguments()[0]; assertTrue(typeArgument instanceof WildcardType); wildcardType = (WildcardType) typeArgument; assertEquals(1, wildcardType.getUpperBounds().length); assertEquals(1, wildcardType.getLowerBounds().length); assertEquals(Object.class, wildcardType.getUpperBounds()[0]); assertEquals(Integer.class, wildcardType.getLowerBounds()[0]); type = Parsers.parseType("java.util.List<java.lang.Integer>[]", null); assertTrue(type instanceof GenericArrayType); GenericArrayType arrayType = (GenericArrayType) type; assertTrue(arrayType.getGenericComponentType() instanceof ParameterizedType); parameterizedType = (ParameterizedType) arrayType.getGenericComponentType(); assertEquals(List.class, nestedParameterizedType.getRawType()); assertEquals(1, nestedParameterizedType.getActualTypeArguments().length); assertEquals(Integer.class, nestedParameterizedType.getActualTypeArguments()[0]); }
static Type parseType(String value, ResourceLoader resourceLoader) { value = value.trim(); if (value.equals(WILDCARD)) { return WildcardTypeImpl.defaultInstance(); } if (value.startsWith(WILDCARD_EXTENDS)) { Type upperBound = parseType(value.substring(WILDCARD_EXTENDS.length(), value.length()), resourceLoader); if (upperBound == null) { return null; } return WildcardTypeImpl.withUpperBound(upperBound); } if (value.startsWith(WILDCARD_SUPER)) { Type lowerBound = parseType(value.substring(WILDCARD_SUPER.length(), value.length()), resourceLoader); if (lowerBound == null) { return null; } return WildcardTypeImpl.withLowerBound(lowerBound); } if (value.contains(ARRAY)) { Type componentType = parseType(value.substring(0, value.indexOf(ARRAY)), resourceLoader); if (componentType == null) { return null; } return new GenericArrayTypeImpl(componentType); } int chevLeft = value.indexOf(CHEVRONS_LEFT); String rawValue = chevLeft < 0 ? value : value.substring(0, chevLeft); Class<?> rawRequiredType = tryLoadClass(rawValue, resourceLoader); if (rawRequiredType == null) { return null; } if (rawRequiredType.getTypeParameters().length == 0) { return rawRequiredType; } int chevRight = value.lastIndexOf(CHEVRONS_RIGHT); if (chevRight < 0) { return null; } List<String> parts = split(value.substring(chevLeft + 1, chevRight), ',', CHEVRONS_LEFT.charAt(0), CHEVRONS_RIGHT.charAt(0)); Type[] typeParameters = new Type[parts.size()]; for (int i = 0; i < typeParameters.length; i++) { Type typeParam = parseType(parts.get(i), resourceLoader); if (typeParam == null) { return null; } typeParameters[i] = typeParam; } return new ParameterizedTypeImpl(rawRequiredType, typeParameters); }
Parsers { static Type parseType(String value, ResourceLoader resourceLoader) { value = value.trim(); if (value.equals(WILDCARD)) { return WildcardTypeImpl.defaultInstance(); } if (value.startsWith(WILDCARD_EXTENDS)) { Type upperBound = parseType(value.substring(WILDCARD_EXTENDS.length(), value.length()), resourceLoader); if (upperBound == null) { return null; } return WildcardTypeImpl.withUpperBound(upperBound); } if (value.startsWith(WILDCARD_SUPER)) { Type lowerBound = parseType(value.substring(WILDCARD_SUPER.length(), value.length()), resourceLoader); if (lowerBound == null) { return null; } return WildcardTypeImpl.withLowerBound(lowerBound); } if (value.contains(ARRAY)) { Type componentType = parseType(value.substring(0, value.indexOf(ARRAY)), resourceLoader); if (componentType == null) { return null; } return new GenericArrayTypeImpl(componentType); } int chevLeft = value.indexOf(CHEVRONS_LEFT); String rawValue = chevLeft < 0 ? value : value.substring(0, chevLeft); Class<?> rawRequiredType = tryLoadClass(rawValue, resourceLoader); if (rawRequiredType == null) { return null; } if (rawRequiredType.getTypeParameters().length == 0) { return rawRequiredType; } int chevRight = value.lastIndexOf(CHEVRONS_RIGHT); if (chevRight < 0) { return null; } List<String> parts = split(value.substring(chevLeft + 1, chevRight), ',', CHEVRONS_LEFT.charAt(0), CHEVRONS_RIGHT.charAt(0)); Type[] typeParameters = new Type[parts.size()]; for (int i = 0; i < typeParameters.length; i++) { Type typeParam = parseType(parts.get(i), resourceLoader); if (typeParam == null) { return null; } typeParameters[i] = typeParam; } return new ParameterizedTypeImpl(rawRequiredType, typeParameters); } }
Parsers { static Type parseType(String value, ResourceLoader resourceLoader) { value = value.trim(); if (value.equals(WILDCARD)) { return WildcardTypeImpl.defaultInstance(); } if (value.startsWith(WILDCARD_EXTENDS)) { Type upperBound = parseType(value.substring(WILDCARD_EXTENDS.length(), value.length()), resourceLoader); if (upperBound == null) { return null; } return WildcardTypeImpl.withUpperBound(upperBound); } if (value.startsWith(WILDCARD_SUPER)) { Type lowerBound = parseType(value.substring(WILDCARD_SUPER.length(), value.length()), resourceLoader); if (lowerBound == null) { return null; } return WildcardTypeImpl.withLowerBound(lowerBound); } if (value.contains(ARRAY)) { Type componentType = parseType(value.substring(0, value.indexOf(ARRAY)), resourceLoader); if (componentType == null) { return null; } return new GenericArrayTypeImpl(componentType); } int chevLeft = value.indexOf(CHEVRONS_LEFT); String rawValue = chevLeft < 0 ? value : value.substring(0, chevLeft); Class<?> rawRequiredType = tryLoadClass(rawValue, resourceLoader); if (rawRequiredType == null) { return null; } if (rawRequiredType.getTypeParameters().length == 0) { return rawRequiredType; } int chevRight = value.lastIndexOf(CHEVRONS_RIGHT); if (chevRight < 0) { return null; } List<String> parts = split(value.substring(chevLeft + 1, chevRight), ',', CHEVRONS_LEFT.charAt(0), CHEVRONS_RIGHT.charAt(0)); Type[] typeParameters = new Type[parts.size()]; for (int i = 0; i < typeParameters.length; i++) { Type typeParam = parseType(parts.get(i), resourceLoader); if (typeParam == null) { return null; } typeParameters[i] = typeParam; } return new ParameterizedTypeImpl(rawRequiredType, typeParameters); } private  Parsers(); }
Parsers { static Type parseType(String value, ResourceLoader resourceLoader) { value = value.trim(); if (value.equals(WILDCARD)) { return WildcardTypeImpl.defaultInstance(); } if (value.startsWith(WILDCARD_EXTENDS)) { Type upperBound = parseType(value.substring(WILDCARD_EXTENDS.length(), value.length()), resourceLoader); if (upperBound == null) { return null; } return WildcardTypeImpl.withUpperBound(upperBound); } if (value.startsWith(WILDCARD_SUPER)) { Type lowerBound = parseType(value.substring(WILDCARD_SUPER.length(), value.length()), resourceLoader); if (lowerBound == null) { return null; } return WildcardTypeImpl.withLowerBound(lowerBound); } if (value.contains(ARRAY)) { Type componentType = parseType(value.substring(0, value.indexOf(ARRAY)), resourceLoader); if (componentType == null) { return null; } return new GenericArrayTypeImpl(componentType); } int chevLeft = value.indexOf(CHEVRONS_LEFT); String rawValue = chevLeft < 0 ? value : value.substring(0, chevLeft); Class<?> rawRequiredType = tryLoadClass(rawValue, resourceLoader); if (rawRequiredType == null) { return null; } if (rawRequiredType.getTypeParameters().length == 0) { return rawRequiredType; } int chevRight = value.lastIndexOf(CHEVRONS_RIGHT); if (chevRight < 0) { return null; } List<String> parts = split(value.substring(chevLeft + 1, chevRight), ',', CHEVRONS_LEFT.charAt(0), CHEVRONS_RIGHT.charAt(0)); Type[] typeParameters = new Type[parts.size()]; for (int i = 0; i < typeParameters.length; i++) { Type typeParam = parseType(parts.get(i), resourceLoader); if (typeParam == null) { return null; } typeParameters[i] = typeParam; } return new ParameterizedTypeImpl(rawRequiredType, typeParameters); } private  Parsers();  }
Parsers { static Type parseType(String value, ResourceLoader resourceLoader) { value = value.trim(); if (value.equals(WILDCARD)) { return WildcardTypeImpl.defaultInstance(); } if (value.startsWith(WILDCARD_EXTENDS)) { Type upperBound = parseType(value.substring(WILDCARD_EXTENDS.length(), value.length()), resourceLoader); if (upperBound == null) { return null; } return WildcardTypeImpl.withUpperBound(upperBound); } if (value.startsWith(WILDCARD_SUPER)) { Type lowerBound = parseType(value.substring(WILDCARD_SUPER.length(), value.length()), resourceLoader); if (lowerBound == null) { return null; } return WildcardTypeImpl.withLowerBound(lowerBound); } if (value.contains(ARRAY)) { Type componentType = parseType(value.substring(0, value.indexOf(ARRAY)), resourceLoader); if (componentType == null) { return null; } return new GenericArrayTypeImpl(componentType); } int chevLeft = value.indexOf(CHEVRONS_LEFT); String rawValue = chevLeft < 0 ? value : value.substring(0, chevLeft); Class<?> rawRequiredType = tryLoadClass(rawValue, resourceLoader); if (rawRequiredType == null) { return null; } if (rawRequiredType.getTypeParameters().length == 0) { return rawRequiredType; } int chevRight = value.lastIndexOf(CHEVRONS_RIGHT); if (chevRight < 0) { return null; } List<String> parts = split(value.substring(chevLeft + 1, chevRight), ',', CHEVRONS_LEFT.charAt(0), CHEVRONS_RIGHT.charAt(0)); Type[] typeParameters = new Type[parts.size()]; for (int i = 0; i < typeParameters.length; i++) { Type typeParam = parseType(parts.get(i), resourceLoader); if (typeParam == null) { return null; } typeParameters[i] = typeParam; } return new ParameterizedTypeImpl(rawRequiredType, typeParameters); } private  Parsers();   }