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.
 
 
 
 
 
 

165 lines
6.7 KiB

  1. package org.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 org.eclipse.jdt.core.JavaCore;
  7. import org.eclipse.jdt.core.dom.AST;
  8. import org.eclipse.jdt.core.dom.ASTParser;
  9. import org.eclipse.jdt.core.dom.ASTVisitor;
  10. import org.eclipse.jdt.core.dom.CompilationUnit;
  11. import org.eclipse.jdt.core.dom.ClassInstanceCreation;
  12. import org.eclipse.jdt.core.dom.FieldAccess;
  13. import org.eclipse.jdt.core.dom.FieldDeclaration;
  14. import org.eclipse.jdt.core.dom.MethodDeclaration;
  15. import org.eclipse.jdt.core.dom.MethodInvocation;
  16. import org.eclipse.jdt.core.dom.Name;
  17. import org.eclipse.jdt.core.dom.PackageDeclaration;
  18. import org.eclipse.jdt.core.dom.QualifiedName;
  19. import org.eclipse.jdt.core.dom.QualifiedType;
  20. import org.eclipse.jdt.core.dom.SimpleName;
  21. import org.eclipse.jdt.core.dom.SimpleType;
  22. import org.eclipse.jdt.core.dom.Statement;
  23. import org.eclipse.jdt.core.dom.Type;
  24. import org.eclipse.jdt.core.dom.TypeDeclaration;
  25. import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
  26. import org.bitshift.parsing.parsers.Parser;
  27. import org.bitshift.parsing.symbols.Symbols;
  28. import org.bitshift.parsing.symbols.JavaSymbols;
  29. /*TODO: Work on parsing partial java code.
  30. * Change visits to endVisit and implement a cache*/
  31. public class JavaParser extends Parser {
  32. protected JavaSymbols symbols;
  33. protected CompilationUnit compUnit;
  34. private Stack<Map<String, Object>> _cache;
  35. public JavaParser(String source) {
  36. super(source);
  37. this.symbols = new JavaSymbols();
  38. this._cache = new Stack<Map<String, Object>>();
  39. }
  40. @Override
  41. public Symbols genSymbols() {
  42. char[] source = this.source.toCharArray();
  43. ASTParser parser = ASTParser.newParser(AST.JLS3);
  44. parser.setSource(source);
  45. Map options = JavaCore.getOptions();
  46. parser.setCompilerOptions(options);
  47. //Work on parsing partial java code later
  48. this.compUnit = (CompilationUnit) parser.createAST(null);
  49. ASTVisitor visitor = new NodeVisitor();
  50. this.compUnit.accept(visitor);
  51. return this.symbols;
  52. }
  53. class NodeVisitor extends ASTVisitor {
  54. public boolean visit(ClassInstanceCreation node) {
  55. Type typeObj = node.getType();
  56. Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName();
  57. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  58. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  59. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  60. symbols.insertClassInstance(name, sl, sc, null, null);
  61. return true;
  62. }
  63. public boolean visit(FieldAccess node) {
  64. Name nameObj = node.getName();
  65. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  66. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  67. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  68. symbols.insertFieldAccess(name, sl, sc, null, null);
  69. return true;
  70. }
  71. public boolean visit(FieldDeclaration node) {
  72. Type typeObj = node.getType();
  73. Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName();
  74. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  75. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  76. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  77. symbols.insertFieldDeclaration(name, sl, sc, null, null);
  78. return true;
  79. }
  80. public boolean visit(MethodDeclaration node) {
  81. Name nameObj = node.getName();
  82. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  83. List<Statement> statements = node.getBody().statements();
  84. Statement last = statements.get(statements.size() - 1);
  85. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  86. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  87. int el = compUnit.getLineNumber(last.getStartPosition()) - 1;
  88. int ec = compUnit.getColumnNumber(last.getStartPosition()) - 1;
  89. symbols.insertMethodDeclaration(name, sl, sc, el, ec);
  90. return true;
  91. }
  92. public boolean visit(MethodInvocation node) {
  93. Name nameObj = node.getName();
  94. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  95. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  96. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  97. symbols.insertMethodInvocation(name, sl, sc, null, null);
  98. return true;
  99. }
  100. public boolean visit(PackageDeclaration node) {
  101. Name nameObj = node.getName();
  102. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  103. symbols.setPackage(name);
  104. return true;
  105. }
  106. public boolean visit(TypeDeclaration node) {
  107. Name nameObj = node.getName();
  108. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  109. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  110. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  111. if (node.isInterface()) {
  112. symbols.insertInterfaceDeclaration(name, sl, sc, null, null);
  113. } else {
  114. symbols.insertClassDeclaration(name, sl, sc, null, null);
  115. }
  116. return true;
  117. }
  118. public boolean visit(VariableDeclarationStatement node) {
  119. Type typeObj = node.getType();
  120. Name nameObj = typeObj.isQualifiedType() ? ((QualifiedType) typeObj).getName() : ((SimpleType) typeObj).getName();
  121. String name = nameObj.isQualifiedName() ? ((QualifiedName)nameObj).getFullyQualifiedName() : ((SimpleName)nameObj).getIdentifier();
  122. int sl = compUnit.getLineNumber(node.getStartPosition()) - 1;
  123. int sc = compUnit.getColumnNumber(node.getStartPosition()) - 1;
  124. symbols.insertVariableDeclaration(name, sl, sc, null, null);
  125. return true;
  126. }
  127. }
  128. }