A semantic search engine for source code https://bitshift.benkurtovic.com/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

128 рядки
4.7 KiB

  1. package org.bitshift.parsing.parsers;
  2. import java.util.Map;
  3. import org.eclipse.jdt.core.JavaCore;
  4. import org.eclipse.jdt.core.dom.AST;
  5. import org.eclipse.jdt.core.dom.ASTParser;
  6. import org.eclipse.jdt.core.dom.ASTVisitor;
  7. import org.eclipse.jdt.core.dom.CompilationUnit;
  8. import org.eclipse.jdt.core.dom.ClassInstanceCreation;
  9. import org.eclipse.jdt.core.dom.FieldAccess
  10. import org.eclipse.jdt.core.dom.FieldDeclaration;
  11. import org.eclipse.jdt.core.dom.MethodDeclaration;
  12. import org.eclipse.jdt.core.dom.MethodInvocation;
  13. import org.eclipse.jdt.core.dom.PackageDeclaration;
  14. import org.eclipse.jdt.core.dom.Statement;
  15. import org.eclipse.jdt.core.dom.TypeDeclaration;
  16. import org.eclipse.jdt.core.dom.VariableDeclarationStatement
  17. import org.bitshift.parsing.parsers.Parser;
  18. import org.bitshift.parsing.symbols.Symbols;
  19. import org.bitshift.parsing.symbols.JavaSymbols;
  20. /*TODO: Work on parsing partial java code.
  21. * Make sure all names of nodes are strings.*/
  22. public class JavaParser extends Parser {
  23. @Override
  24. public Symbols genSymbols() {
  25. char[] source = this.source.toCharArray();
  26. ASTParser parser = ASTParser.newParser(AST.JLS3);
  27. parser.setSource(source);
  28. Map options = JavaCore.getOptions();
  29. parser.setCompilerOptions(options);
  30. //Work on parsing partial java code later
  31. CompilationUnit result = (CompilationUnit) parser.createAST(null);
  32. ASTVisitor visitor = new NodeVisitor(result);
  33. result.accept(visitor);
  34. return visitor.symbols;
  35. }
  36. class NodeVisitor extends ASTVisitor {
  37. protected Symbols symbols;
  38. protected CompilationUnit compUnit;
  39. public NodeVisitor(CompilationUnit compUnit) {
  40. symbols = new JavaSymbols();
  41. }
  42. public boolean visit(ClassInstanceCreation node) {
  43. String name = node.getType().getName();
  44. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  45. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  46. symbols.insertClassInstance(name, sl, sc, null, null);
  47. return true;
  48. }
  49. public boolean visit(FieldAccess node) {
  50. String name = node.getName();
  51. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  52. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  53. symbols.insertFieldAccess(name, sl, sc, null, null);
  54. return true;
  55. }
  56. public boolean visit(FieldDeclaration node) {
  57. String name = node.getType().getName();
  58. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  59. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  60. symbols.insertFieldDeclaration(name, sl, sc, null, null);
  61. return true;
  62. }
  63. public boolean visit(MethodDeclaration node) {
  64. String name = node.getName();
  65. List<Statement> statements = node.getBody().statements();
  66. Statement last = statements.get(statements.size() - 1);
  67. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  68. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  69. int el = compUnit.getLineNumber(last.getStartPosition()) - 1;
  70. int ec = compUnit.getColumnNumber(last.getStartPosition()) - 1;
  71. symbols.insertMethodDeclaration(name, sl, sc, el, ec);
  72. return true;
  73. }
  74. public boolean visit(MethodInvocation node) {
  75. String name = node.getName();
  76. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  77. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  78. symbols.insertMethodInvocation(name, sl, sc, null, null);
  79. return true;
  80. }
  81. public boolean visit(PackageDeclaration node) {
  82. symbols.setPackage(node.getName());
  83. return true;
  84. }
  85. public boolean visit(TypeDeclaration node) {
  86. String name = node.getName();
  87. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  88. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  89. if (node.isInterface()) {
  90. symbols.insertInterfaceDeclaration(name, sl, sc, null, null);
  91. } else {
  92. symbols.insertClassDeclaration(name, sl, sc, null, null);
  93. }
  94. return true;
  95. }
  96. public boolean visit(VariableDeclarationStatement node) {
  97. String name = node.getType().getName();
  98. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  99. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  100. symbols.insertVariableDeclaration(name, sl, sc, null, null);
  101. return true;
  102. }
  103. }
  104. }