Package com.github.antlrjavaparser.api

Examples of com.github.antlrjavaparser.api.CompilationUnit


            sb.append("\n");
            sb.append("  }\n");
            sb.append("}\n");
            final ByteArrayInputStream bais = new ByteArrayInputStream(sb
                    .toString().getBytes());
            CompilationUnit ci;
            try {
                ci = JavaParser.parse(bais);
            }
            catch (final IOException e) {
                throw new IllegalStateException(
                        "Illegal state: Unable to parse input stream", e);
            }
            catch (final ParseException pe) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not parse correctly", pe);
            }
            final List<TypeDeclaration> types = ci.getTypes();
            if (types == null || types.size() != 1) {
                throw new IllegalArgumentException("Method body invalid");
            }
            final TypeDeclaration td = types.get(0);
            final List<BodyDeclaration> bodyDeclarations = td.getMembers();
View Full Code Here


                    + field.getFieldName() + " = "
                    + field.getFieldInitializer() + ";\n");
            sb.append("}\n");
            final ByteArrayInputStream bais = new ByteArrayInputStream(sb
                    .toString().getBytes());
            CompilationUnit ci;
            try {
                ci = JavaParser.parse(bais);
            }
            catch (final IOException e) {
                throw new IllegalStateException(
                        "Illegal state: Unable to parse input stream", e);
            }
            catch (final ParseException pe) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not parse correctly", pe);
            }
            final List<TypeDeclaration> types = ci.getTypes();
            if (types == null || types.size() != 1) {
                throw new IllegalArgumentException("Field member invalid");
            }
            final TypeDeclaration td = types.get(0);
            final List<BodyDeclaration> bodyDeclarations = td.getMembers();
View Full Code Here

            sb.append("\n");
            sb.append("  }\n");
            sb.append("}\n");
            final ByteArrayInputStream bais = new ByteArrayInputStream(sb
                    .toString().getBytes());
            CompilationUnit ci;
            try {
                ci = JavaParser.parse(bais);
            }
            catch (final IOException e) {
                throw new IllegalStateException(
                        "Illegal state: Unable to parse input stream", e);
            }
            catch (final ParseException pe) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not parse correctly", pe);
            }
            final List<TypeDeclaration> types = ci.getTypes();
            if (types == null || types.size() != 1) {
                throw new IllegalArgumentException("Method body invalid");
            }
            final TypeDeclaration td = types.get(0);
            final List<BodyDeclaration> bodyDeclarations = td.getMembers();
View Full Code Here

    @Override
    public final String getCompilationUnitContents(
            final ClassOrInterfaceTypeDetails cid) {
        Validate.notNull(cid, "Class or interface type details are required");
        // Create a compilation unit to store the type to be created
        final CompilationUnit compilationUnit = new CompilationUnit();

        // NB: this import list is replaced at the end of this method by a
        // sorted version
        compilationUnit.setImports(new ArrayList<ImportDeclaration>());

        if (!cid.getName().isDefaultPackage()) {
            compilationUnit.setPackage(new PackageDeclaration(ASTHelper
                    .createNameExpr(cid.getName().getPackage()
                            .getFullyQualifiedPackageName())));
        }

        // Add the class of interface declaration to the compilation unit
        final List<TypeDeclaration> types = new ArrayList<TypeDeclaration>();
        compilationUnit.setTypes(types);

        updateOutput(compilationUnit, null, cid, null);

        return compilationUnit.toString();
    }
View Full Code Here

        Validate.notBlank(declaredByMetadataId,
                "Declaring metadata ID required");
        Validate.notNull(typeName, "Java type to locate required");
        try {
            final CompilationUnit compilationUnit = JavaParser
                    .parse(new ByteArrayInputStream(fileContents.getBytes()));
            final TypeDeclaration typeDeclaration = JavaParserUtils
                    .locateTypeDeclaration(compilationUnit, typeName);
            if (typeDeclaration == null) {
                return null;
View Full Code Here

        catch (final IOException ignored) {
        }
        if (StringUtils.isBlank(fileContents)) {
            return getCompilationUnitContents(cid);
        }
        CompilationUnit compilationUnit;
        try {
            compilationUnit = JavaParser.parse(new ByteArrayInputStream(
                    fileContents.getBytes()));

        }
        catch (final IOException e) {
            throw new IllegalStateException(e);
        }
        catch (final ParseException e) {
            throw new IllegalStateException(e);
        }

        // Load new compilation unit from cid information
        final String cidContents = getCompilationUnitContents(cid);
        CompilationUnit cidCompilationUnit;
        try {
            cidCompilationUnit = JavaParser.parse(new ByteArrayInputStream(
                    cidContents.getBytes()));

        }
        catch (final IOException e) {
            throw new IllegalStateException(e);
        }
        catch (final ParseException e) {
            throw new IllegalStateException(e);
        }

        // Update package
        if (!compilationUnit.getPackage().getName().getName()
                .equals(cidCompilationUnit.getPackage().getName().getName())) {
            compilationUnit.setPackage(cidCompilationUnit.getPackage());
        }

        // Update imports
        UpdateCompilationUnitUtils.updateCompilationUnitImports(
                compilationUnit, cidCompilationUnit);
View Full Code Here

            catch (final IOException ignored) {
            }
            if (StringUtils.isBlank(typeContents)) {
                return null;
            }
            final CompilationUnit compilationUnit = JavaParser
                    .parse(new ByteArrayInputStream(typeContents.getBytes()));
            final String typeName = fileIdentifier.substring(
                    fileIdentifier.lastIndexOf(File.separator) + 1,
                    fileIdentifier.lastIndexOf("."));
            for (final TypeDeclaration typeDeclaration : compilationUnit
                    .getTypes()) {
                if (typeName.equals(typeDeclaration.getName())) {
                    return new JavaType(compilationUnit.getPackage().getName()
                            .getName()
                            + "." + typeDeclaration.getName());
                }
            }
            return null;
View Full Code Here

            catch (final IOException ignored) {
            }
            if (StringUtils.isBlank(typeContents)) {
                return null;
            }
            final CompilationUnit compilationUnit = JavaParser
                    .parse(new ByteArrayInputStream(typeContents.getBytes()));
            if (compilationUnit == null || compilationUnit.getPackage() == null) {
                return null;
            }
            return new JavaPackage(compilationUnit.getPackage().getName()
                    .toString());
        }
        catch (final IOException e) {
            throw new IllegalStateException(e);
        }
View Full Code Here

TOP

Related Classes of com.github.antlrjavaparser.api.CompilationUnit

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.