Example usage for org.eclipse.jdt.core JavaCore createCompilationUnitFrom

List of usage examples for org.eclipse.jdt.core JavaCore createCompilationUnitFrom

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore createCompilationUnitFrom.

Prototype

public static ICompilationUnit createCompilationUnitFrom(IFile file) 

Source Link

Document

Creates and returns a compilation unit element for the given source file (i.e.

Usage

From source file:at.bestsolution.efxclipse.tooling.fxml.compile.FxmlAnnotationCompilationParticipant.java

License:Open Source License

@Override
public void buildStarting(BuildContext[] files, boolean isBatch) {
    super.buildStarting(files, isBatch);
    for (BuildContext context : files) {
        List<CategorizedProblem> problems = null;
        try {//from   w  w w  .j av a2 s . co m
            ICompilationUnit unit = JavaCore.createCompilationUnitFrom(context.getFile());
            problems = checkCU(unit, null);
            context.recordNewProblems(problems.toArray(new CategorizedProblem[problems.size()]));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (problems != null) {
                problems.clear();
            }
        }
    }
}

From source file:at.bestsolution.fxide.jdt.editor.JDTSourceFileInput.java

License:Open Source License

@Inject
public JDTSourceFileInput(IWorkspace workspace, @Named(Constants.DOCUMENT_URL) String url,
        @Optional EventBus eventBus) {
    this.url = url;
    this.eventBus = eventBus;
    String path = url.substring("module-file:".length());
    String project = path.substring(0, path.indexOf('/'));

    IProject p = workspace.getRoot().getProject(project);
    file = p.getFile(new Path(path.substring(project.length() + 1)));

    this.owner = new JDTWorkingCopyOwner(this);
    try {/*from  w  w w  .ja v  a 2s . c  o m*/
        this.compilationUnit = JavaCore.createCompilationUnitFrom(file).getWorkingCopy(owner, null);
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:br.ufal.cideei.util.CachedICompilationUnitParser.java

License:Open Source License

public CompilationUnit parse(IFile aFile) {
    // MISS// w  w w. j  a  v  a2 s. c o  m
    if (!aFile.equals(this.file)) {
        //         System.out.println("CachedICompilationUnitParser: miss");
        /*
         * Lazy initializes the member ASTParser
         */
        if (parser == null) {
            parser = ASTParser.newParser(AST.JLS3);
            parser.setKind(ASTParser.K_COMPILATION_UNIT);
            parser.setResolveBindings(true);
        }

        /*
         * Create a ASTNode (a CompilationUnit) by reusing the parser;
         */
        ICompilationUnit compilationUnit = JavaCore.createCompilationUnitFrom(aFile);
        parser.setSource(compilationUnit);
        CompilationUnit jdtCompilationUnit = (CompilationUnit) parser.createAST(null);

        /*
         * Caches the result
         */
        this.file = aFile;
        return this.cu = jdtCompilationUnit;
    } // HIT
    else {
        return this.cu;
    }
}

From source file:ca.ecliptical.pde.internal.ds.DSAnnotationCompilationParticipant.java

License:Open Source License

@Override
public void processAnnotations(BuildContext[] files) {
    // we need to process CUs in context of a project; separate them by project
    HashMap<IJavaProject, Map<ICompilationUnit, BuildContext>> filesByProject = new HashMap<IJavaProject, Map<ICompilationUnit, BuildContext>>();
    for (BuildContext file : files) {
        ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file.getFile());
        if (cu == null)
            continue;

        Map<ICompilationUnit, BuildContext> map = filesByProject.get(cu.getJavaProject());
        if (map == null) {
            map = new HashMap<ICompilationUnit, BuildContext>();
            filesByProject.put(cu.getJavaProject(), map);
        }//  ww w .j a  v  a 2 s  .co m

        map.put(cu, file);
    }

    // process all CUs in each project
    for (Map.Entry<IJavaProject, Map<ICompilationUnit, BuildContext>> entry : filesByProject.entrySet()) {
        processAnnotations(entry.getKey(), entry.getValue());
    }
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static CompilationUnit getCU(IFile file, PPAOptions options) {
    CompilationUnit cu = null;//ww  w  .j a  va2  s .c  o  m
    try {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        PPATypeRegistry registry = new PPATypeRegistry(
                (JavaProject) JavaCore.create(icu.getUnderlyingResource().getProject()));
        ASTNode node = null;
        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setSource(icu);
        node = parser2.createAST(null);
        PPAEngine ppaEngine = new PPAEngine(registry, options);

        cu = (CompilationUnit) node;

        ppaEngine.addUnitToProcess(cu);
        ppaEngine.doPPA();
        ppaEngine.reset();
    } catch (JavaModelException jme) {
        // Retry with the file version.
        logger.warn("Warning while getting CU from PPA");
        logger.debug("Exception", jme);
        cu = getCU(file.getLocation().toFile(), options);
    } catch (Exception e) {
        logger.error("Error while getting CU from PPA", e);
    }

    return cu;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static CompilationUnit getCUNoPPA(IFile file) {
    CompilationUnit cu = null;/*from  w w  w.  j a  v  a 2  s.co  m*/
    try {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        ASTNode node = null;
        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setSource(icu);
        node = parser2.createAST(null);
        cu = (CompilationUnit) node;
    } catch (Exception e) {
        logger.error("Error while getting CU without PPA", e);
    }

    return cu;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static ASTNode getNode(IFile file, PPAOptions options, int kind) {
    ASTNode node = null;//from w  w w .jav a2  s  . c o m
    try {
        ICompilationUnit icu = JavaCore.createCompilationUnitFrom(file);
        PPATypeRegistry registry = new PPATypeRegistry(
                (JavaProject) JavaCore.create(icu.getUnderlyingResource().getProject()));
        PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
        parser2.setStatementsRecovery(true);
        parser2.setResolveBindings(true);
        parser2.setSource(icu);
        parser2.setKind(kind);
        node = parser2.createAST(null);
        PPAEngine ppaEngine = new PPAEngine(registry, options);

        ppaEngine.addUnitToProcess(node);
        ppaEngine.doPPA();
        ppaEngine.reset();
    } catch (Exception e) {
        logger.error("Error while getting CU from PPA", e);
    }

    return node;
}

From source file:ca.mcgill.sable.soot.launching.SootFileLauncher.java

License:Open Source License

public void handleFiles(Object toProcess) {

    setDoNotContinue(false);//from  w  w  w. ja  va 2s . co m
    if (toProcess instanceof IClassFile) {
        IClassFile cf = (IClassFile) toProcess;
        IPackageFragmentRoot pfr = (IPackageFragmentRoot) cf.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        IPackageFragment pf = (IPackageFragment) cf.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
        if (pfr.getResource() != null) {
            setClasspathAppend(platform_location + pfr.getPath().toOSString());
        } else {
            setClasspathAppend(pfr.getPath().toOSString());
        }
        addJars();
        if (pf.isDefaultPackage()) {
            getToProcessList().add(removeFileExt(cf.getElementName()));
        } else {
            getToProcessList().add(pf.getElementName() + "." + removeFileExt(cf.getElementName()));
        }
    } else if (toProcess instanceof IFile) {
        IFile file = (IFile) toProcess;
        if (file.getFileExtension().compareTo("jimple") == 0) {
            setClasspathAppend(platform_location + file.getParent().getFullPath().toOSString());
            addJars();
            setIsSrcPrec(true);
            setSrcPrec(LaunchCommands.JIMPLE_IN);
            getToProcessList().add(removeFileExt(file.getName()));
        } else if (file.getFileExtension().equals("java")) {
            try {

                handleSourceFile(JavaCore.createCompilationUnitFrom(file));
            } catch (Exception e) {
                System.out.println("problem creating CompilationUnit");
            }

        }

        else if (file.getFileExtension().equals("class")) {
            try {
                handleClassFile(file);

            } catch (Exception e) {
                System.out.println("not a class file");
            }
        }

    } else if (toProcess instanceof ICompilationUnit) {
        ICompilationUnit cu = (ICompilationUnit) toProcess;
        handleSourceFile(cu);

    }
}

From source file:ch.mlutz.plugins.t4e.handlers.AnalyzeHandler.java

License:Open Source License

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    if (event.getCommand().getId().equals(Command.ANALYZE)) {

        // get workbench window
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        // set selection service
        ISelectionService service = window.getSelectionService();
        // set structured selection
        IStructuredSelection structured = (IStructuredSelection) service.getSelection();

        //check if it is an IFile
        Object el = structured.getFirstElement();
        log.info(el.getClass().getName());

        ICompilationUnit compilationUnit = null;

        if (!(el instanceof ICompilationUnit) && el instanceof IFile) {
            IFile file = (IFile) el;// ww w.j  a va 2  s.c om

            compilationUnit = JavaCore.createCompilationUnitFrom((IFile) file);
            final IJavaElement javaElement = JavaCore.create((IFile) file);
            log.info(javaElement.toString());
            IPath path = file.getLocation();
            System.out.println(path.toPortableString());
        }

        if (el instanceof ICompilationUnit) {
            compilationUnit = (ICompilationUnit) el;

            log.info(compilationUnit.toString());
            try {
                log.info(compilationUnit.getTypes().toString());
            } catch (JavaModelException e) {
                log.warn("Getting types from compilation unit failed. ", e);
            }
        }

        /*
        //check if it is an ICompilationUnit
        if (structured.getFirstElement() instanceof ICompilationUnit) {
           ICompilationUnit cu = (ICompilationUnit) structured.getFirstElement();
           System.out.println(cu.getElementName());
        }
        */
    }

    return null;
}

From source file:ch.mlutz.plugins.t4e.serializer.EclipseSerializer.java

License:Open Source License

public static <E extends IJavaElement> E deserializeJavaElement(ObjectInputStream inputStream,
        IWorkspaceRoot workspaceRoot, Class<E> keyClass) throws IOException, ClassNotFoundException {
    IResource resource = deserializeResource(inputStream, workspaceRoot, IResource.class);
    if (resource instanceof IFile) {
        IFile file = (IFile) resource;//from  w  w  w. j  a  va2 s .  c  om
        @SuppressWarnings("unchecked")
        E javaElement = (E) JavaCore.createCompilationUnitFrom(file).getAdapter(keyClass);
        return javaElement;
    }
    return null;
}