From f451e426e0ec604ff87d1b354bbd5788a33cc329 Mon Sep 17 00:00:00 2001 From: Benjamin Attal Date: Sun, 20 Apr 2014 12:14:38 -0400 Subject: [PATCH] Refactor of the Java Parser Mod: Parser.java: - Moved client reading and writing methods to the abstract parser class, so that it is not specific to the JavaParser JavaParser.java: - Implemented NodeVisitor._cache. The cache is a stack of data packets. When a node that we want information on is first visited, a new packet of data is pushed onto the stack. The child nodes of that original node than add information to the packet, and when the original node is traversed again on the way up the tree, the data is popped from the cache and added to the symbols. This makes it possible to gather information about various levels of the tree easily. JavaSymbols.java: - Refactor all the insertMethods to simply add a packet of data to the appropriate HashMap. Symbols.java - Add a createCoord method which returns an arraylist representing a point in a document. --- .../com/bitshift/parsing/parsers/JavaParser.java | 162 +++++++++++--------- .../java/com/bitshift/parsing/parsers/Parser.java | 44 +++++- .../com/bitshift/parsing/symbols/JavaSymbols.java | 169 ++++++++------------- .../java/com/bitshift/parsing/symbols/Symbols.java | 8 + 4 files changed, 201 insertions(+), 182 deletions(-) diff --git a/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java b/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java index be5fc37..d7e1c10 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/JavaParser.java @@ -5,11 +5,6 @@ import java.util.List; import java.util.Map; import java.util.Stack; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.io.IOException; - import java.net.Socket; import org.eclipse.jdt.core.JavaCore; @@ -46,44 +41,8 @@ public class JavaParser extends Parser { super(clientSocket); } - private String readFromClient() { - String fromClient = ""; - - try { - BufferedReader clientReader = new BufferedReader( - new InputStreamReader(this.clientSocket.getInputStream())); - - int bytes = Integer.parseInt(clientReader.readLine()); - - StringBuilder builder = new StringBuilder(); - int i = 0; - - while(i < bytes) { - char aux = (char)clientReader.read(); - builder.append(aux); - i++; - } - - fromClient = builder.toString(); - - } catch (IOException ex) { - } - - return fromClient; - } - - private void writeToClient(String toClient) { - try { - PrintWriter clientWriter = new PrintWriter( - this.clientSocket.getOutputStream(), true); - - clientWriter.println(toClient); - } catch (IOException ex) { - } - } - @Override - public Symbols genSymbols() { + protected Symbols genSymbols() { char[] source = this.readFromClient().toCharArray(); ASTParser parser = ASTParser.newParser(AST.JLS3); @@ -118,28 +77,24 @@ public class JavaParser extends Parser { this._cache = new Stack>(); } - public boolean visit(ClassInstanceCreation node) { - return true; - } - - public boolean visit(FieldAccess node) { - Name nameObj = node.getName(); - String name = nameObj.isQualifiedName() ? - ((QualifiedName) nameObj).getFullyQualifiedName() : - ((SimpleName) nameObj).getIdentifier(); - + public boolean visit(FieldDeclaration node) { + HashMap data = new HashMap(); int sl = this.root.getLineNumber(node.getStartPosition()); int sc = this.root.getColumnNumber(node.getStartPosition()); - this.symbols.insertFieldAccess(name, sl, sc, null, null); + data.put("coord", Symbols.createCoord(sl, sc, null, null)); + this._cache.push(data); return true; } - public boolean visit(FieldDeclaration node) { - return true; + public void endVisit(FieldDeclaration node) { + HashMap data = this._cache.pop(); + String name = (String)data.remove("name"); + this.symbols.insertFieldDeclaration(name, data); } public boolean visit(MethodDeclaration node) { + HashMap data = new HashMap(); Name nameObj = node.getName(); String name = nameObj.isQualifiedName() ? ((QualifiedName) nameObj).getFullyQualifiedName() : @@ -152,59 +107,114 @@ public class JavaParser extends Parser { int el = this.root.getLineNumber(last.getStartPosition()); int ec = this.root.getColumnNumber(last.getStartPosition()); - this.symbols.insertMethodDeclaration(name, sl, sc, el, ec); + data.put("coord", Symbols.createCoord(sl, sc, null, null)); + data.put("name", name); + this._cache.push(data); return true; } + public void endVisit(MethodDeclaration node) { + HashMap data = this._cache.pop(); + String name = (String)data.remove("name"); + this.symbols.insertMethodDeclaration(name, data); + } + public boolean visit(MethodInvocation node) { + HashMap data = new HashMap(); Name nameObj = node.getName(); String name = nameObj.isQualifiedName() ? ((QualifiedName) nameObj).getFullyQualifiedName() : ((SimpleName) nameObj).getIdentifier(); - int sl = this.root.getLineNumber(node.getStartPosition()); int sc = this.root.getColumnNumber(node.getStartPosition()); - this.symbols.insertMethodInvocation(name, sl, sc, null, null); + data.put("coord", Symbols.createCoord(sl, sc, null, null)); + data.put("name", name); + this._cache.push(data); return true; } + public void endVisit(MethodInvocation node) { + HashMap data = this._cache.pop(); + String name = (String)data.remove("name"); + this.symbols.insertMethodInvocation(name, data); + } + public boolean visit(PackageDeclaration node) { - Name nameObj = node.getName(); - String name = nameObj.isQualifiedName() ? - ((QualifiedName) nameObj).getFullyQualifiedName() : - ((SimpleName) nameObj).getIdentifier(); + HashMap data = new HashMap(); + this._cache.push(data); + return true; + } + public void endVisit(PackageDeclaration node) { + HashMap data = this._cache.pop(); + String name = (String)data.remove("name"); this.symbols.setPackage(name); - return true; } public boolean visit(TypeDeclaration node) { - Name nameObj = node.getName(); - String name = nameObj.isQualifiedName() ? - ((QualifiedName) nameObj).getFullyQualifiedName() : - ((SimpleName) nameObj).getIdentifier(); + HashMap data = new HashMap(); int sl = this.root.getLineNumber(node.getStartPosition()); int sc = this.root.getColumnNumber(node.getStartPosition()); + data.put("coord", Symbols.createCoord(sl, sc, null, null)); + this._cache.push(data); + return true; + } + + public void endVisit(TypeDeclaration node) { + HashMap data = this._cache.pop(); + String name = (String)data.remove("name"); + if (node.isInterface()) { - this.symbols.insertInterfaceDeclaration(name, sl, sc, null, null); + this.symbols.insertInterfaceDeclaration(name, data); } else { - this.symbols.insertClassDeclaration(name, sl, sc, null, null); + this.symbols.insertClassDeclaration(name, data); } - return true; } public boolean visit(VariableDeclarationFragment node) { - Name nameObj = node.getName(); - String name = nameObj.isQualifiedName() ? - ((QualifiedName) nameObj).getFullyQualifiedName() : - ((SimpleName) nameObj).getIdentifier(); - + HashMap data = new HashMap(); int sl = this.root.getLineNumber(node.getStartPosition()); int sc = this.root.getColumnNumber(node.getStartPosition()); - this.symbols.insertVariableDeclaration(name, sl, sc, null, null); + + data.put("coord", Symbols.createCoord(sl, sc, null, null)); + this._cache.push(data); + return true; + } + + public void endVisit(VariableDeclarationFragment node) { + HashMap data = this._cache.pop(); + String name = (String)data.remove("name"); + this.symbols.insertVariableDeclaration(name, data); + } + + public boolean visit(QualifiedName node) { + if (!this._cache.empty()) { + HashMap data = this._cache.pop(); + + if(!data.containsKey("name")) { + String name = node.getFullyQualifiedName(); + data.put("name", name); + } + + this._cache.push(data); + } + return true; + } + + public boolean visit(SimpleName node) { + if (!this._cache.empty()) { + HashMap data = this._cache.pop(); + + if(!data.containsKey("name")) { + String name = node.getIdentifier(); + data.put("name", name); + } + + this._cache.push(data); + } return true; } diff --git a/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java b/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java index 7ce9b7c..088c185 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/parsers/Parser.java @@ -1,6 +1,12 @@ package com.bitshift.parsing.parsers; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.IOException; + import java.net.Socket; + import com.bitshift.parsing.symbols.Symbols; public abstract class Parser implements Runnable { @@ -11,7 +17,43 @@ public abstract class Parser implements Runnable { this.clientSocket = clientSocket; } - abstract Symbols genSymbols(); + protected String readFromClient() { + String fromClient = ""; + + try { + BufferedReader clientReader = new BufferedReader( + new InputStreamReader(this.clientSocket.getInputStream())); + + int bytes = Integer.parseInt(clientReader.readLine()); + + StringBuilder builder = new StringBuilder(); + int i = 0; + + while(i < bytes) { + char aux = (char)clientReader.read(); + builder.append(aux); + i++; + } + + fromClient = builder.toString(); + + } catch (IOException ex) { + } + + return fromClient; + } + + protected void writeToClient(String toClient) { + try { + PrintWriter clientWriter = new PrintWriter( + this.clientSocket.getOutputStream(), true); + + clientWriter.println(toClient); + } catch (IOException ex) { + } + } + + protected abstract Symbols genSymbols(); public abstract void run(); diff --git a/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java b/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java index ef8efb5..bec9c0f 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/symbols/JavaSymbols.java @@ -1,7 +1,6 @@ package com.bitshift.parsing.symbols; import java.util.List; -import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import com.bitshift.parsing.symbols.Symbols; @@ -10,19 +9,19 @@ import com.bitshift.parsing.symbols.Symbols; public class JavaSymbols extends Symbols { private String _packageName; - private Map _classes; - private Map _interfaces; - private Map _methods; - private Map _fields; - private Map _vars; + private HashMap> _classes; + private HashMap> _interfaces; + private HashMap> _methods; + private HashMap> _fields; + private HashMap> _vars; public JavaSymbols() { _packageName = null; - _classes = new HashMap(); - _interfaces = new HashMap(); - _methods = new HashMap(); - _fields = new HashMap(); - _vars = new HashMap(); + _classes = new HashMap>(); + _interfaces = new HashMap>(); + _methods = new HashMap>(); + _fields = new HashMap>(); + _vars = new HashMap>(); } public boolean setPackage(String name) { @@ -30,118 +29,78 @@ public class JavaSymbols extends Symbols { return true; } - public boolean insertClassDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_classes.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(0, pos); - this._classes.put(name, copy); - return true; - } - public boolean insertClassInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_classes.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(pos); - this._classes.put(name, copy); + public boolean insertClassDeclaration(String name, HashMap data) { + this._classes.put(name, data); return true; } - public boolean insertInterfaceDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_interfaces.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(0, pos); - this._interfaces.put(name, copy); - return true; - } - public boolean insertInterfaceInstance(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_interfaces.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(pos); - this._interfaces.put(name, copy); + public boolean insertInterfaceDeclaration(String name, HashMap data) { + this._interfaces.put(name, data); return true; } - public boolean insertMethodDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_methods.get(name); - copy = (copy == null) ? new ArrayList>() : copy; + public boolean insertMethodDeclaration(String name, HashMap data) { + HashMap method = this._methods.get(name); + if (method == null) { + method = new HashMap(); + method.put("declaration", data); + } else { + method.put("declaration", data); + } - copy.add(0, pos); - this._methods.put(name, copy); + this._methods.put(name, method); return true; } - public boolean insertMethodInvocation(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_methods.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(pos); - this._methods.put(name, copy); + public boolean insertMethodInvocation(String name, HashMap data) { + HashMap method = this._methods.get(name); + if (method == null) { + method = new HashMap(); + ArrayList calls = new ArrayList(10); + calls.add(data); + method.put("calls", calls); + } else { + ArrayList calls = (ArrayList)method.get("calls"); + calls = (calls == null) ? new ArrayList(10) : calls; + calls.add(data); + method.put("calls", calls); + } + + this._methods.put(name, method); return true; } - public boolean insertFieldDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_fields.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(0, pos); - this._fields.put(name, copy); - return true; - } - public boolean insertFieldAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_fields.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(pos); - this._fields.put(name, copy); + public boolean insertFieldDeclaration(String name, HashMap data) { + this._fields.put(name, data); return true; } - public boolean insertVariableDeclaration(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_vars.get(name); - copy = (copy == null) ? new ArrayList>() : copy; + public boolean insertVariableDeclaration(String name, HashMap data) { + HashMap var = this._vars.get(name); + if (var == null) { + var = new HashMap(); + var.put("declaration", data); + } else { + var.put("declaration", data); + } - copy.add(0, pos); - this._vars.put(name, copy); + this._vars.put(name, var); return true; } - public boolean insertVariableAccess(String name, Integer startLine, Integer startCol, Integer endLine, Integer endCol) { - List pos = new ArrayList(4); - pos.add(startLine); pos.add(startCol); pos.add(endLine); pos.add(endCol); - - List> copy = (List>)_vars.get(name); - copy = (copy == null) ? new ArrayList>() : copy; - - copy.add(pos); - this._vars.put(name, copy); + public boolean insertVariableAccess(String name, HashMap data) { + HashMap var = this._vars.get(name); + if (var == null) { + var = new HashMap(); + ArrayList uses = new ArrayList(10); + uses.add(data); + var.put("uses", uses); + } else { + ArrayList uses = (ArrayList)var.get("uses"); + uses = (uses == null) ? new ArrayList(10) : uses; + uses.add(data); + var.put("uses", uses); + } + + this._vars.put(name, var); return true; } diff --git a/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java b/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java index 116dbd7..8bbf44d 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/symbols/Symbols.java @@ -1,9 +1,17 @@ package com.bitshift.parsing.symbols; +import java.util.ArrayList; + public abstract class Symbols { public Symbols() { } + public static ArrayList createCoord(Integer startLine, Integer startCol, Integer endLine, Integer endCol) { + ArrayList coord = new ArrayList(4); + coord.add(startLine); coord.add(startCol); coord.add(endLine); coord.add(endCol); + return coord; + } + }