Example usage for org.eclipse.jdt.internal.core BinaryType getPackageFragment

List of usage examples for org.eclipse.jdt.internal.core BinaryType getPackageFragment

Introduction

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

Prototype

@Override
    public IPackageFragment getPackageFragment() 

Source Link

Usage

From source file:de.instantouch.model.search.SnakeClassInfo.java

License:Open Source License

public void createFrom(BinaryType binaryType) throws SnakeWrongTypeException {
    String className = binaryType.getFullyQualifiedName();
    findByName("className").set(className);

    ITypeRoot typeRoot = binaryType.getTypeRoot();

    IPackageFragment packageFragment = binaryType.getPackageFragment();
    IJavaElement parent = packageFragment.getParent();
    if (parent instanceof ExternalPackageFragmentRoot) {

        ExternalPackageFragmentRoot root = (ExternalPackageFragmentRoot) parent;
        String path = root.getPath().toString();

        int end = path.lastIndexOf("/");
        int start = path.lastIndexOf("/", end - 1);

        String bundleName = path.substring(++start, end);
        findByName("bundleName").set(bundleName);
    } else if (parent instanceof JarPackageFragmentRoot) {

        JarPackageFragmentRoot root = (JarPackageFragmentRoot) parent;
        String path = root.getPath().toString();

        int start = path.lastIndexOf("plugins/");
        String bundle = path.substring(start + "plugins/".length());
        int end = bundle.indexOf("_");

        String bundleName = bundle.substring(0, end);
        findByName("bundleName").set(bundleName);
    }//from www . j av a 2 s .c  om
}

From source file:edu.uci.ics.sourcerer.extractor.ast.FeatureExtractor.java

License:Open Source License

public ClassExtractionReport extractClassFiles(Collection<IClassFile> classFiles, boolean force) {
    ClassExtractionReport report = new ClassExtractionReport();
    ClassFileExtractor extractor = new ClassFileExtractor(bundle);
    ReferenceExtractorVisitor visitor = new ReferenceExtractorVisitor(bundle);
    IMissingTypeWriter missingTypeWriter = bundle.getMissingTypeWriter();

    for (IClassFile classFile : classFiles) {
        try {//from  w  ww.j  a  v  a 2  s  . c o m
            if (ClassFileExtractor.isTopLevelOrAnonymous(classFile)) {
                ISourceRange source = classFile.getSourceRange();

                boolean hasSource = true;
                if (source == null || source.getLength() == 0) {
                    source = classFile.getSourceRange();
                    if (source == null || source.getLength() == 0) {
                        hasSource = false;
                    }
                }

                if (hasSource) {
                    // Verify that the source file matches the binary file
                    BinaryType type = (BinaryType) classFile.getType();
                    if (type.isAnonymous()) {
                        continue;
                    }
                    String sourceFile = type.getPackageFragment().getElementName() + "."
                            + type.getSourceFileName(null);
                    String fqn = classFile.getType().getFullyQualifiedName() + ".java";
                    if (!fqn.equals(sourceFile)) {
                        //              logger.log(Level.WARNING, "Source fqn mismatch: " + sourceFile + " " + fqn);
                        continue;
                    }
                }

                if (!hasSource || Extractor.EXTRACT_BINARY.getValue()) {
                    extractor.extractClassFile(classFile);
                    report.reportBinaryExtraction();
                    if (hasSource) {
                        report.reportSourceSkipped();
                    }
                } else {
                    try {
                        parser.setStatementsRecovery(true);
                        parser.setResolveBindings(true);
                        parser.setBindingsRecovery(true);
                        parser.setSource(classFile);

                        CompilationUnit unit = (CompilationUnit) parser.createAST(null);
                        boolean foundProblem = false;
                        // start by checking for a "public type" error
                        // just skip this unit in if one is found 
                        for (IProblem problem : unit.getProblems()) {
                            if (problem.isError() && problem.getID() == IProblem.PublicClassMustMatchFileName) {
                                foundProblem = true;
                            }
                        }
                        if (foundProblem) {
                            continue;
                        }

                        boolean secondOrder = checkForMissingTypes(unit, report, missingTypeWriter);
                        if (force || !report.hadMissingType()) {
                            if (secondOrder) {
                                visitor.setBindingFreeMode(true);
                            }
                            try {
                                unit.accept(visitor);
                                report.reportSourceExtraction();
                            } catch (Exception e) {
                                logger.log(Level.SEVERE, "Error in extracting " + classFile.getElementName(),
                                        e);
                                for (IProblem problem : unit.getProblems()) {
                                    if (problem.isError()) {
                                        logger.log(Level.SEVERE, "Error in source for class file ("
                                                + classFile.getElementName() + "): " + problem.getMessage());
                                    }
                                }
                                report.reportSourceExtractionException();
                                try {
                                    extractor.extractClassFile(classFile);
                                    report.reportBinaryExtraction();
                                } catch (Exception e2) {
                                    logger.log(Level.SEVERE, "Unable to extract " + classFile.getElementName(),
                                            e2);
                                    report.reportBinaryExtractionException();
                                }
                            }
                            visitor.setBindingFreeMode(false);
                        }
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Error in extracting " + classFile.getElementName(), e);
                        extractor.extractClassFile(classFile);
                        report.reportBinaryExtraction();
                    }
                }
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Unable to extract " + classFile.getElementName(), e);
            report.reportBinaryExtractionException();
        }
    }
    return report;
}