Example usage for org.eclipse.jdt.internal.core CompilationUnit getSource

List of usage examples for org.eclipse.jdt.internal.core CompilationUnit getSource

Introduction

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

Prototype

@Override
public String getSource() throws JavaModelException 

Source Link

Usage

From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java

License:Open Source License

private static String handleException(CompilationUnit unitElement, ITDAwareNameEnvironment environment,
        RuntimeException e) throws JavaModelException {
    AJLog.log("Exception occurred during problem detection:");
    AJLog.log(e.getClass().getName() + ": " + e.getMessage());
    StackTraceElement[] trace = e.getStackTrace();
    for (int i = 0; i < trace.length; i++) {
        AJLog.log(trace[i].toString());//from   ww  w  .j  av  a2 s. c om
    }
    Throwable cause = e.getCause();
    if (cause != null && cause != e) {
        AJLog.log("Caused by:");
        AJLog.log(cause.getClass().getName() + ": " + cause.getMessage());
        trace = cause.getStackTrace();
        for (int i = 0; i < trace.length; i++) {
            AJLog.log(trace[i].toString());
        }
    }

    String lineDelimiter = org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR;
    StringBuffer message = new StringBuffer("All Source code being worked on:"); //$NON-NLS-1$ 
    message.append(lineDelimiter);
    message.append("----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
    message.append(lineDelimiter);
    if (unitElement instanceof AJCompilationUnit) {
        ((AJCompilationUnit) unitElement).requestOriginalContentMode();
    }
    message.append(unitElement.getSource());
    if (unitElement instanceof AJCompilationUnit) {
        ((AJCompilationUnit) unitElement).discardOriginalContentMode();
    }
    message.append(lineDelimiter);
    message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$

    message.append("----------------------------------- WORKING COPIES -------------------------------------"); //$NON-NLS-1$
    if (environment != null) {
        ICompilationUnit[] workingCopies = environment.getWorkingCopies();
        if (workingCopies != null) {
            for (int i = 0; i < workingCopies.length; i++) {
                message.append(
                        "----------------------------------- WORKING COPY SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
                message.append(lineDelimiter);
                if (workingCopies[i] instanceof AJCompilationUnit) {
                    ((AJCompilationUnit) workingCopies[i]).requestOriginalContentMode();
                }
                message.append(workingCopies[i].getSource());
                if (workingCopies[i] instanceof AJCompilationUnit) {
                    ((AJCompilationUnit) workingCopies[i]).discardOriginalContentMode();
                }
                message.append(lineDelimiter);
                message.append(
                        "----------------------------------- WORKING COPY SOURCE END -------------------------------------"); //$NON-NLS-1$
            }
        }
    } else {
        message.append("none");
    }
    message.append(
            "----------------------------------- WORKING COPIES END -------------------------------------"); //$NON-NLS-1$

    AJLog.log(message.toString());
    return message.toString();
}

From source file:org.eclipse.ajdt.ui.tests.editor.contentassist.ContentAssistTests.java

License:Open Source License

private ICompletionProposal[] getCompletionProposals(IFile file, String marker) throws JavaModelException {
    AspectJEditor editor = (AspectJEditor) openFileInAspectJEditor(file, false);
    AJCompletionProcessor proc = new AJCompletionProcessor(editor, getContentAssistant(editor),
            IDocument.DEFAULT_CONTENT_TYPE);
    AJCompilationUnit unit = AJCompilationUnitManager.INSTANCE.getAJCompilationUnit(file);
    String content;/*from   ww  w . jav  a2s.co m*/
    if (unit != null) {
        unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
        unit.requestOriginalContentMode();
        content = unit.getSource();
        unit.discardOriginalContentMode();
    } else {
        CompilationUnit javaunit = (CompilationUnit) JavaCore.create(file);
        content = javaunit.getSource();
    }
    int offset = content.indexOf(marker);
    return proc.computeCompletionProposals(editor.getViewer(), offset);
}

From source file:org.eclipse.contribution.weaving.jdt.tests.itdawareness.ITDAwarenessTests.java

License:Open Source License

private ICompletionProposal[] getCompletionProposals(IFile file, String marker) throws Exception {
    JavaEditor editor = (JavaEditor) EditorUtility.openInEditor(file);
    JavaCompletionProcessor proc = new JavaCompletionProcessor(editor, getContentAssistant(editor),
            IDocument.DEFAULT_CONTENT_TYPE);
    CompilationUnit javaunit = (CompilationUnit) JavaCore.create(file);
    String content = javaunit.getSource();
    int offset = content.indexOf(marker);
    return proc.computeCompletionProposals(editor.getViewer(), offset);
}

From source file:org.eclipse.jdt.internal.core.CompilationUnitProblemFinder.java

License:Open Source License

public static CompilationUnitDeclaration process(CompilationUnit unitElement, SourceElementParser parser,
        WorkingCopyOwner workingCopyOwner, HashMap problems, boolean creatingAST, int reconcileFlags,
        IProgressMonitor monitor) throws JavaModelException {

    JavaProject project = (JavaProject) unitElement.getJavaProject();
    CancelableNameEnvironment environment = null;
    CancelableProblemFactory problemFactory = null;
    CompilationUnitProblemFinder problemFinder = null;
    CompilationUnitDeclaration unit = null;
    try {//w  w w.j  a v a2 s .c o m
        environment = new CancelableNameEnvironment(project, workingCopyOwner, monitor);
        problemFactory = new CancelableProblemFactory(monitor);
        CompilerOptions compilerOptions = getCompilerOptions(project.getOptions(true), creatingAST,
                ((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0));
        boolean ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
        compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
        // GROOVY start
        // options fetched prior to building problem finder then configured based on project
        CompilerUtils.configureOptionsBasedOnNature(compilerOptions, project);
        // GROOVY end
        problemFinder = new CompilationUnitProblemFinder(environment, getHandlingPolicy(), compilerOptions,
                getRequestor(), problemFactory);
        boolean analyzeAndGenerateCode = true;
        if (ignoreMethodBodies) {
            analyzeAndGenerateCode = false;
        }
        try {
            if (parser != null) {
                problemFinder.parser = parser;
                unit = parser.parseCompilationUnit(unitElement, true/*full parse*/, monitor);
                problemFinder.resolve(unit, unitElement, true, // verify methods
                        analyzeAndGenerateCode, // analyze code
                        analyzeAndGenerateCode); // generate code
            } else {
                unit = problemFinder.resolve(unitElement, true, // verify methods
                        analyzeAndGenerateCode, // analyze code
                        analyzeAndGenerateCode); // generate code
            }
        } catch (AbortCompilation e) {
            problemFinder.handleInternalException(e, unit);
        }
        if (unit != null) {
            CompilationResult unitResult = unit.compilationResult;
            CategorizedProblem[] unitProblems = unitResult.getProblems();
            int length = unitProblems == null ? 0 : unitProblems.length;
            if (length > 0) {
                CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
                System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
                problems.put(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, categorizedProblems);
            }
            unitProblems = unitResult.getTasks();
            length = unitProblems == null ? 0 : unitProblems.length;
            if (length > 0) {
                CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
                System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
                problems.put(IJavaModelMarker.TASK_MARKER, categorizedProblems);
            }
            if (NameLookup.VERBOSE) {
                System.out.println(
                        Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " //$NON-NLS-1$
                                + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$
                System.out.println(
                        Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " //$NON-NLS-1$
                                + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$
            }
        }
    } catch (OperationCanceledException e) {
        // catch this exception so as to not enter the catch(RuntimeException e) below
        throw e;
    } catch (RuntimeException e) {
        // avoid breaking other tools due to internal compiler failure (40334)
        String lineDelimiter = unitElement.findRecommendedLineSeparator();
        StringBuffer message = new StringBuffer("Exception occurred during problem detection:"); //$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(
                "----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(unitElement.getSource());
        message.append(lineDelimiter);
        message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$
        Util.log(e, message.toString());
        throw new JavaModelException(e, IJavaModelStatusConstants.COMPILER_FAILURE);
    } finally {
        if (environment != null)
            environment.setMonitor(null); // don't hold a reference to this external object
        if (problemFactory != null)
            problemFactory.monitor = null; // don't hold a reference to this external object
        // NB: unit.cleanUp() is done by caller
        if (problemFinder != null && !creatingAST)
            problemFinder.lookupEnvironment.reset();
    }
    return unit;
}