Example usage for org.eclipse.jdt.internal.compiler.batch CompilationUnit CompilationUnit

List of usage examples for org.eclipse.jdt.internal.compiler.batch CompilationUnit CompilationUnit

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.batch CompilationUnit CompilationUnit.

Prototype

public CompilationUnit(char[] contents, String fileName, String encoding) 

Source Link

Usage

From source file:ch.uzh.ifi.seal.changedistiller.ast.java.JavaCompilationUtils.java

License:Apache License

private static ICompilationUnit createCompilationUnit(String source, String filename) {
    return new CompilationUnit(source.toCharArray(), filename, null);
}

From source file:ch.uzh.ifi.seal.changedistiller.util.CompilationUtils.java

License:Apache License

private static ICompilationUnit createCompilationunit(String source, String filename) {
    ICompilationUnit cu = new CompilationUnit(source.toCharArray(), filename, null);
    return cu;/*from w w w.j a v  a2s . c  om*/
}

From source file:com.android.build.gradle.tasks.annotations.ExtractAnnotationsDriver.java

License:Apache License

@NonNull
private static Pair<Collection<CompilationUnitDeclaration>, INameEnvironment> parseSources(
        @NonNull List<File> sourcePaths, @NonNull List<String> classpath, @NonNull String encoding,
        long languageLevel) throws IOException {
    List<ICompilationUnit> sourceUnits = Lists.newArrayListWithExpectedSize(100);

    for (File source : gatherJavaSources(sourcePaths)) {
        char[] contents = Util.getFileCharContent(source, encoding);
        ICompilationUnit unit = new CompilationUnit(contents, source.getPath(), encoding);
        sourceUnits.add(unit);/*from  www  . ja  v  a2s.c  om*/
    }

    Map<ICompilationUnit, CompilationUnitDeclaration> outputMap = Maps
            .newHashMapWithExpectedSize(sourceUnits.size());

    CompilerOptions options = EcjParser.createCompilerOptions();
    options.docCommentSupport = true; // So I can find @hide

    // Note: We can *not* set options.ignoreMethodBodies=true because it disables
    // type attribution!

    options.sourceLevel = languageLevel;
    options.complianceLevel = options.sourceLevel;
    // We don't generate code, but just in case the parser consults this flag
    // and makes sure that it's not greater than the source level:
    options.targetJDK = options.sourceLevel;
    options.originalComplianceLevel = options.sourceLevel;
    options.originalSourceLevel = options.sourceLevel;
    options.inlineJsrBytecode = true; // >= 1.5

    INameEnvironment environment = EcjParser.parse(options, sourceUnits, classpath, outputMap, null);
    Collection<CompilationUnitDeclaration> parsedUnits = outputMap.values();
    return Pair.of(parsedUnits, environment);
}

From source file:com.android.tools.lint.EcjParser.java

License:Apache License

@Override
public void prepareJavaParse(@NonNull final List<JavaContext> contexts) {
    if (mProject == null || contexts.isEmpty()) {
        return;/*from   w  ww. j  a va 2  s  .  c om*/
    }

    List<ICompilationUnit> sources = Lists.newArrayListWithExpectedSize(contexts.size());
    mSourceUnits = Maps.newHashMapWithExpectedSize(sources.size());
    for (JavaContext context : contexts) {
        String contents = context.getContents();
        if (contents == null) {
            continue;
        }
        File file = context.file;
        CompilationUnit unit = new CompilationUnit(contents.toCharArray(), file.getPath(), UTF_8);
        sources.add(unit);
        mSourceUnits.put(file, unit);
    }
    List<String> classPath = computeClassPath(contexts);
    mCompiled = Maps.newHashMapWithExpectedSize(mSourceUnits.size());
    try {
        mEnvironment = parse(createCompilerOptions(), sources, classPath, mCompiled, mClient);
    } catch (Throwable t) {
        mClient.log(t, "ECJ compiler crashed");
    }

    if (DEBUG_DUMP_PARSE_ERRORS) {
        for (CompilationUnitDeclaration unit : mCompiled.values()) {
            // so maybe I don't need my map!!
            CategorizedProblem[] problems = unit.compilationResult().getAllProblems();
            if (problems != null) {
                for (IProblem problem : problems) {
                    if (problem == null || !problem.isError()) {
                        continue;
                    }
                    System.out.println(new String(problem.getOriginatingFileName()) + ":"
                            + (problem.isError() ? "Error" : "Warning") + ": " + problem.getSourceLineNumber()
                            + ": " + problem.getMessage());
                }
            }
        }
    }
}

From source file:com.android.tools.lint.EcjParser.java

License:Apache License

@Nullable
private CompilationUnitDeclaration getParsedUnit(@NonNull JavaContext context, @NonNull String code) {
    ICompilationUnit sourceUnit = null;/*from  w  w w .  j  a  v  a2  s .c o m*/
    if (mSourceUnits != null && mCompiled != null) {
        sourceUnit = mSourceUnits.get(context.file);
        if (sourceUnit != null) {
            CompilationUnitDeclaration unit = mCompiled.get(sourceUnit);
            if (unit != null) {
                return unit;
            }
        }
    }

    if (sourceUnit == null) {
        sourceUnit = new CompilationUnit(code.toCharArray(), context.file.getName(), UTF_8);
    }
    try {
        CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
        return getParser().parse(sourceUnit, compilationResult);
    } catch (AbortCompilation e) {
        // No need to report Java parsing errors while running in Eclipse.
        // Eclipse itself will already provide problem markers for these files,
        // so all this achieves is creating "multiple annotations on this line"
        // tooltips instead.
        return null;
    }
}

From source file:de.plugins.eclipse.depclipse.testcommons.Util.java

License:Open Source License

public static CompilationUnit[] compilationUnits(String[] testFiles) {
    int length = testFiles.length / 2;
    CompilationUnit[] result = new CompilationUnit[length];
    int index = 0;
    for (int i = 0; i < length; i++) {
        result[i] = new CompilationUnit(testFiles[index + 1].toCharArray(), testFiles[index], null);
        index += 2;/*w  w  w.  jav  a  2  s. c om*/
    }
    return result;
}

From source file:io.gige.compiler.internal.CompilationTaskImpl.java

License:Apache License

protected void setTargets(Iterable<? extends JavaFileObject> compilationUnits) {
    this.targets = stream(compilationUnits.spliterator(), false)
            .filter(file -> file.getKind() == JavaFileObject.Kind.SOURCE)
            .map(file -> new CompilationUnit(null, file.getName(), null) {
                @Override/*from   w ww. java2s .  co m*/
                public char[] getContents() {
                    try {
                        return file.getCharContent(true).toString().toCharArray();
                    } catch (IOException e) {
                        throw new AbortCompilationUnit(null, e, null);
                    }
                }
            }).toArray(ICompilationUnit[]::new);
}

From source file:io.gige.compiler.internal.HookedJavaFileObject.java

License:Open Source License

protected void closed() {
    if (!_closed) {
        _closed = true;/*from w  ww . j a  va 2 s.c om*/
        // TODO: support encoding
        switch (this.getKind()) {
        case SOURCE:
            CompilationUnit unit = new CompilationUnit(null, _fileName, null /* encoding */);
            _filer.addNewUnit(unit);
            break;
        case CLASS:
            IBinaryType binaryType = null;
            try {
                binaryType = ClassFileReader.read(_fileName);
            } catch (ClassFormatException e) {
                /*
                 * When the annotation processor produces garbage, javac
                 * seems to show some resilience, by hooking the source
                 * type, which since is resolved can answer annotations
                 * during discovery - Not sure if this sanctioned by the
                 * spec, to be taken up with Oracle. Here we mimic the bug,
                 * see that addNewClassFile is simply collecting
                 * ReferenceBinding's, so adding a SourceTypeBinding works
                 * just fine.
                 */
                ReferenceBinding type = this._filer._env.getCompiler().lookupEnvironment
                        .getType(CharOperation.splitOn('.', _typeName.toCharArray()));
                if (type != null)
                    _filer.addNewClassFile(type);
            } catch (IOException e) {
                // ignore
            }
            if (binaryType != null) {
                char[] name = binaryType.getName();
                ReferenceBinding type = this._filer._env.getCompiler().lookupEnvironment
                        .getType(CharOperation.splitOn('/', name));
                if (type != null && type.isValidBinding()) {
                    if (type.isBinaryBinding()) {
                        _filer.addNewClassFile(type);
                    } else {
                        BinaryTypeBinding binaryBinding = new BinaryTypeBinding(type.getPackage(), binaryType,
                                this._filer._env.getCompiler().lookupEnvironment, true);
                        if (binaryBinding != null)
                            _filer.addNewClassFile(binaryBinding);
                    }
                }
            }
            break;
        case HTML:
        case OTHER:
            break;
        }
    }
}

From source file:lombok.ast.grammar.EcjTreeBuilderTest.java

License:Open Source License

@Override
protected ASTNode parseWithTargetCompiler(Source source) {
    CompilerOptions compilerOptions = ecjCompilerOptions();
    Parser parser = new Parser(new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(),
            compilerOptions, new DefaultProblemFactory()), compilerOptions.parseLiteralExpressionsAsConstants);
    parser.javadocParser.checkDocComment = true;
    CompilationUnit sourceUnit = new CompilationUnit(source.getRawInput().toCharArray(), source.getName(),
            "UTF-8");
    CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
    CompilationUnitDeclaration cud = parser.parse(sourceUnit, compilationResult);

    if (cud.hasErrors())
        return null;
    return cud;//from w  ww .j  a v  a 2  s  .c  om
}

From source file:lombok.ast.grammar.EcjTreeConverterType1Test.java

License:Open Source License

protected Node parseWithTargetCompiler(Source source) {
    CompilerOptions compilerOptions = ecjCompilerOptions();
    Parser parser = new Parser(new ProblemReporter(DefaultErrorHandlingPolicies.proceedWithAllProblems(),
            compilerOptions, new DefaultProblemFactory()), compilerOptions.parseLiteralExpressionsAsConstants);
    parser.javadocParser.checkDocComment = true;
    CompilationUnit sourceUnit = new CompilationUnit(source.getRawInput().toCharArray(), source.getName(),
            "UTF-8");
    CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
    CompilationUnitDeclaration cud = parser.parse(sourceUnit, compilationResult);

    if (cud.hasErrors())
        return null;

    EcjTreeConverter converter = new EcjTreeConverter();
    converter.visit(source.getRawInput(), cud);
    return converter.get();
}