@Override public Object execute(ExecutionEvent event) { // Plugin.isManuallyStarted = true; System.out.println("ASIDECodeAnnotateHandler.java is ran ---first line"); targetPart = HandlerUtil.getActivePart(event); IWorkbenchPartSite site = targetPart.getSite(); ISelectionProvider selectionProvider = site.getSelectionProvider(); if (selectionProvider == null) { return null; } ISelection selection = selectionProvider.getSelection(); if (selection == null) { System.out.println("selectProject = "); return null; } IResource iRes = extractSelection(selection); if (iRes == null) { System.out.println("test == null"); return null; } selectProject = iRes.getProject(); if (selectProject == null) { System.out.println("selectProject == null"); return null; } System.out.println("selectProject = " + selectProject.getName()); // the following is temporarily added here pathCollector = ModelRegistry.getPathCollectorForProject(selectProject); if (pathCollector == null) { pathCollector = new PathCollector(selectProject); } paths = pathCollector.getAllPaths(); if (paths == null) paths = Collections.synchronizedList(new ArrayList<Path>()); System.out.println( "ASIDECodeAnnotateHandler.java is ran -- start iterating files of the project"); IScriptProject scriptProject = DLTKCore.create(selectProject); if (scriptProject == null) { System.out.println("scirpt project == null"); return null; } int count = 1; // gather statistics // GatherStatistics.NumOfWarningsInEachFile(); // while tablename = ... Utils.removeAllQuestionMarkers(iRes); Plugin.projectResource = iRes; Iterator ite = Plugin.sensitive_DB_Tables.iterator(); String currentSensitiveTableName = null; // commented out Nov. 27 /*while(ite.hasNext()){ currentSensitiveTableName = (String) ite.next(); Plugin.CurrentSensitiveDBTable = currentSensitiveTableName; System.out.println("Current Table is=" + Plugin.CurrentSensitiveDBTable);*/ String currentTableName; while (!Plugin.sensitive_DB_Tables_AlphRanked .isEmpty()) { // collect the warnings that comes from one table, one throughout iteration // for each table, and put the results into the currentTableName = Plugin.sensitive_DB_Tables_AlphRanked.first(); count = 1; Plugin.allMarkerRecords.clear(); while (Plugin.sensitiveOperationsForCurrentIteration != null && Plugin.sensitiveOperationsForCurrentIteration.size() != 0) { count++; System.out.println("-----------------begin round " + count); System.out.println( "Plugin.sensitiveOperationsForCurrentIteration size =!!!" + Plugin.sensitiveOperationsForCurrentIteration.size()); IScriptFolder[] folders = null; try { folders = scriptProject.getScriptFolders(); } catch (ModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("number of folders ==" + folders.length); /*String pattern = "Exec"; process(selectProject, pattern);*/ //////////////// Plugin.sensitiveOperationsForAnotherIteration.clear(); Plugin.sensitiveOperationsForAnotherIteration = new HashSet(); // System.out.println("at the begining point: size of current " + // Plugin.sensitiveOperationsForCurrentIteration.size()); int numOfFiles = 0; for (IScriptFolder folder : folders) { String folderName = folder.getElementName(); if (!Constants.PHPLibraryFolders.contains(folderName)) { ISourceModule[] sourceModules = null; try { sourceModules = folder.getSourceModules(); } catch (ModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } numOfFiles += sourceModules.length; } } /* for(int i = 0; i < 20; i++) System.out.println("files num = " + numOfFiles);*/ System.out.println("sum of folders =" + folders.length); int currentFolderNum = 1; for (IScriptFolder folder : folders) { System.out.println("folder scanning = " + currentFolderNum + "/" + folders.length); String folderName = folder.getElementName(); System.out.println("folder name = " + folderName); if (!Constants.PHPLibraryFolders.contains(folderName)) { ISourceModule[] sourceModules = null; try { sourceModules = folder.getSourceModules(); } catch (ModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (ISourceModule tmpSourceModule : sourceModules) { System.out.println("scanning " + tmpSourceModule.getElementName()); // if it is not the first time to run CodeAnnotate on this project, then disable the // scan function, // we have already pop the marker records from the file and displayed in Eclipse // already. if (false) { } // temporarily testing /* if(Plugin.FIRST_TIME_RUN == false){ //simply display the markers based on the marker records in the file String fileDir = tmpSourceModule.getResource().getFullPath().toString(); System.out.println("all markers size = " + Plugin.allMarkerRecords.size()); HashSet<MarkerRecord> markerRecordsInSingleFile = Utils.getMarkerRecordsForSingleFile(Plugin.allMarkerRecords, fileDir); Utils.createMarkersForSingleFile(markerRecordsInSingleFile, tmpSourceModule); HashSet<AnnotationRecord> annotationRecordsInSingleFile = Utils.getAnnotationRecordsForSingleFile(Plugin.allAnnotationRecords, fileDir); Utils.createAnnotationsForSingleFile(annotationRecordsInSingleFile, tmpSourceModule); System.out.println("finished creating markers for fileDir = " + fileDir + ", markerRecordsInSingleFile size = " + markerRecordsInSingleFile.size()); }*/ else { // start scanning the files for sensitive operations // System.out.println("isourcemodule being built = " + // tmpSourceModule.getElementName().toLowerCase()); // System.out.println("full path of the source module is ---" + // tmpSourceModule.getResource().getFullPath().toString()); SensitiveOperationVisitor visitor = new SensitiveOperationVisitor( tmpSourceModule, Plugin.sensitiveOperationsForCurrentIteration, Plugin.sensitiveOperationsForAnotherIteration, Plugin.sensitiveOperations); Program root = null; try { root = Utils.getCompilationUnit(tmpSourceModule); } catch (Exception e) { // TODO Auto-generated catch block System.err.println("root = util.getcompilationUnit() throws exception!"); e.printStackTrace(); } // System.out.println("begin of traverseTopDown"); if (root == null) { System.err.println( "tmpSourceModule name = " + tmpSourceModule.getElementName() + " in " + tmpSourceModule.getPath().toString()); System.err.println("root == null"); // return null; } root.traverseTopDown(visitor); Plugin.sensitiveOperations = visitor.getSensitiveOperations(); Plugin.sensitiveOperationsForAnotherIteration = visitor.getSensitiveOperationsForAnotherIteration(); } } } currentFolderNum++; } Plugin.sensitiveOperationsForCurrentIteration.clear(); Plugin.sensitiveOperationsForCurrentIteration = new HashSet(); // System.out.println("Plugin.sensitiveOperationsForAnotherIteration size after iteration =" // + Plugin.sensitiveOperationsForAnotherIteration.size()); Plugin.sensitiveOperationsForCurrentIteration = (HashSet<SensitiveMethod>) Plugin.sensitiveOperationsForAnotherIteration.clone(); // System.out.println("after assignment, Plugin.sensitiveOperationsForCurrentIteratio size = // " + Plugin.sensitiveOperationsForCurrentIteration.size()); /*String newRuleFileName = "newRulesForIteration" + count + "th.txt"; InRunPluginDataSave.writeNewSensitiveRulesIntoFile(newRuleFileName, Plugin.sensitiveOperationsForCurrentIteration); String mappingFileName = "numOfWarningsInEachFileInIteration" + (count-1) + "th.txt";; InRunPluginDataSave.writeMappingBetweenWarningsAndFiles(mappingFileName, Plugin.numberOfWarningsInEachFile); if(count == 2){ String newTableNamesFileName = "tableNamesEncounteredInIteration" + (count-1) + "th.txt"; InRunPluginDataSave.writeTableNamesIntoFile(newTableNamesFileName, Plugin.allTableNames); String mappingFileName2 = "numOfWarningsRelatedToEachTableForIteration" + (count-1) + "th.txt"; InRunPluginDataSave.writeMappingBetweenWarningsAndFiles(mappingFileName2, Plugin.numberOfWarningsRelatedToEachTable); }*/ PostRunPluginConfig.writeMarkerRecordIntoFile( Plugin.allMarkerRecords, count, currentTableName); } //// newly added // remove the first table name in the treeset so that we focus on the next table in the next // iteration. Plugin.sensitive_DB_Tables_AlphRanked.pollFirst(); Plugin.sensitiveOperationsForCurrentIteration = RulesUtils.getSensitiveOperations(); } // commented out Nov. 27 /*GatherStatistics.writeMarkersForEachTable(Plugin.allMarkerRecords, Plugin.CurrentSensitiveDBTable); Plugin.allMarkerRecords.clear(); Plugin.allMarkerRecords = new HashSet(); count = 1; Plugin.sensitiveOperationsForCurrentIteration.clear(); Plugin.sensitiveOperationsForCurrentIteration = new HashSet(); Plugin.sensitiveOperationsForCurrentIteration = (HashSet<SensitiveMethod>) Plugin.sensitiveOperationsForCurrentIteration_backup.clone(); Plugin.sensitiveOperations.clear(); Plugin.sensitiveOperations = new HashSet(); Plugin.sensitiveOperations = (HashSet<SensitiveMethod>)Plugin.sensitiveOperations_backup.clone(); }*/ // above is temporarily added. // below are temporarily added for the analysis use // GatherStatistics.filesWithoutRequiredAccessControls(Plugin.numberOfWarningsInEachFile, // Plugin.numberOfAccessControlsInEachFile); /* * Use a Job to attach a {@link CodeAnnotateDocumentEditListener} to * each and every IDocument that is related to a ICompilationUnit in the * selected project */ /* * Job job = new MountListenerJob("Mount listener to Java file", * JavaCore.create(selectProject)); job.setPriority(Job.INTERACTIVE); * job.schedule(); */ /* Delegates all heavy lifting to {@link PathFinder} */ /*Job heavy_job = new Job("Finding paths in Project: " + selectProject.getName()) { @Override protected IStatus run(final IProgressMonitor monitor) { try { Plugin.getDefault().getWorkbench().getDisplay() .asyncExec(new Runnable() { @Override public void run() { // PathFinder.getInstance(selectProject).run(monitor); } }); } finally { monitor.done(); } return Status.OK_STATUS; } }; heavy_job.setPriority(Job.LONG); heavy_job.schedule(); */ System.out.println("finished scanning, marker records saved"); // PostRunPluginConfig.config(Plugin.allMarkerRecords, Plugin.allAnnotationRecords); // PostRunPluginConfig.writeMarkerRecordIntoFile(Plugin.allMarkerRecords, count); // GatherStatistics.readWarningStatistics(Plugin.sensitive_DB_Tables_AlphRanked, // "Update_Level5.txt"); return null; }
public static void writeMarkerRecordIntoFile(HashSet<MarkerRecord> allMarkerRecords) { String basePath = Utils.getPlugingBasePath(); String fileName = basePath + Plugin.Cleaned_MARKER_RECORD_FILE; System.out.println("size of markers =========== " + Plugin.allMarkerRecords.size()); Iterator<MarkerRecord> iter = allMarkerRecords.iterator(); MarkerRecord tmpMarkerRecord = null; String fileDir = null; int nodeStart, nodeLength; String markerType; boolean isAnnotated; String seperator = Plugin.COMMA; StringBuffer strBuf = new StringBuffer(); while (iter.hasNext()) { StringBuffer annotationsStrBuf = new StringBuffer(); Iterator<AnnotationRecord> iterAnnotation = null; AnnotationRecord annotationRecord = null; int markerHashCode; tmpMarkerRecord = iter.next(); markerHashCode = tmpMarkerRecord.hashCode(); markerType = tmpMarkerRecord.getMarkerType(); isAnnotated = tmpMarkerRecord.isAnnotated(); fileDir = tmpMarkerRecord.getNodePositionInfo().getFileDir(); nodeStart = tmpMarkerRecord.getNodePositionInfo().getStartPosition(); nodeLength = tmpMarkerRecord.getNodePositionInfo().getLength(); iterAnnotation = tmpMarkerRecord.getAnnotationRecords().iterator(); while (iterAnnotation.hasNext()) { annotationRecord = iterAnnotation.next(); if (iterAnnotation.hasNext()) annotationsStrBuf.append(annotationRecord.hashCode() + Plugin.ITEM_SEPERATOR); else annotationsStrBuf.append(annotationRecord.hashCode()); } // store the Marker information in the persistent file String str = markerHashCode + seperator + fileDir + seperator + nodeStart + seperator + nodeLength + seperator + markerType + seperator + isAnnotated + seperator + annotationsStrBuf.toString() + "\n"; strBuf.append(str); System.out.println( "fileDir=" + fileDir + ", nodeStart=" + nodeStart + ",nodeLength=" + nodeLength + ", markerType=" + markerType + ",isAnnotated=" + isAnnotated); } FileWriter fw = null; try { fw = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(fw); bw.write(strBuf.toString()); bw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }