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

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

Introduction

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

Prototype

@Override
    public boolean isAnonymous() throws JavaModelException 

Source Link

Usage

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 ww  w  .j  av a2s  . co  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;
}