Search in sources :

Example 1 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project cas by apereo.

the class ConfigurationMetadataGenerator method parseCompilationUnit.

@SneakyThrows
private void parseCompilationUnit(final Set<ConfigurationMetadataProperty> collectedProps, final Set<ConfigurationMetadataProperty> collectedGroups, final ConfigurationMetadataProperty p, final String typePath, final String typeName, final boolean indexNameWithBrackets) {
    try (InputStream is = new FileInputStream(typePath)) {
        final CompilationUnit cu = JavaParser.parse(is);
        new FieldVisitor(collectedProps, collectedGroups, indexNameWithBrackets, typeName).visit(cu, p);
        if (cu.getTypes().size() > 0) {
            final ClassOrInterfaceDeclaration decl = ClassOrInterfaceDeclaration.class.cast(cu.getType(0));
            for (int i = 0; i < decl.getExtendedTypes().size(); i++) {
                final ClassOrInterfaceType parentType = decl.getExtendedTypes().get(i);
                final Class parentClazz = locatePropertiesClassForType(parentType);
                final String parentTypePath = buildTypeSourcePath(parentClazz.getName());
                parseCompilationUnit(collectedProps, collectedGroups, p, parentTypePath, parentClazz.getName(), indexNameWithBrackets);
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FileInputStream(java.io.FileInputStream) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) SneakyThrows(lombok.SneakyThrows)

Example 2 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.

the class NodeWithImplements method addImplements.

/**
 * Add an implements to this
 *
 * @param name the name of the type to extends from
 * @return this
 */
@SuppressWarnings("unchecked")
public default T addImplements(String name) {
    ClassOrInterfaceType classOrInterfaceType = new ClassOrInterfaceType(name);
    getImplements().add(classOrInterfaceType);
    classOrInterfaceType.setParentNode((Node) this);
    return (T) this;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 3 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.

the class NodeWithExtends method addExtends.

/**
 * Add an extends to this
 *
 * @param name the name of the type to extends from
 * @return this
 */
@SuppressWarnings("unchecked")
public default T addExtends(String name) {
    ClassOrInterfaceType classOrInterfaceType = new ClassOrInterfaceType(name);
    getExtends().add(classOrInterfaceType);
    classOrInterfaceType.setParentNode((Node) this);
    return (T) this;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 4 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.

the class DefaultPackageTest method aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageImportingIt.

@Test(expected = UnsolvedSymbolException.class)
public void aClassInDefaultPackageCanBeAccessedFromOutsideTheDefaultPackageImportingIt() {
    String code = "package myPackage; import B; class A extends B {}";
    MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
    memoryTypeSolver.addDeclaration("B", new MyClassDeclaration("B"));
    ClassOrInterfaceType jpType = JavaParser.parse(code).getClassByName("A").get().getExtendedTypes(0);
    ResolvedType resolvedType = JavaParserFacade.get(memoryTypeSolver).convertToUsage(jpType);
    assertEquals("B", resolvedType.asReferenceType().getQualifiedName());
}
Also used : MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 5 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.

the class DefaultPackageTest method aClassInDefaultPackageCanBeAccessedFromTheDefaultPackage.

@Test
public void aClassInDefaultPackageCanBeAccessedFromTheDefaultPackage() {
    String code = "class A extends B {}";
    MemoryTypeSolver memoryTypeSolver = new MemoryTypeSolver();
    memoryTypeSolver.addDeclaration("B", new MyClassDeclaration("B"));
    ClassOrInterfaceType jpType = JavaParser.parse(code).getClassByName("A").get().getExtendedTypes(0);
    ResolvedType resolvedType = JavaParserFacade.get(memoryTypeSolver).convertToUsage(jpType);
    assertEquals("B", resolvedType.asReferenceType().getQualifiedName());
}
Also used : MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Aggregations

ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)76 Expression (com.github.javaparser.ast.expr.Expression)33 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)29 Type (com.github.javaparser.ast.type.Type)24 NameExpr (com.github.javaparser.ast.expr.NameExpr)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)21 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)21 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)17 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)17 NodeList (com.github.javaparser.ast.NodeList)14 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)13 TypeParameter (com.github.javaparser.ast.type.TypeParameter)13 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)12 Test (org.junit.Test)11 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)9 List (java.util.List)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 HashMap (java.util.HashMap)7