A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

205 lines
7.1 KiB

  1. package com.bitshift.parsing.parsers;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Stack;
  6. import java.net.Socket;
  7. import org.eclipse.jdt.core.JavaCore;
  8. import org.eclipse.jdt.core.dom.AST;
  9. import org.eclipse.jdt.core.dom.ASTParser;
  10. import org.eclipse.jdt.core.dom.ASTVisitor;
  11. import org.eclipse.jdt.core.dom.CompilationUnit;
  12. import org.eclipse.jdt.core.dom.ClassInstanceCreation;
  13. import org.eclipse.jdt.core.dom.MethodDeclaration;
  14. import org.eclipse.jdt.core.dom.MethodInvocation;
  15. import org.eclipse.jdt.core.dom.Name;
  16. import org.eclipse.jdt.core.dom.PackageDeclaration;
  17. import org.eclipse.jdt.core.dom.QualifiedName;
  18. import org.eclipse.jdt.core.dom.SimpleName;
  19. import org.eclipse.jdt.core.dom.Statement;
  20. import org.eclipse.jdt.core.dom.TypeDeclaration;
  21. import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
  22. import com.bitshift.parsing.parsers.Parser;
  23. import com.bitshift.parsing.symbols.Symbols;
  24. import com.bitshift.parsing.symbols.JavaSymbols;
  25. /*TODO: Work on parsing partial java code.*/
  26. public class JavaParser extends Parser {
  27. public JavaParser(Socket clientSocket) {
  28. super(clientSocket);
  29. }
  30. @Override
  31. protected Symbols genSymbols() {
  32. char[] source = this.readFromClient().toCharArray();
  33. ASTParser parser = ASTParser.newParser(AST.JLS3);
  34. parser.setSource(source);
  35. Map options = JavaCore.getOptions();
  36. parser.setCompilerOptions(options);
  37. CompilationUnit root = (CompilationUnit) parser.createAST(null);
  38. NodeVisitor visitor = new NodeVisitor(root);
  39. root.accept(visitor);
  40. return visitor.symbols;
  41. }
  42. @Override
  43. public void run() {
  44. JavaSymbols symbols = (JavaSymbols) this.genSymbols();
  45. writeToClient(symbols.toString());
  46. }
  47. class NodeVisitor extends ASTVisitor {
  48. protected CompilationUnit root;
  49. protected JavaSymbols symbols;
  50. private Stack<HashMap<String, Object>> _cache;
  51. public NodeVisitor(CompilationUnit root) {
  52. this.root = root;
  53. this.symbols = new JavaSymbols();
  54. this._cache = new Stack<HashMap<String, Object>>();
  55. }
  56. public boolean visit(MethodDeclaration node) {
  57. HashMap<String, Object> data = new HashMap<String, Object>();
  58. Name nameObj = node.getName();
  59. String name = nameObj.isQualifiedName() ?
  60. ((QualifiedName) nameObj).getFullyQualifiedName() :
  61. ((SimpleName) nameObj).getIdentifier();
  62. List<Statement> statements = node.getBody().statements();
  63. int sl = this.root.getLineNumber(node.getStartPosition());
  64. int sc = this.root.getColumnNumber(node.getStartPosition());
  65. Integer el = sl;
  66. Integer ec = -1;
  67. if (statements.size() > 0) {
  68. Statement last = statements.get(statements.size() - 1);
  69. el = this.root.getLineNumber(last.getStartPosition());
  70. ec = this.root.getColumnNumber(last.getStartPosition());
  71. }
  72. data.put("coord", Symbols.createCoord(sl, sc, el, ec));
  73. data.put("name", name);
  74. this._cache.push(data);
  75. return true;
  76. }
  77. public void endVisit(MethodDeclaration node) {
  78. HashMap<String, Object> data = this._cache.pop();
  79. String name = (String)data.remove("name");
  80. this.symbols.insertMethodDeclaration("\"" + name + "\"", data);
  81. }
  82. public boolean visit(MethodInvocation node) {
  83. HashMap<String, Object> data = new HashMap<String, Object>();
  84. Name nameObj = node.getName();
  85. String name = nameObj.isQualifiedName() ?
  86. ((QualifiedName) nameObj).getFullyQualifiedName() :
  87. ((SimpleName) nameObj).getIdentifier();
  88. int sl = this.root.getLineNumber(node.getStartPosition());
  89. int sc = this.root.getColumnNumber(node.getStartPosition());
  90. data.put("coord", Symbols.createCoord(sl, sc, sl, -1));
  91. data.put("name", name);
  92. this._cache.push(data);
  93. return true;
  94. }
  95. public void endVisit(MethodInvocation node) {
  96. HashMap<String, Object> data = this._cache.pop();
  97. String name = (String)data.remove("name");
  98. this.symbols.insertMethodInvocation("\"" + name + "\"", data);
  99. }
  100. public boolean visit(PackageDeclaration node) {
  101. HashMap<String, Object> data = new HashMap<String, Object>();
  102. this._cache.push(data);
  103. return true;
  104. }
  105. public void endVisit(PackageDeclaration node) {
  106. HashMap<String, Object> data = this._cache.pop();
  107. String name = (String)data.remove("name");
  108. this.symbols.setPackage(name);
  109. }
  110. public boolean visit(TypeDeclaration node) {
  111. HashMap<String, Object> data = new HashMap<String, Object>();
  112. int sl = this.root.getLineNumber(node.getStartPosition());
  113. int sc = this.root.getColumnNumber(node.getStartPosition());
  114. data.put("coord", Symbols.createCoord(sl, sc, sl, -1));
  115. this._cache.push(data);
  116. return true;
  117. }
  118. public void endVisit(TypeDeclaration node) {
  119. HashMap<String, Object> data = this._cache.pop();
  120. String name = (String)data.remove("name");
  121. if (node.isInterface()) {
  122. this.symbols.insertInterfaceDeclaration("\"" + name + "\"", data);
  123. } else {
  124. this.symbols.insertClassDeclaration("\"" + name + "\"", data);
  125. }
  126. }
  127. public boolean visit(VariableDeclarationFragment node) {
  128. HashMap<String, Object> data = new HashMap<String, Object>();
  129. int sl = this.root.getLineNumber(node.getStartPosition());
  130. int sc = this.root.getColumnNumber(node.getStartPosition());
  131. data.put("coord", Symbols.createCoord(sl, sc, sl, -1));
  132. this._cache.push(data);
  133. return true;
  134. }
  135. public void endVisit(VariableDeclarationFragment node) {
  136. HashMap<String, Object> data = this._cache.pop();
  137. String name = (String)data.remove("name");
  138. this.symbols.insertVariableDeclaration("\"" + name + "\"", data);
  139. }
  140. public boolean visit(QualifiedName node) {
  141. if (!this._cache.empty()) {
  142. HashMap<String, Object> data = this._cache.pop();
  143. if(!data.containsKey("name")) {
  144. String name = node.getFullyQualifiedName();
  145. data.put("name", name);
  146. }
  147. this._cache.push(data);
  148. }
  149. return true;
  150. }
  151. public boolean visit(SimpleName node) {
  152. if (!this._cache.empty()) {
  153. HashMap<String, Object> data = this._cache.pop();
  154. if(!data.containsKey("name")) {
  155. String name = node.getIdentifier();
  156. data.put("name", name);
  157. }
  158. this._cache.push(data);
  159. }
  160. return true;
  161. }
  162. }
  163. }