Example usage for org.eclipse.jdt.core ICompilationUnit getOwner

List of usage examples for org.eclipse.jdt.core ICompilationUnit getOwner

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ICompilationUnit getOwner.

Prototype

WorkingCopyOwner getOwner();

Source Link

Document

Returns null if this ICompilationUnit is the primary working copy, or this ICompilationUnit is not a working copy, otherwise the WorkingCopyOwner

Usage

From source file:com.google.gwt.eclipse.core.validators.java.JavaCompilationParticipant.java

License:Open Source License

public static JavaValidationResult validateCompilationUnit(ASTNode ast) {
    ICompilationUnit cu = JavaASTUtils.getCompilationUnit(ast);

    // If the compilation unit is not on the build classpath, return an empty
    // list of problems/JSNI Java references.
    if (!cu.getJavaProject().isOnClasspath(cu)) {
        return new JavaValidationResult();
    }//w w  w . j  a v a 2  s . c o m

    // Walk the Java AST to find problems and Java references in JSNI blocks
    JavaValidationVisitor visitor = new JavaValidationVisitor();
    ast.accept(visitor);

    /*
     * Index the Java references in the compilation unit, but only if we're
     * doing a reconcile from the Java Editor or from our direct call from
     * JsniReferenceChange.perform method. There will be no working copy owner
     * in both of these cases. However, during the pre-refactoring checks, the
     * JDT will call reconcile on a working copy with a JavaRenameProcessor as
     * the working copy owner. If we index the Java refs during that reconcile,
     * which is based on a particular JDT refactoring, we may have inaccurate
     * offsets when we go to calculate the edits for our own refactoring.
     */
    if (cu.getOwner() == null) {
        List<JsniJavaRef> jsniJavaRefs = visitor.getValidationResult().getJavaRefs();
        indexJavaRefs(cu, jsniJavaRefs);
    }

    return visitor.getValidationResult();
}

From source file:edu.brown.cs.bubbles.bedrock.BedrockEditor.java

License:Open Source License

/********************************************************************************/

private synchronized FileData findFile(String proj, String file, String bid, String id)
        throws BedrockException {
    FileData fd = file_map.get(file);//from w  w  w  . j ava  2 s.  c o m

    if (fd == null) {
        ICompilationUnit icu = null;
        icu = our_plugin.getProjectManager().getCompilationUnit(proj, file);
        if (icu == null && proj != null) {
            icu = our_plugin.getProjectManager().getCompilationUnit(null, file);
            if (icu != null)
                proj = null;
        }

        if (icu == null)
            return null;
        BedrockPlugin.logD("START FILE " + proj + " " + file + " " + bid + " " + icu.isWorkingCopy() + " "
                + icu.hasResourceChanged() + " " + icu.isOpen() + " " + icu.getOwner() + " "
                + System.identityHashCode(icu) + " " + System.identityHashCode(icu.getPrimary()));

        icu = icu.getPrimary();

        fd = new FileData(proj, file, icu);
        file_map.put(file, fd);
    }

    if (id != null)
        fd.setCurrentId(bid, id);

    return fd;
}

From source file:net.sourceforge.c4jplugin.internal.ui.contracthierarchy.ContractHierarchyLifeCycle.java

License:Open Source License

private void processDelta(IJavaElementDelta delta, ArrayList<IType> changedTypes) {
    IJavaElement element = delta.getElement();
    switch (element.getElementType()) {
    case IJavaElement.TYPE:
        processTypeDelta((IType) element, changedTypes);
        processChildrenDelta(delta, changedTypes); // (inner types)
        break;/*from   w  ww  . ja  v a 2 s .co m*/
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        processChildrenDelta(delta, changedTypes);
        break;
    case IJavaElement.COMPILATION_UNIT:
        ICompilationUnit cu = (ICompilationUnit) element;
        if (cu.getOwner() != null) {
            return;
        }

        if (delta.getKind() == IJavaElementDelta.CHANGED && isPossibleStructuralChange(delta.getFlags())) {
            try {
                if (cu.exists()) {
                    IType[] types = cu.getAllTypes();
                    for (int i = 0; i < types.length; i++) {
                        processTypeDelta(types[i], changedTypes);
                    }
                }
            } catch (JavaModelException e) {
                C4JActivator.log(e);
            }
        } else {
            processChildrenDelta(delta, changedTypes);
        }
        break;
    case IJavaElement.CLASS_FILE:
        if (delta.getKind() == IJavaElementDelta.CHANGED) {
            try {
                IType type = ((IClassFile) element).getType();
                processTypeDelta(type, changedTypes);
            } catch (JavaModelException e) {
                C4JActivator.log(e);
            }
        } else {
            processChildrenDelta(delta, changedTypes);
        }
        break;
    }
}

From source file:org.dyno.visual.swing.base.JavaUtil.java

License:Open Source License

private static void releaseDocument(ICompilationUnit cu, IDocument document, IProgressMonitor monitor)
        throws CoreException {
    if (cu.getOwner() == null) {
        IFile file = (IFile) cu.getResource();
        if (file.exists()) {
            ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
            bufferManager.disconnect(file.getFullPath(), LocationKind.IFILE, monitor);
            return;
        }/*from   w  w w.j  a v a2s  .  c  o  m*/
    }
    cu.getBuffer().setContents(document.get());
    monitor.done();
}

From source file:org.dyno.visual.swing.base.JavaUtil.java

License:Open Source License

private static IDocument aquireDocument(ICompilationUnit cu, IProgressMonitor monitor) throws CoreException {
    if (cu.getOwner() == null) {
        IFile file = (IFile) cu.getResource();
        if (file.exists()) {
            ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
            IPath path = cu.getPath();/*from ww  w  .j  a v  a 2s  . co  m*/
            bufferManager.connect(path, LocationKind.IFILE, monitor);
            return bufferManager.getTextFileBuffer(path, LocationKind.IFILE).getDocument();
        }
    }
    monitor.done();
    return new Document(cu.getSource());
}

From source file:org.dyno.visual.swing.base.JavaUtil.java

License:Open Source License

private static void commitDocument(ICompilationUnit cu, IDocument document, TextEdit edit,
        IProgressMonitor monitor) throws CoreException, MalformedTreeException, BadLocationException {
    if (cu.getOwner() == null) {
        IFile file = (IFile) cu.getResource();
        if (file.exists()) {
            IStatus status = makeCommittable(new IResource[] { file }, null);
            if (!status.isOK()) {
                throw new CoreException(status);
            }//from   w  w  w.  ja v  a2  s. com
            new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits(); // apply
            ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
            bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE).commit(monitor, true);
            return;
        }
    }
    new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits();
}

From source file:org.eclipse.ajdt.internal.ui.refactoring.ITDRenameRefactoringProvider.java

License:Open Source License

/**
 * Creates an AST for the given compilation unit with translated code.
 * The source locations of the created AST have been converted back so that they reflect the source locations
 * of the original, non-translated source.
 *///from ww  w . j  ava2  s .  c  o  m
public CompilationUnit createSourceConvertedAST(String contents, ICompilationUnit unit, boolean resolveBindings,
        boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor monitor) {
    AspectsConvertingParser converter = new AspectsConvertingParser(contents.toCharArray());
    converter.setUnit(unit);
    final ArrayList<Replacement> replacements = converter.convert(ConversionOptions.CODE_COMPLETION);

    ASTParser fParser = ASTParser.newParser(AST.JLS4);
    fParser.setResolveBindings(resolveBindings);
    fParser.setStatementsRecovery(statementsRecovery);
    fParser.setBindingsRecovery(bindingsRecovery);
    fParser.setProject(unit.getJavaProject());
    fParser.setSource(converter.content);
    fParser.setUnitName(unit.getElementName());
    if (unit.getOwner() != null) {
        fParser.setWorkingCopyOwner(unit.getOwner());
    }
    CompilationUnit result = (CompilationUnit) fParser.createAST(monitor);
    result.accept(new ASTVisitor() {
        @Override
        public void preVisit(ASTNode node) {
            int newStart = AspectsConvertingParser.translatePositionToBeforeChanges(node.getStartPosition(),
                    replacements);
            int newEnd = AspectsConvertingParser
                    .translatePositionToBeforeChanges(node.getStartPosition() + node.getLength(), replacements);
            node.setSourceRange(newStart, newEnd - newStart);
        }
    });
    return result;
}

From source file:org.eclipse.che.jdt.quickfix.QuickFixTest.java

License:Open Source License

protected static final ArrayList collectCorrections2(ICompilationUnit cu, int nProblems) throws CoreException {

    final ArrayList problemsList = new ArrayList();
    final IProblemRequestor requestor = new IProblemRequestor() {
        public void acceptProblem(IProblem problem) {
            problemsList.add(problem);//from  w w w  . j a  v  a2s .  co m
        }

        public void beginReporting() {
            problemsList.clear();
        }

        public void endReporting() {
        }

        public boolean isActive() {
            return true;
        }
    };

    WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() {
        public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
            return requestor;
        }
    };
    ICompilationUnit wc = cu.getWorkingCopy(workingCopyOwner, null);
    try {
        wc.reconcile(ICompilationUnit.NO_AST, true, true, wc.getOwner(), null);
    } finally {
        wc.discardWorkingCopy();
    }

    IProblem[] problems = (IProblem[]) problemsList.toArray(new IProblem[problemsList.size()]);
    assertNumberOfProblems(nProblems, problems);

    return collectCorrections(cu, problems[0], null);
}

From source file:org.eclipse.che.jdt.util.JavaModelUtil.java

License:Open Source License

/**
 * Returns true if a cu is a primary cu (original or shared working copy)
 * @param cu the compilation unit//w w w .  j  a  v  a2  s. co m
 * @return return <code>true</code> if the CU is primary
 */
public static boolean isPrimary(ICompilationUnit cu) {
    return cu.getOwner() == null;
}

From source file:org.eclipse.objectteams.otdt.tests.otmodel.OTReconcilerTests.java

License:Open Source License

@SuppressWarnings("unchecked") // options in a raw map
public void testLocalInRoFi() throws CoreException, InterruptedException {
    try {//from  w  w  w. j  a  v  a 2  s. com
        // Resources creation
        IJavaProject p = createOTJavaProject("P", new String[] { "" }, new String[] { "JCL15_LIB" }, "bin");
        // set compliance to 1.6 to force generated of a stack map attribute.
        Map options = p.getOptions(true);
        options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
        options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
        options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
        p.setOptions(options);
        IProject project = p.getProject();
        IProjectDescription prjDesc = project.getDescription();
        prjDesc.setNatureIds(OTDTPlugin.createProjectNatures(prjDesc));
        prjDesc.setBuildSpec(OTDTPlugin.createProjectBuildCommands(prjDesc));
        project.setDescription(prjDesc, null);

        OTREContainer.initializeOTJProject(project);

        this.createFile("/P/Plain.java", "public class Plain {\n" + "    public Object getVal() {\n"
                + "        return \"Plain\";\n" + "    }\n" + "}\n");
        String sourceTeam = "public team class Foo {\n" + "    String foo, blub, bar, dings, wurgs, zork;\n"
                + "    public static void main(String[] args) {\n" + "        new Foo().new Role().test();\n"
                + "    }\n" + "}\n";
        this.createFile("/P/Foo.java", sourceTeam);
        this.createFolder("/P/Foo");

        String sourceRole1 = "team package Foo;\n" + "@SuppressWarnings(\"unused\")\n"
                + "public class Role1 {\n" + "   String s1, s2, s3, s4, s5, s6;\n" + "}\n";
        this.createFile("/P/Foo/Role1.java", sourceRole1);

        String sourceRole2 = "team package Foo;\n" + "@SuppressWarnings(\"unused\")\n"
                + "public class Role2 extends Role1 {\n" + "        protected void test() {\n"
                + "            Plain isub = new Plain() {\n" + "                public Object getVal() {\n"
                + "                    Object edits = super.getVal();\n"
                + "                    if (edits instanceof String) {\n"
                + "                        String string = (String)edits;\n"
                + "                        int l = string.length();\n" + "                    }\n"
                + "                    return edits;\n" + "                }\n" + "            };\n"
                + "            Object v = isub.getVal();\n" + "        }\n" + "}\n";
        this.createFile("/P/Foo/Role2.java", sourceRole2);

        char[] sourceChars = sourceRole2.toCharArray();
        this.problemRequestor.initialize(sourceChars);

        ICompilationUnit wc = getCompilationUnit("/P/Foo/Role2.java").getWorkingCopy(this.wcOwner, null);
        wc.reconcile(AST.JLS3, ICompilationUnit.FORCE_PROBLEM_DETECTION
                | ICompilationUnit.ENABLE_STATEMENTS_RECOVERY | ICompilationUnit.ENABLE_BINDINGS_RECOVERY,
                wc.getOwner(), null);

        assertProblems("Unexpected problems",
                "----------\n" + "----------\n" + "----------\n" + "----------\n");
    } finally {
        deleteProject("P");
    }
}