kaveri插件分析

1)SliceAnnotate。jave文件的361行的handSelectionForSlice处理了edit的selection时间响应,并根据得到的java源代码获取jimple语句。

 1 /**
 2      * Update the slice view with the information from the current selection.
 3      * 
 4      * @param selection
 5      */
 6     private void handleSelectionForSliceView(ISelection selection) {
 7         final ITextSelection _tSelect = (ITextSelection) selection;
 8         if (editor != null) {
 9             String _text = "";
10             final int _nSelLine = _tSelect.getEndLine() + 1;
11             try {
12                 final IRegion _region = editor.getDocumentProvider()
13                         .getDocument(editor.getEditorInput())
14                         .getLineInformation(_nSelLine - 1);
15 
16                 _text = editor.getDocumentProvider().getDocument(
17                         editor.getEditorInput()).get(_region.getOffset(),
18                         _region.getLength());
19             } catch (BadLocationException _ble) {
20                 KaveriErrorLog.logException("Bad Location Exception", _ble);
21                 SECommons.handleException(_ble);
22                 return;
23             }
24             _text = _text.trim();
25 
26             try {
27                 final IType _type = SelectionConverter.getTypeAtOffset(editor);
28                 final IJavaElement _element = SelectionConverter
29                         .getElementAtOffset(editor);
30                 if (_element != null && _element instanceof IMethod) {
31                     final IFile _file = ((IFileEditorInput) editor
32                             .getEditorInput()).getFile();
33                     
34                     final List _stmtlist = SootConvertor.getStmtForLine(_file,
35                             _type, (IMethod) _element, _nSelLine);
36 
37                     if (_stmtlist != null && _stmtlist.size() >= 3) {
38                         final PartialStmtData _psd = KaveriPlugin.getDefault()
39                                 .getIndusConfiguration().getStmtList();
40 
41                         _psd.setJavaFile(_file);
42                         _psd.setSelectedStatement(_text);
43                         _psd.setClassName(PrettySignature.getSignature(_type));                        
44                         _psd.setMethodName(PrettySignature
45                                 .getSignature((IMethod) _element));
46                         _psd.setLineNo(_nSelLine);
47                         _psd.setStmtList(_stmtlist);
48                     }
49                 }
50             } catch (JavaModelException e) {
51                 KaveriErrorLog.logException("Java Model Exception", e);
52                 SECommons.handleException(e);
53             }
54 
55         }
56     }

从一个文件中获取类IndusDecorator.java的134行:             final List _lst = SECommons.getClassesInFile(_file);

把一个class转变成sootclass,TagToAnnotationMapper的第73行 :

if (Scene.v().containsClass(_className)) {
_sootclass = Scene.v().getSootClass(_className);

 

            final IProject _project = _file.getProject();
            final IJavaProject _jp = JavaCore.create(_project);

 

  1  for (int _i = 0; _i < fileList.size(); _i++) {
  2                   final IFile _javaFile = (IFile) fileList.get(_i);
  3                     final ICompilationUnit _icunit = (ICompilationUnit) JavaCore
  4                             .create(_javaFile);
  5 
  6                     if (_icunit != null) {
  7                         IType[] _types = null;
  8 
  9                         _types = _icunit.getAllTypes();
 10 
 11                         for (int _nrun = 0; _nrun < _types.length; _nrun++) {
 12                             final String _elemName = _types[_nrun]
 13                                     .getFullyQualifiedName();
 14                             _classNamesList.add(_elemName);
 15                         }
 16                     }
 17                 }
 18 
 19         Options.v().set_keep_line_number(true);/*设置保留行号*/ 
 20 
 21        if (fileList.size() > 0) {
 22             final IFile _file = (IFile) fileList.get(0);
 23             final IProject _project = _file.getProject();
 24             final IJavaProject _jproject = JavaCore.create(_project);
 25 
 26             final List _listoffiles = SECommons.processForFiles(_jproject);
 27             if (_listoffiles != null && _listoffiles.size() > 0) {
 28                 completeFileList = _listoffiles;
 29                 final List _classlist = new LinkedList();
 30                 for (int _i = 0; _i < _listoffiles.size(); _i++) {
 31                     final IFile _jfile = (IFile) _listoffiles.get(_i);
 32                     _classlist.addAll(SECommons.getClassesInFile(_jfile));
 33                 }
 34                 driver.setApplicationClasses(_classlist);
 35             }
 36         }/*这一段代码示范了java文件和类之间的转换*/
 37 
 38 /*EclipseIndusDriver下面这段代码显示了怎么把class转化为sootclass其中scence是Soot.Scence*/
 39 public void setApplicationClasses(List classes) {
 40         for (int _i = 0; _i < classes.size(); _i++) {
 41             final String _classname = (String) classes.get(_i);
 42             try {
 43                 final SootClass _sootclass = scene.getSootClass(_classname);
 44                 if (_sootclass != null) {
 45                     _sootclass.setApplicationClass();
 46                 }
 47 
 48             } catch (RuntimeException _rme) {
 49 
 50             }
 51         }
 52     }
 53 
 54 
 55 
 56 
 57 
 58 
 59     public int getLineNumber(IFile javafile, int offset){
 60         try{
 61             int lineNumber = 0;
 62             int totalOffset = 0;
 63             InputStreamReader is = new InputStreamReader(javafile.getContents());
 64             BufferedReader br=new BufferedReader(is);
 65             String str;
 66             //char[] charset = new char[offset+1];
 67             
 68             //br.read(charset);
 69             while((str = br.readLine())!= null){
 70                 totalOffset = totalOffset + str.length() + 2;
 71                 lineNumber = lineNumber + 1;
 72                 if(totalOffset > offset){
 73                     break;
 74                 }
 75             }
 76             return lineNumber;
 77         }
 78         catch(Exception e){
 79             return 0;
 80         }
 81         
 82     }
 83     
 84     public void divideStatement(List _fileList){
 85         Iterator i = _fileList.iterator();
 86         while(i.hasNext()){
 87             try{
 88                 IFile ifile = (IFile)i.next();
 89                 final ICompilationUnit _icunit = (ICompilationUnit) JavaCore
 90                 .create(ifile);
 91                 IType[] _types = null;
 92 
 93                 _types = _icunit.getAllTypes();
 94                 IJavaElement[] ie = _icunit.getChildren();
 95                 for(int j=0; j<ie.length; j++){
 96                     IJavaElement _ie = ie[j];
 97                     System.out.println(_ie.getElementName());
 98                     if (_ie != null && _ie instanceof IMethod) {
 99                         IMethod im = (IMethod) _ie;
100                         String str = im.getElementName();
101                         System.out.println(str);
102                     }            
103                 }
104                 for (int _nrun = 0; _nrun < _types.length; _nrun++) {
105                     IType it = _types[_nrun];
106                     IJavaElement[] ije = it.getChildren();
107                     for(int k=0; k<ije.length; k++){
108                         IJavaElement _ij = ije[k];
109                         System.out.println(_ij.getElementName());
110                         if (_ij != null && _ij instanceof IMethod) {
111                             IMethod im = (IMethod) _ij;
112                             //int hdh = im.getOccurrenceCount();
113                             //System.out.println(hdh);
114                             ISourceRange isr = im.getSourceRange();
115                             //System.out.println(im.getSource());
116                             int offset = isr.getOffset();
117                             int length = isr.getLength();
118                             System.out.println(this.getLineNumber(ifile, offset));
119                             System.out.println(this.getLineNumber(ifile, offset+length));
120                             String str = im.getElementName();
121                             System.out.println(str);
122                         }            
123                     }
124                     final String _elemName = _types[_nrun]
125                             .getFullyQualifiedName();
126                     
127                     System.out.println(_elemName);
128                 }
129                 
130             }
131             catch(Exception e){
132                 return;
133             }
134         }
135     }

 

posted @ 2011-11-10 22:42  xlhuang  阅读(216)  评论(0编辑  收藏  举报