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.
 
 
 
 
 
 

216 lines
7.2 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.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.io.IOException;
  10. import java.net.Socket;
  11. import org.eclipse.jdt.core.JavaCore;
  12. import org.eclipse.jdt.core.dom.AST;
  13. import org.eclipse.jdt.core.dom.ASTParser;
  14. import org.eclipse.jdt.core.dom.ASTVisitor;
  15. import org.eclipse.jdt.core.dom.CompilationUnit;
  16. import org.eclipse.jdt.core.dom.ClassInstanceCreation;
  17. import org.eclipse.jdt.core.dom.FieldAccess;
  18. import org.eclipse.jdt.core.dom.FieldDeclaration;
  19. import org.eclipse.jdt.core.dom.MethodDeclaration;
  20. import org.eclipse.jdt.core.dom.MethodInvocation;
  21. import org.eclipse.jdt.core.dom.Name;
  22. import org.eclipse.jdt.core.dom.PackageDeclaration;
  23. import org.eclipse.jdt.core.dom.QualifiedName;
  24. import org.eclipse.jdt.core.dom.QualifiedType;
  25. import org.eclipse.jdt.core.dom.SimpleName;
  26. import org.eclipse.jdt.core.dom.SimpleType;
  27. import org.eclipse.jdt.core.dom.Statement;
  28. import org.eclipse.jdt.core.dom.Type;
  29. import org.eclipse.jdt.core.dom.TypeDeclaration;
  30. import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
  31. import com.bitshift.parsing.parsers.Parser;
  32. import com.bitshift.parsing.symbols.Symbols;
  33. import com.bitshift.parsing.symbols.JavaSymbols;
  34. /*TODO: Work on parsing partial java code.
  35. * Change visits to endVisit and implement a cache for more concise code structure.
  36. * Get rid of unecessary imports.
  37. * Fix column and line numbers.*/
  38. public class JavaParser extends Parser {
  39. public JavaParser(Socket clientSocket) {
  40. super(clientSocket);
  41. }
  42. private String readFromClient() {
  43. String fromClient = "";
  44. try {
  45. BufferedReader clientReader = new BufferedReader(
  46. new InputStreamReader(this.clientSocket.getInputStream()));
  47. int bytes = Integer.parseInt(clientReader.readLine());
  48. System.out.println(bytes);
  49. StringBuilder builder = new StringBuilder();
  50. int i = 0;
  51. while(i < bytes) {
  52. char aux = (char)clientReader.read();
  53. builder.append(aux);
  54. i++;
  55. }
  56. fromClient = builder.toString();
  57. } catch (IOException ex) {
  58. }
  59. return fromClient;
  60. }
  61. private void writeToClient(String toClient) {
  62. try {
  63. PrintWriter clientWriter = new PrintWriter(
  64. this.clientSocket.getOutputStream(), true);
  65. clientWriter.println(toClient);
  66. } catch (IOException ex) {
  67. }
  68. }
  69. @Override
  70. public Symbols genSymbols() {
  71. char[] source = this.readFromClient().toCharArray();
  72. ASTParser parser = ASTParser.newParser(AST.JLS3);
  73. parser.setSource(source);
  74. Map options = JavaCore.getOptions();
  75. parser.setCompilerOptions(options);
  76. CompilationUnit root = (CompilationUnit) parser.createAST(null);
  77. NodeVisitor visitor = new NodeVisitor(root);
  78. root.accept(visitor);
  79. return visitor.symbols;
  80. }
  81. @Override
  82. public void run() {
  83. JavaSymbols symbols = (JavaSymbols) this.genSymbols();
  84. System.out.println(symbols.toString());
  85. writeToClient(symbols.toString());
  86. }
  87. class NodeVisitor extends ASTVisitor {
  88. protected CompilationUnit root;
  89. protected JavaSymbols symbols;
  90. private Stack<HashMap<String, Object>> _cache;
  91. public NodeVisitor(CompilationUnit root) {
  92. this.root = root;
  93. this.symbols = new JavaSymbols();
  94. this._cache = new Stack<HashMap<String, Object>>();
  95. }
  96. public boolean visit(ClassInstanceCreation node) {
  97. return true;
  98. }
  99. public boolean visit(FieldAccess node) {
  100. Name nameObj = node.getName();
  101. String name = nameObj.isQualifiedName() ?
  102. ((QualifiedName) nameObj).getFullyQualifiedName() :
  103. ((SimpleName) nameObj).getIdentifier();
  104. int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
  105. int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
  106. this.symbols.insertFieldAccess(name, sl, sc, null, null);
  107. return true;
  108. }
  109. public boolean visit(FieldDeclaration node) {
  110. return true;
  111. }
  112. public boolean visit(MethodDeclaration node) {
  113. Name nameObj = node.getName();
  114. String name = nameObj.isQualifiedName() ?
  115. ((QualifiedName) nameObj).getFullyQualifiedName() :
  116. ((SimpleName) nameObj).getIdentifier();
  117. List<Statement> statements = node.getBody().statements();
  118. Statement last = statements.get(statements.size() - 1);
  119. int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
  120. int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
  121. int el = this.root.getLineNumber(last.getStartPosition()) - 1;
  122. int ec = this.root.getColumnNumber(last.getStartPosition()) - 1;
  123. this.symbols.insertMethodDeclaration(name, sl, sc, el, ec);
  124. return true;
  125. }
  126. public boolean visit(MethodInvocation node) {
  127. Name nameObj = node.getName();
  128. String name = nameObj.isQualifiedName() ?
  129. ((QualifiedName) nameObj).getFullyQualifiedName() :
  130. ((SimpleName) nameObj).getIdentifier();
  131. int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
  132. int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
  133. this.symbols.insertMethodInvocation(name, sl, sc, null, null);
  134. return true;
  135. }
  136. public boolean visit(PackageDeclaration node) {
  137. Name nameObj = node.getName();
  138. String name = nameObj.isQualifiedName() ?
  139. ((QualifiedName) nameObj).getFullyQualifiedName() :
  140. ((SimpleName) nameObj).getIdentifier();
  141. this.symbols.setPackage(name);
  142. return true;
  143. }
  144. public boolean visit(TypeDeclaration node) {
  145. Name nameObj = node.getName();
  146. String name = nameObj.isQualifiedName() ?
  147. ((QualifiedName) nameObj).getFullyQualifiedName() :
  148. ((SimpleName) nameObj).getIdentifier();
  149. int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
  150. int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
  151. if (node.isInterface()) {
  152. this.symbols.insertInterfaceDeclaration(name, sl, sc, null, null);
  153. } else {
  154. this.symbols.insertClassDeclaration(name, sl, sc, null, null);
  155. }
  156. return true;
  157. }
  158. public boolean visit(VariableDeclarationFragment node) {
  159. Name nameObj = node.getName();
  160. String name = nameObj.isQualifiedName() ?
  161. ((QualifiedName) nameObj).getFullyQualifiedName() :
  162. ((SimpleName) nameObj).getIdentifier();
  163. int sl = this.root.getLineNumber(node.getStartPosition()) - 1;
  164. int sc = this.root.getColumnNumber(node.getStartPosition()) - 1;
  165. this.symbols.insertVariableDeclaration(name, sl, sc, null, null);
  166. return true;
  167. }
  168. }
  169. }