com.github.javaparser.resolution.types.ResolvedType Java Examples

The following examples show how to use com.github.javaparser.resolution.types.ResolvedType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaParserUtil.java    From Recaf with MIT License 6 votes vote down vote up
/**
 * Converts the resolved type to an internal representation.
 * If the type is an array the component type's internal name is returned.
 * Primitives return their boxed names.
 *
 * @param type
 * 		JavaParser resolved type.
 *
 * @return Internalized representation.
 */
public static String toInternal(ResolvedType type) {
	if(type.isVoid() || type.isPrimitive())
		return type.asPrimitive().getBoxTypeQName().replace(".", "/");
	if(type.isArray())
		return toInternal(type.asArrayType().getComponentType());
	if(type.isReference()) {
		if(type.asReferenceType().getTypeDeclaration() != null)
			return toInternal(type.asReferenceType().getTypeDeclaration().get());
		else
			return type.asReferenceType().getQualifiedName().replace(".", "/");
	}
	// The above cases should have internalized the name...
	// If not lets be alerted of a uncaught case.
	throw new IllegalStateException("Cannot internalize type: " + type);
}
 
Example #2
Source File: JavaParserUtil.java    From Recaf with MIT License 6 votes vote down vote up
/**
 * @param type
 *            JavaParser type. Must be an object type.
 * @return Internal descriptor from type, assuming the type is available.
 */
private static String typeToDesc(ResolvedType type) {
	String qualified = null;
	if(type instanceof ResolvedTypeVariable)
		qualified = ((ResolvedTypeVariable) type).qualifiedName();
	else if(type instanceof ResolvedTypeParameterDeclaration)
		qualified = type.asTypeParameter().getQualifiedName();
	else if(type.isPrimitive())
		return primTypeToDesc(type.asPrimitive());
	else if(type.isVoid())
		return "V";
	else
		qualified = toInternal(type);
	if(qualified == null)
		return null;
	// Substring out generics
	if(qualified.contains("<") && qualified.contains(">"))
		qualified = qualified.substring(0, qualified.indexOf('<'));
	StringBuilder sbDesc = new StringBuilder();
	for(int i = 0; i < type.arrayLevel(); i++)
		sbDesc.append("[");
	sbDesc.append("L");
	sbDesc.append(qualified.replace('.', '/'));
	sbDesc.append(";");
	return sbDesc.toString();
}
 
Example #3
Source File: FieldHelper.java    From apigcc with MIT License 6 votes vote down vote up
/**
 * 获取真实的属性类型
 * @param parent
 * @param field
 * @return
 */
public static ResolvedType getActuallyType(ResolvedReferenceType parent, ResolvedFieldDeclaration field) {
    ResolvedType type = field.getType();
    if (type.isReferenceType()) {
        //将父类的T,传递给 属性的T
        return ClassHelper.useClassTypeParameter(parent, type.asReferenceType());
    }
    if (type.isTypeVariable()) {
        //类型为T,这种泛型
        Optional<ResolvedType> optional = ClassHelper.getTypeParameter(parent, field.getType().asTypeParameter().getName());
        if (optional.isPresent()) {
            return optional.get();
        }
    }
    return type;
}
 
Example #4
Source File: ParserTypeUtil.java    From JRemapper with MIT License 6 votes vote down vote up
/**
 * @param type
 *            JavaParser type. Must be an object type.
 * @return Internal descriptor from type, assuming the type is available.
 */
private static String typeToDesc(ResolvedType type) {
	String qualified = null;
	if (type instanceof ResolvedTypeVariable) {
		qualified = ((ResolvedTypeVariable) type).qualifiedName();
	} else if (type instanceof ResolvedTypeParameterDeclaration) {
		qualified = type.asTypeParameter().getQualifiedName();;
	} else if(type.isPrimitive()) {
		return primTypeToDesc(type.asPrimitive());
	} else if(type.isVoid()) {
		return "V";
	} else {
		qualified = type.describe();
	}
	if (qualified == null)
		return null;
	StringBuilder sbDesc = new StringBuilder();
	for (int i = 0; i < type.arrayLevel(); i++)
		sbDesc.append("[");
	sbDesc.append("L");
	sbDesc.append(qualified.replace('.', '/'));
	sbDesc.append(";");
	return sbDesc.toString();
}
 
Example #5
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_ReturnArraySchema_When_GivenTypeIsAnArray() {
    ResolvedType arrayType = mock(ResolvedType.class);
    ResolvedArrayType arrayResolvedType = mock(ResolvedArrayType.class);
    ResolvedType stringType = mockReferencedTypeOf(String.class);

    when(arrayType.isArray()).thenReturn(true);
    when(arrayType.asArrayType()).thenReturn(arrayResolvedType);
    when(arrayResolvedType.getComponentType()).thenReturn(stringType);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(arrayType);
    Assert.assertTrue(schema instanceof ArraySchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(
            ((ArraySchema) schema).getItems() instanceof StringSchema);
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #6
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_ReturnNullableOptional_When_GivenTypeIsAnOptionalBean() {
    ResolvedType resolvedType = mockReferencedTypeOf(Optional.class);
    ResolvedReferenceType resolvedReferenceType = resolvedType
            .asReferenceType();

    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = Collections
            .singletonList(
                    new Pair<>(null, mockReferencedTypeOf(TestBean.class)));
    when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof ComposedSchema);
    Assert.assertTrue(schema.getNullable());
    Assert.assertEquals(1, ((ComposedSchema) schema).getAllOf().size());
    String beanRef = schemaResolver
            .getFullQualifiedNameRef(TestBean.class.getCanonicalName());
    Assert.assertEquals(beanRef,
            ((ComposedSchema) schema).getAllOf().get(0).get$ref());

    Assert.assertEquals(1, schemaResolver.getFoundTypes().size());
}
 
Example #7
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_ReturnNullableOptional_When_GivenTypeIsAnOptionalString() {
    ResolvedType resolvedType = mockReferencedTypeOf(Optional.class);
    ResolvedReferenceType resolvedReferenceType = resolvedType
            .asReferenceType();

    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = Collections
            .singletonList(
                    new Pair<>(null, mockReferencedTypeOf(String.class)));
    when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof StringSchema);
    Assert.assertTrue(schema.getNullable());

    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #8
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_ReturnNotNullableMap_When_GivenTypeIsAMap() {
    ResolvedType resolvedType = mockReferencedTypeOf(Map.class);
    ResolvedReferenceType resolvedReferenceType = resolvedType
            .asReferenceType();
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = new LinkedList<>();
    pairs.add(null);
    pairs.add(new Pair<>(null, mockReferencedTypeOf(Number.class)));
    when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof MapSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(
            schema.getAdditionalProperties() instanceof NumberSchema);

    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #9
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void should_ReturnNotNullableArray_When_GivenTypeIsAListString() {
    ResolvedType resolvedType = mockReferencedTypeOf(Collection.class);
    ResolvedReferenceType resolvedReferenceType = resolvedType
            .asReferenceType();

    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> pairs = Collections
            .singletonList(
                    new Pair<>(null, mockReferencedTypeOf(String.class)));
    when(resolvedReferenceType.getTypeParametersMap()).thenReturn(pairs);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof ArraySchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(
            ((ArraySchema) schema).getItems() instanceof StringSchema);
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #10
Source File: MemberExplorer.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public List<ResolvedTypeDeclaration> getTypeArgumentDeclarations() {
    List<ResolvedTypeDeclaration> declarations = new ArrayList<>();
    for (Pair<ResolvedTypeParameterDeclaration, ResolvedType> pair : getReferenceType().getTypeParametersMap()) {
        if (pair.b.isReferenceType()) {
            declarations.add(pair.b.asReferenceType().getTypeDeclaration());
        } else if (pair.b.isTypeVariable()) { // generics
            declarations.add(pair.b.asTypeVariable().asTypeParameter());
        } // isTypeVariable()asTypeParameter()
    }
    return declarations;
}
 
Example #11
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableNumberSchema_When_GivenTypeIsAPrimitiveInt() {
    ResolvedType numberType = mockPrimitiveTypeOf(
            ResolvedPrimitiveType.INT);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(numberType);
    Assert.assertTrue(schema instanceof NumberSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #12
Source File: MemberExplorer.java    From jeddict with Apache License 2.0 5 votes vote down vote up
public List<String> getTypeArguments() {
    List<String> args = new ArrayList<>();
    for (Pair<ResolvedTypeParameterDeclaration, ResolvedType> pair : getReferenceType().getTypeParametersMap()) {
        if (pair.b.isReferenceType()) {
            args.add(pair.b.asReferenceType().getQualifiedName());
        } else if (pair.b.isTypeVariable()) { // generics
            args.add(pair.b.asTypeVariable().describe());
        }
    }
    return args;
}
 
Example #13
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private ResolvedType mockPrimitiveTypeOf(
        ResolvedPrimitiveType primitiveType) {
    ResolvedType numberType = mock(ResolvedType.class);

    when(numberType.isPrimitive()).thenReturn(true);
    when(numberType.asPrimitive()).thenReturn(primitiveType);
    return numberType;
}
 
Example #14
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private ResolvedType mockReferencedTypeOf(Class clazz) {
    ResolvedType resolvedType = mock(ResolvedType.class);
    ResolvedReferenceType resolvedReferenceType = mock(
            ResolvedReferenceType.class);

    when(resolvedType.isPrimitive()).thenReturn(false);
    when(resolvedType.isReferenceType()).thenReturn(true);
    when(resolvedType.asReferenceType()).thenReturn(resolvedReferenceType);
    when(resolvedReferenceType.getQualifiedName())
            .thenReturn(clazz.getCanonicalName());
    return resolvedType;
}
 
Example #15
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableBeanSchema_When_GivenTypeIsABeanType() {
    ResolvedType resolvedType = mockReferencedTypeOf(TestBean.class);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof ObjectSchema);
    Assert.assertNull(schema.getNullable());
    String beanRef = schemaResolver
            .getFullQualifiedNameRef(TestBean.class.getCanonicalName());
    Assert.assertEquals(beanRef, schema.get$ref());

    Assert.assertEquals(1, schemaResolver.getFoundTypes().size());
}
 
Example #16
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableObject_When_GivenTypeIsAnUnhandledJavaType() {
    ResolvedType resolvedType = mockReferencedTypeOf(Class.class);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertNotNull(schema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #17
Source File: SpringComponentResolver.java    From apigcc with MIT License 5 votes vote down vote up
@Override
public TypeDescription resolve(ResolvedType type) {

    Optional<ResolvedType> optional = ClassHelper.getTypeParameter(type.asReferenceType(), 0);
    return optional
            .map(Apigcc.getInstance().getTypeResolvers()::resolve)
            .orElseGet(UnAvailableTypeDescription::new);
}
 
Example #18
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableStringSchema_When_GivenTypeIsAString() {
    ResolvedType resolvedType = mockReferencedTypeOf(String.class);
    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof StringSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #19
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableBoolean_When_GivenTypeIsAPrimitiveBoolean() {
    ResolvedType resolvedType = mockPrimitiveTypeOf(
            ResolvedPrimitiveType.BOOLEAN);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof BooleanSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #20
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableNumberSchema_When_GivenTypeIsANumber() {
    ResolvedType numberType = mockReferencedTypeOf(Number.class);

    Schema schema = schemaResolver.parseResolvedTypeToSchema(numberType);
    Assert.assertTrue(schema instanceof NumberSchema);
    Assert.assertNull(schema.getNullable());
    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #21
Source File: SchemaResolverTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void should_ReturnNotNullableDate_When_GivenTypeIsAInstant() {
    ResolvedType resolvedType = mockReferencedTypeOf(Instant.class);
    Schema schema = schemaResolver.parseResolvedTypeToSchema(resolvedType);

    Assert.assertTrue(schema instanceof DateTimeSchema);
    Assert.assertNull(schema.getNullable());

    Assert.assertTrue(schemaResolver.getFoundTypes().isEmpty());
}
 
Example #22
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private Schema createEnumTypeSchema(ResolvedType resolvedType) {
    ResolvedReferenceType type = resolvedType.asReferenceType();
    List<String> entries = type
            .getTypeDeclaration().asEnum().getEnumConstants().stream()
            .map(ResolvedEnumConstantDeclaration::getName)
            .collect(Collectors.toList());
    String qualifiedName = type.getQualifiedName();
    foundTypes.put(qualifiedName, type);
    StringSchema schema = new StringSchema();
    schema.name(qualifiedName);
    schema.setEnum(entries);
    schema.$ref(getFullQualifiedNameRef(qualifiedName));
    return schema;
}
 
Example #23
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean isTypeOf(ResolvedType type, Class... clazz) {
    if (!type.isReferenceType()) {
        return false;
    }
    List<String> classes = Arrays.stream(clazz).map(Class::getName)
            .collect(Collectors.toList());
    return classes.contains(type.asReferenceType().getQualifiedName())
            || type.asReferenceType().getAllAncestors().stream()
                    .map(ResolvedReferenceType::getQualifiedName)
                    .anyMatch(classes::contains);
}
 
Example #24
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean isStringType(ResolvedType type) {
    if (type.isPrimitive()) {
        return type.asPrimitive() == ResolvedPrimitiveType.CHAR;
    } else {
        return isTypeOf(type, String.class, Character.class);
    }
}
 
Example #25
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean isBooleanType(ResolvedType type) {
    if (type.isPrimitive()) {
        return type.asPrimitive() == ResolvedPrimitiveType.BOOLEAN;
    } else {
        return isTypeOf(type, Boolean.class);
    }
}
 
Example #26
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private boolean isNumberType(ResolvedType type) {
    if (type.isPrimitive()) {
        ResolvedPrimitiveType resolvedPrimitiveType = type.asPrimitive();
        return resolvedPrimitiveType != ResolvedPrimitiveType.BOOLEAN
                && resolvedPrimitiveType != ResolvedPrimitiveType.CHAR;
    } else {
        return isTypeOf(type, Number.class);
    }
}
 
Example #27
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private Schema createMapSchema(ResolvedType type) {
    Schema mapSchema = new MapSchema();
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> typeParametersMap = type
            .asReferenceType().getTypeParametersMap();
    if (typeParametersMap.size() == 2) {
        // Assumed that Map always has the first type parameter as `String`
        // and
        // the second is for its value type
        ResolvedType mapValueType = typeParametersMap.get(1).b;
        mapSchema.additionalProperties(
                parseResolvedTypeToSchema(mapValueType));
    }
    return mapSchema;
}
 
Example #28
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
private Schema createCollectionSchema(ResolvedReferenceType type) {
    ArraySchema array = new ArraySchema();
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> typeParametersMap = type
            .getTypeParametersMap();
    if (!typeParametersMap.isEmpty()) {
        ResolvedType collectionParameterType = typeParametersMap.get(0).b;
        array.items(parseResolvedTypeToSchema(collectionParameterType));
    }
    return array;
}
 
Example #29
Source File: SchemaResolver.java    From flow with Apache License 2.0 5 votes vote down vote up
Schema parseResolvedTypeToSchema(ResolvedType resolvedType) {
    if (resolvedType.isArray()) {
        return createArraySchema(resolvedType);
    }
    if (isNumberType(resolvedType)) {
        return new NumberSchema();
    } else if (isStringType(resolvedType)) {
        return new StringSchema();
    } else if (isCollectionType(resolvedType)) {
        return createCollectionSchema(resolvedType.asReferenceType());
    } else if (isBooleanType(resolvedType)) {
        return new BooleanSchema();
    } else if (isMapType(resolvedType)) {
        return createMapSchema(resolvedType);
    } else if (isDateType(resolvedType)) {
        return new DateSchema();
    } else if (isDateTimeType(resolvedType)) {
        return new DateTimeSchema();
    } else if (isOptionalType(resolvedType)) {
        return createOptionalSchema(resolvedType.asReferenceType());
    } else if (isUnhandledJavaType(resolvedType)) {
        return new ObjectSchema();
    } else if (isTypeOf(resolvedType, Enum.class)) {
        return createEnumTypeSchema(resolvedType);
    }
    return createUserBeanSchema(resolvedType);
}
 
Example #30
Source File: ParserTypeUtil.java    From JRemapper with MIT License 5 votes vote down vote up
/**
 * @param type
 *            JavaParser type. Must be a primitive.
 * @return Internal descriptor.
 */
private static String primTypeToDesc(ResolvedType type) {
	String desc = null;
	switch (type.describe()) {
		case "boolean":
			desc = "Z";
			break;
		case "int":
			desc = "I";
			break;
		case "long":
			desc = "J";
			break;
		case "short":
			desc = "S";
			break;
		case "byte":
			desc = "B";
			break;
		case "double":
			desc = "D";
			break;
		case "float":
			desc = "F";
			break;
		case "void":
			desc = "V";
			break;
		default:
			throw new RuntimeException("Unknown primitive type field '" + type.describe() + "'");
	}
	StringBuilder sbDesc = new StringBuilder();
	for (int i = 0; i < type.arrayLevel(); i++)
		sbDesc.append("[");
	sbDesc.append(desc);
	return sbDesc.toString();
}