data: recording braces in java

* implemented recording of scope braces as local symbols for java
This commit is contained in:
malte_langkabel
2016-08-16 15:22:59 +02:00
parent 77848a3d67
commit 783e0ac060
6 changed files with 244 additions and 17 deletions
-2
View File
@@ -46,8 +46,6 @@ fi
javac -d ./classes -classpath $CLASSPATH src/io/coati/*.java
echo $CLASSPATH
mkdir -p bin
cd classes
+2 -2
View File
@@ -11,8 +11,8 @@ import me.tomassetti.symbolsolver.model.resolution.TypeSolver;
public class ASTDumper extends JavaAstVisitor{
public ASTDumper(int callbackId, String filePath, TypeSolver typeSolver) {
super(callbackId, filePath, typeSolver);
public ASTDumper(int callbackId, String filePath, FileContent fileContent, TypeSolver typeSolver) {
super(callbackId, filePath, fileContent, typeSolver);
}
int indent = 0;
@@ -0,0 +1,49 @@
package io.coati;
import java.util.Arrays;
import java.util.List;
public class FileContent
{
public class Location
{
public int line;
public int column;
Location(int line, int column)
{
this.line = line;
this.column = column;
}
}
private List<String> m_lines;
public FileContent(String text)
{
m_lines = Arrays.asList(text.split("\\r?\\n"));
}
public Location find(String s)
{
return find(s, 1, 1);
}
public Location find(String s, int fromLine, int fromColumn)
{
int lineIndex = fromLine - 1;
int startColumn = fromColumn - 1;
int column = -1;
while (lineIndex < m_lines.size())
{
column = m_lines.get(lineIndex).indexOf(s, startColumn);
if (column != -1)
{
return new Location(lineIndex + 1, column + 1);
}
startColumn = 0;
lineIndex++;
}
return new Location(0, 0);
}
}
+40 -1
View File
@@ -16,9 +16,12 @@ import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.body.VariableDeclaratorId;
import com.github.javaparser.ast.comments.BlockComment;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.ArrayInitializerExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.SwitchStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.PrimitiveType;
import com.github.javaparser.ast.type.Type;
@@ -44,14 +47,16 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
{
private int m_callbackId = -1;
private String m_filePath;
private FileContent m_fileContent;
private TypeSolver m_typeSolver;
private List<DeclContext> m_context = new ArrayList<DeclContext>();
private boolean m_verbose = false;
public JavaAstVisitor(int callbackId, String filePath, TypeSolver typeSolver)
public JavaAstVisitor(int callbackId, String filePath, FileContent fileContent, TypeSolver typeSolver)
{
m_callbackId = callbackId;
m_filePath = filePath;
m_fileContent = fileContent;
m_typeSolver = typeSolver;
String[] filePathParts = filePath.split("/");
@@ -90,6 +95,9 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
n.getBeginLine(), n.getBeginColumn(), n.getEndLine(), n.getEndColumn(),
AccessKind.fromAccessSpecifier(ModifierSet.getAccessSpecifier(n.getModifiers())), false
);
FileContent.Location scopeStartLocation = m_fileContent.find("{", n.getBeginLine(), n.getBeginColumn());
recordScope(scopeStartLocation.line, scopeStartLocation.column, n.getEndLine(), n.getEndColumn());
List<DeclContext> parentContext = m_context;
m_context = new ArrayList<DeclContext>();
@@ -139,6 +147,9 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
AccessKind.fromAccessSpecifier(ModifierSet.getAccessSpecifier(n.getModifiers())), false
);
FileContent.Location scopeStartLocation = m_fileContent.find("{", n.getBeginLine(), n.getBeginColumn());
recordScope(scopeStartLocation.line, scopeStartLocation.column, n.getEndLine(), n.getEndColumn());
List<DeclContext> parentContext = m_context;
m_context = new ArrayList<DeclContext>();
m_context.add(new DeclContext(qualifiedName));
@@ -754,12 +765,39 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
return qualifiedName;
}
@Override public void visit(final BlockStmt n, final Void v)
{
recordScope(n.getBeginLine(), n.getBeginColumn(), n.getEndLine(), n.getEndColumn());
super.visit(n, v);
}
@Override public void visit(final ArrayInitializerExpr n, final Void v)
{
recordScope(n.getBeginLine(), n.getBeginColumn(), n.getEndLine(), n.getEndColumn());
super.visit(n, v);
}
@Override public void visit(final SwitchStmt n, final Void v)
{
FileContent.Location scopeStartLocation = m_fileContent.find("{", n.getBeginLine(), n.getBeginColumn());
recordScope(scopeStartLocation.line, scopeStartLocation.column, n.getEndLine(), n.getEndColumn());
super.visit(n, v);
}
private void recordScope(int beginLine, int beginColumn, int endLine, int endColumn)
{
String qualifiedName = m_filePath + "<" + beginLine + ":" + beginColumn + ">";
JavaIndexer.recordLocalSymbol(m_callbackId, qualifiedName, beginLine, beginColumn, beginLine, beginColumn);
JavaIndexer.recordLocalSymbol(m_callbackId, qualifiedName, endLine, endColumn, endLine, endColumn);
}
@Override public void visit(final LineComment n, final Void v)
{
JavaIndexer.recordComment(
m_callbackId,
n.getBeginLine(), n.getBeginColumn(), n.getEndLine(), n.getEndColumn()
);
super.visit(n, v);
}
@Override public void visit(final BlockComment n, final Void v)
@@ -768,5 +806,6 @@ public class JavaAstVisitor extends JavaAstVisitorAdapter
m_callbackId,
n.getBeginLine(), n.getBeginColumn(), n.getEndLine(), n.getEndColumn()
);
super.visit(n, v);
}
}
+3 -3
View File
@@ -48,9 +48,9 @@ public class JavaIndexer
}
}
CompilationUnit cu = JavaParser.parse(new StringReader(fileContent), true);
JavaAstVisitor astVisitor = new JavaAstVisitor(address, filePath, typeSolver);
// JavaAstVisitor astVisitor = new ASTDumper(address, filePath, typeSolver);
// JavaAstVisitor astVisitor = new JavaAstVisitor(address, filePath, new FileContent(fileContent), typeSolver);
JavaAstVisitor astVisitor = new ASTDumper(address, filePath, new FileContent(fileContent), typeSolver);
cu.accept(astVisitor, null);
}
catch (ParseException e)
+150 -9
View File
@@ -207,8 +207,12 @@ public:
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 1);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<4:15> <4:15 4:15>");
TS_ASSERT_EQUALS(client->localSymbols.size(), 5);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<3:1> <3:1 3:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<3:1> <7:1 7:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<4:15> <4:15 4:15>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<5:2> <5:2 5:2>");
TS_ASSERT_EQUALS(client->localSymbols[4], "input.cc<5:2> <6:2 6:2>");
}
void test_java_parser_finds_definition_of_local_variable()
@@ -224,8 +228,12 @@ public:
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 1);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<6:7> <6:7 6:7>");
TS_ASSERT_EQUALS(client->localSymbols.size(), 5);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<3:1> <3:1 3:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<3:1> <8:1 8:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<5:2> <5:2 5:2>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<5:2> <7:2 7:2>");
TS_ASSERT_EQUALS(client->localSymbols[4], "input.cc<6:7> <6:7 6:7>");
}
void test_java_parser_finds_type_argument_name_in_signature_of_method()
@@ -549,9 +557,13 @@ public:
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 2);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<4:15> <4:15 4:15>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<4:15> <6:3 6:3>");
TS_ASSERT_EQUALS(client->localSymbols.size(), 6);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<3:1> <3:1 3:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<3:1> <8:1 8:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<4:15> <4:15 4:15>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<5:2> <5:2 5:2>");
TS_ASSERT_EQUALS(client->localSymbols[4], "input.cc<5:2> <7:2 7:2>");
TS_ASSERT_EQUALS(client->localSymbols[5], "input.cc<4:15> <6:3 6:3>");
}
void test_java_parser_finds_assignment_of_local_variable()
@@ -568,9 +580,138 @@ public:
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 6);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<3:1> <3:1 3:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<3:1> <9:1 9:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<5:2> <5:2 5:2>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<5:2> <8:2 8:2>");
TS_ASSERT_EQUALS(client->localSymbols[4], "input.cc<6:7> <6:7 6:7>");
TS_ASSERT_EQUALS(client->localSymbols[5], "input.cc<6:7> <7:3 7:3>");
}
void test_java_parser_finds_scope_of_class_declaration()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public class A\n"
"{\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 2);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<6:7> <6:7 6:7>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<6:7> <7:3 7:3>");
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <3:1 3:1>");
}
void test_java_parser_finds_scope_of_enum_declaration()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public enum A\n"
"{\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 2);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <3:1 3:1>");
}
void test_java_parser_finds_scope_of_constructor_declaration()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public class A\n"
"{\n"
" public A()\n"
" {\n"
" }\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 4);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <6:1 6:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<4:2> <4:2 4:2>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<4:2> <5:2 5:2>");
}
void test_java_parser_finds_scope_of_method_declaration()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public class A\n"
"{\n"
" public void a()\n"
" {\n"
" }\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 4);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <6:1 6:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<4:2> <4:2 4:2>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<4:2> <5:2 5:2>");
}
void test_java_parser_finds_scope_of_switch_statement()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public class A\n"
"{\n"
" public void a()\n"
" {\n"
" switch(2)\n"
" {\n"
" case 1:\n"
" break;\n"
" }\n"
" }\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 6);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <11:1 11:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<4:2> <4:2 4:2>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<4:2> <10:2 10:2>");
TS_ASSERT_EQUALS(client->localSymbols[4], "input.cc<6:3> <6:3 6:3>");
TS_ASSERT_EQUALS(client->localSymbols[5], "input.cc<6:3> <9:3 9:3>");
}
void test_java_parser_finds_scope_of_block_statement()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public class A\n"
"{\n"
" public void a()\n"
" {\n"
" {\n"
" }\n"
" }\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 6);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <8:1 8:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<4:2> <4:2 4:2>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<4:2> <7:2 7:2>");
TS_ASSERT_EQUALS(client->localSymbols[4], "input.cc<5:3> <5:3 5:3>");
TS_ASSERT_EQUALS(client->localSymbols[5], "input.cc<5:3> <6:3 6:3>");
}
void test_java_parser_finds_scope_of_array_initialization_list()
{
std::shared_ptr<TestParserClient> client = parseCode(
"public class A\n"
"{\n"
" private int[] array = {1, 2};\n"
"}\n"
);
TS_ASSERT_EQUALS(client->localSymbols.size(), 4);
TS_ASSERT_EQUALS(client->localSymbols[0], "input.cc<2:1> <2:1 2:1>");
TS_ASSERT_EQUALS(client->localSymbols[1], "input.cc<2:1> <4:1 4:1>");
TS_ASSERT_EQUALS(client->localSymbols[2], "input.cc<3:24> <3:24 3:24>");
TS_ASSERT_EQUALS(client->localSymbols[3], "input.cc<3:24> <3:29 3:29>");
}
void test_java_parser_finds_usage_of_type_parameter_of_class()