Search in sources :

Example 1 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit 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 CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project Gargoyle by callakrsos.

the class FileUtil method consumeJavaParser.

/********************************
	 * 작성일 : 2016. 7. 14. 작성자 : KYJ
	 *
	 * JavaParser
	 *
	 * @param javaFile
	 * @param converter
	 * @param errorHandler
	 ********************************/
public static void consumeJavaParser(File javaFile, Consumer<CompilationUnit> converter, Consumer<Exception> errorHandler) {
    try {
        if (javaFile != null && javaFile.exists()) {
            CompilationUnit cu = GargoyleJavaParser.getCompileUnit(javaFile);
            converter.accept(cu);
        }
    } catch (Exception e) {
        errorHandler.accept(e);
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GagoyleParamEmptyException(com.kyj.fx.voeditor.visual.exceptions.GagoyleParamEmptyException)

Example 3 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project Gargoyle by callakrsos.

the class VoEditorController method setFromFile.

/********************************
	 * 작성일 : 2016. 7. 4. 작성자 : KYJ
	 *
	 *
	 * @param voFile
	 * @throws IOException
	 * @throws FileNotFoundException
	 ********************************/
public void setFromFile(File voFile) {
    if (voFile == null || !voFile.exists())
        return;
    if (voFile.isFile()) {
        CompilationUnit cu;
        try (FileInputStream in = new FileInputStream(voFile)) {
            // parse the file
            cu = JavaParser.parse(in);
            PackageDeclaration packageDeclaration = cu.getPackage();
            txtPackageName.setText(packageDeclaration.getName().toString());
            txtClassName.setText(voFile.getName().substring(0, voFile.getName().indexOf('.')));
            txtLocation.setText(voFile.getAbsolutePath());
            MethodVisitor methodVisitor = new MethodVisitor();
            methodVisitor.visit(cu, null);
            List<TableModelDVO> valideFieldMeta = methodVisitor.getValideFieldMeta(t -> {
                TableModelDVO tableModelDVO = new TableModelDVO();
                tableModelDVO.setName(t.getName());
                tableModelDVO.setType(t.getFieldType().getSimpleName());
                return tableModelDVO;
            });
            this.tbVoEditor.getItems().addAll(valideFieldMeta);
        } catch (IOException | ParseException e) {
            LOGGER.error(ValueUtil.toString(e));
        }
    } else {
        txtLocation.setText(voFile.getAbsolutePath());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) TableModelDVO(kyj.Fx.dao.wizard.core.model.vo.TableModelDVO) IOException(java.io.IOException) ParseException(com.github.javaparser.ParseException) FileInputStream(java.io.FileInputStream) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 4 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project Gargoyle by callakrsos.

the class GargoyleJavaParser method getPackageName.

/********************************
	 * 작성일 : 2016. 7. 14. 작성자 : KYJ
	 *
	 *
	 * @param javaFile
	 * @param converter
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws ParseException
	 ********************************/
public static String getPackageName(File javaFile, FileCheckHandler<String> converter) throws FileNotFoundException, IOException, ParseException {
    String packageName = null;
    if (javaFile == null) {
        packageName = converter.ifNull();
    } else if (!javaFile.exists())
        packageName = converter.notExists();
    else if (javaFile.isFile()) {
        CompilationUnit cu = getCompileUnit(javaFile);
        packageName = getPackageName(cu);
    }
    return packageName;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit)

Example 5 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project Gargoyle by callakrsos.

the class VOEditorParser method parse.

public void parse() throws ParseException {
    // FileInputStream in = new FileInputStream(fileName);
    CompilationUnit cu = JavaParser.parse(is);
    PackageDeclaration package1 = cu.getPackage();
    LOGGER.debug(package1.getName().toString());
    LOGGER.debug(String.format("package name : %s", package1.getName().getName()));
    // prints the resulting compilation unit to default system output
    LOGGER.debug(cu.toString());
    new MethodVisitor().visit(cu, null);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)489 Test (org.junit.Test)304 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)160 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)140 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)128 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)101 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)70 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)66 Context (com.github.javaparser.symbolsolver.core.resolution.Context)62 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)55 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)51 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 File (java.io.File)39 Expression (com.github.javaparser.ast.expr.Expression)38 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 MethodUsage (com.github.javaparser.resolution.MethodUsage)34 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)33 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)29 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)29 ArrayList (java.util.ArrayList)29