/** * Function for traversing the source files in the application under analysis * * @param astC * @return */ public TreeSet analyzeLibraryCode(ASTCrawler astC) { IJavaProject libProject = RepositoryAnalyzer.getInstance().getCurrentJProject(); if (libProject == null) { logger.warn("No library project is available as input. Nothing can be done, Sorry!!!!"); return null; } try { IPackageFragment[] fragments = libProject.getPackageFragments(); for (int j = 0; j < fragments.length; j++) { switch (fragments[j].getKind()) { case IPackageFragmentRoot.K_SOURCE: /** * @todo I'm not sure whether K_SOURCE actually means non-Archive (and therefore further * testing is obsolete) */ IPackageFragmentRoot root = (IPackageFragmentRoot) fragments[j].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); if (!root.isArchive()) { ICompilationUnit[] units = fragments[j].getCompilationUnits(); for (ICompilationUnit icu : units) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setProject(libProject); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setSource(icu); CompilationUnit cu_java = (CompilationUnit) parser.createAST(null); try { // This also clears the class specific data of the previous run String path = icu.getPath().toString(); int indexOfLastSlash; if ((indexOfLastSlash = path.lastIndexOf("/")) != -1) { path = path.substring(indexOfLastSlash + 1, path.length()); } astC.preProcessClass(cu_java, path); MethodInvocationHolder.MIHKEYGEN = 0; cu_java.accept(astC); } catch (Exception ex) { ex.printStackTrace(); } } } break; default: break; } } } catch (Exception ex) { ex.printStackTrace(); } return astC.postProcess(); }
/** Function for invoking ASTCrawler on each file in the list stored in a directory */ public TreeSet invokeASTCrawler( String directoryPath, List filesList, String sourceObject, String destinationObject, boolean bReUseOldInstance) { // ASTCrawler class if (!bReUseOldInstance || astC == null) astC = new ASTCrawler(directoryPath); astC.sourceObj = sourceObject; astC.destObj = destinationObject; if (CommonConstants.OPERATION_MODE == CommonConstants.DETECT_BUGS_IN_LIBRARY) { return analyzeLibraryCode(astC); } int fileLimitCounter = 0; // A maximum of 1000 files are analyzed for each kind of object // to get some early results for improving the tool for (Iterator iter = filesList.iterator(); iter.hasNext(); ) { try { // Incrementing static counter : RepositoryAnalyzer.numAnalyzedCodeSamples++; String inputFileName = (String) iter.next(); // AnamolyDetector // If the combination of package name and file name is same and // is already visitied, don't visit again to reduce duplicate // patterns, as duplicate samples can be downloaded from GCSE int indexOfUnderscore = 0; if ((indexOfUnderscore = inputFileName.indexOf("_")) != -1) { RepositoryAnalyzer raObj = RepositoryAnalyzer.getInstance(); String searchStr = sourceObject.replace(".", "_") + inputFileName; String file_packagename = inputFileName.substring(indexOfUnderscore + 1, inputFileName.length()) + "#" + raObj.getCodeSampleIDToPackageMapper().get(searchStr); if (raObj.getVisitedCodeSamples().contains(file_packagename)) { continue; } else { raObj.getVisitedCodeSamples().add(file_packagename); } } File inputFile = new File(directoryPath + CommonConstants.FILE_SEP + inputFileName); long flength = inputFile.length(); // Ignore parsing files more than 50KB. Needs more heuristics later to deal here. if (flength > 50000) { continue; } logger.warn("Analyzing " + inputFile.getName()); char[] source = new char[(int) inputFile.length()]; FileReader fileReader = new FileReader(inputFile); fileReader.read(source); fileReader.close(); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setSource(source); parser.setProject(RepositoryAnalyzer.getInstance().getLibProject()); CompilationUnit root = (CompilationUnit) parser.createAST(null); if ((root.getFlags() & CompilationUnit.MALFORMED) != 0) { logger.error("Error occurred while parsing the file: " + inputFile.getName()); continue; } try { // This also clears the class specific data of the previous run astC.preProcessClass(root, inputFile.getName()); MethodInvocationHolder.MIHKEYGEN = 0; root.accept(astC); } catch (Exception ex) { ex.printStackTrace(); } fileLimitCounter++; if (fileLimitCounter > CommonConstants.LIMIT_NUM_FILES) { break; } } catch (Exception ex) { logger.error("Error occurred while reading the file " + ex); logger.error(ex.getStackTrace().toString()); } } return astC.postProcess(); }