com.github.javaparser.utils.Pair Java Examples

The following examples show how to use com.github.javaparser.utils.Pair. 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: 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 #2
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 #3
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 #4
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 #5
Source File: ClassHelper.java    From apigcc with MIT License 5 votes vote down vote up
/**
 * 获取泛型信息
 * @param referenceType
 * @param index 位置信息
 * @return
 */
public static Optional<ResolvedType> getTypeParameter(ResolvedReferenceType referenceType, int index){
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> typeParameters = referenceType.getTypeParametersMap();
    if(typeParameters.size()>index){
        Pair<ResolvedTypeParameterDeclaration, ResolvedType> pair = typeParameters.get(index);
        return Optional.of(pair.b);
    }
    return Optional.empty();
}
 
Example #6
Source File: ClassHelper.java    From apigcc with MIT License 5 votes vote down vote up
/**
 * 获取泛型信息
 * @param referenceType
 * @param a 如 T E 等
 * @return
 */
public static Optional<ResolvedType> getTypeParameter(ResolvedReferenceType referenceType, String a){
    List<Pair<ResolvedTypeParameterDeclaration, ResolvedType>> typeParameters = referenceType.getTypeParametersMap();
    for (Pair<ResolvedTypeParameterDeclaration, ResolvedType> pair : typeParameters) {
        if(Objects.equals(pair.a.getName(),a)){
            return Optional.of(pair.b);
        }
    }
    return Optional.empty();
}
 
Example #7
Source File: ClassHelper.java    From apigcc with MIT License 5 votes vote down vote up
/**
 * 使用父类的泛型 解析当前类
 * @param parent
 * @param field
 */
public static ResolvedType useClassTypeParameter(ResolvedReferenceType parent, ResolvedReferenceType field){
    for (Pair<ResolvedTypeParameterDeclaration, ResolvedType> pair : field.getTypeParametersMap()) {
        if(pair.b.isTypeVariable()){
            Optional<ResolvedType> typeParameter = getTypeParameter(parent, pair.b.asTypeVariable().describe());
            if (typeParameter.isPresent()) {
                return field.replaceTypeVariables(pair.b.asTypeVariable().asTypeParameter(), typeParameter.get());
            }
        }
    }
    return field;
}
 
Example #8
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 #9
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 #10
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 #11
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;
}