From 14facfd0e7b0248bb22972b90c8c4aa519401f5c Mon Sep 17 00:00:00 2001 From: Gang ZHANG Date: Sun, 23 Sep 2018 11:04:11 +0800 Subject: [PATCH] can store parameter variable --- .../depends/extractor/GenericHandler.java | 4 ++ .../extractor/java/JavaAdapterListener.java | 33 ++++++------ .../depends/extractor/java/JavaHandler.java | 6 +++ .../FormalParameterListContextHelper.java | 50 +++++++++++++++---- .../java/context/UnannTypeContextHelper.java | 4 +- .../extractor/java/JavaVarResolveTest.java | 12 ++++- .../java-code-examples/FieldVar.java | 2 +- .../java-code-examples/LocalVar.java | 7 +++ 8 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 src/test/resources/java-code-examples/LocalVar.java diff --git a/src/main/java/depends/extractor/GenericHandler.java b/src/main/java/depends/extractor/GenericHandler.java index eba95bb..1906f17 100644 --- a/src/main/java/depends/extractor/GenericHandler.java +++ b/src/main/java/depends/extractor/GenericHandler.java @@ -37,6 +37,10 @@ public abstract class GenericHandler { entityRepo.addRelation(context().currentFunction().getId(), parameterTypeName, DependencyType.RELATION_PARAMETER); } + public void addReturnRelation(String returnTypeName) { + entityRepo.addRelation(context().currentFunction().getId(), returnTypeName, DependencyType.RELATION_RETURN); + } + public void addVars(String type, String var) { entityRepo.addRelation(context().lastContainer().getId(), type, DependencyType.RELATION_DEFINE); } diff --git a/src/main/java/depends/extractor/java/JavaAdapterListener.java b/src/main/java/depends/extractor/java/JavaAdapterListener.java index c492efd..128ea0d 100644 --- a/src/main/java/depends/extractor/java/JavaAdapterListener.java +++ b/src/main/java/depends/extractor/java/JavaAdapterListener.java @@ -23,6 +23,7 @@ import depends.javaextractor.Java9Parser.InterfaceTypeContext; import depends.javaextractor.Java9Parser.LocalVariableDeclarationContext; import depends.javaextractor.Java9Parser.MethodDeclarationContext; import depends.javaextractor.Java9Parser.MethodDeclaratorContext; +import depends.javaextractor.Java9Parser.MethodHeaderContext; import depends.javaextractor.Java9Parser.NormalClassDeclarationContext; import depends.javaextractor.Java9Parser.NormalInterfaceDeclarationContext; import depends.javaextractor.Java9Parser.PackageDeclarationContext; @@ -34,6 +35,7 @@ import depends.javaextractor.Java9Parser.StaticImportOnDemandDeclarationContext; import depends.javaextractor.Java9Parser.SuperclassContext; import depends.javaextractor.Java9Parser.SuperinterfacesContext; import depends.javaextractor.Java9Parser.TypeImportOnDemandDeclarationContext; +import depends.util.Tuple; public class JavaAdapterListener extends Java9BaseListener{ private JavaHandler handler; @@ -147,15 +149,20 @@ public class JavaAdapterListener extends Java9BaseListener{ ///////////////////////////////////////////////////////// // Method Parameters @Override - public void enterMethodDeclarator(MethodDeclaratorContext ctx) { - Collection parameterTypes = new ArrayList<>(); - if (ctx.formalParameterList()!=null) { - parameterTypes = new FormalParameterListContextHelper(ctx.formalParameterList()).extractParameterTypeList(); + public void enterMethodHeader(MethodHeaderContext ctx) { + MethodDeclaratorContext declarator = ctx.methodDeclarator(); + FormalParameterListContextHelper helper = new FormalParameterListContextHelper(declarator.formalParameterList()); + handler.foundMethodDeclarator(declarator.identifier().getText(),helper.getParameterTypeList()); + + ResultContext result = ctx.result(); + handler.foundReturn(new UnannTypeContextHelper().calculateType(result.unannType())); + super.enterMethodHeader(ctx); + + Collection> varList = helper.getVarList(); + for (Tuple var:varList) { + handler.foundVarDefintion(var.x,var.y); } - handler.foundMethodDeclarator(ctx.identifier().getText(),parameterTypes); - super.enterMethodDeclarator(ctx); } - ///////////////////////////////////////////////////////// // Field @Override @@ -208,18 +215,6 @@ public class JavaAdapterListener extends Java9BaseListener{ super.exitBlock(ctx); } - @Override - public void enterResult(ResultContext ctx) { - // TODO Auto-generated method stub - super.enterResult(ctx); - } } -// relations to be clarified: -// 1. functionName(ParametersList) throws ExceptionTypeList <-- exceptionTypeList should be parameter? -// 2. class defines members <-- what's the relation between them? -// 3. 'Contains' does not defined in the code. Please clarify -// 4. (TODO): the arguments of generic type have not be calculated yet -// 5. Parameter is not about method - > var for java; it is method ( parameter type - diff --git a/src/main/java/depends/extractor/java/JavaHandler.java b/src/main/java/depends/extractor/java/JavaHandler.java index c10ebb7..1c3845c 100644 --- a/src/main/java/depends/extractor/java/JavaHandler.java +++ b/src/main/java/depends/extractor/java/JavaHandler.java @@ -37,6 +37,11 @@ public class JavaHandler extends GenericHandler { addInheritRelation(superClassName); } + public void foundReturn(String returnTypeName) { + returnTypeName = context().resolveTypeNameRef(returnTypeName); + addReturnRelation(returnTypeName); + } + public void foundImplements(String interfaceName) { interfaceName = context().resolveTypeNameRef(interfaceName); addImplementRelation(interfaceName); @@ -59,5 +64,6 @@ public class JavaHandler extends GenericHandler { addVars(context().resolveTypeNameRef(type),var); } + } diff --git a/src/main/java/depends/extractor/java/context/FormalParameterListContextHelper.java b/src/main/java/depends/extractor/java/context/FormalParameterListContextHelper.java index 794a550..6a325b7 100644 --- a/src/main/java/depends/extractor/java/context/FormalParameterListContextHelper.java +++ b/src/main/java/depends/extractor/java/context/FormalParameterListContextHelper.java @@ -6,15 +6,23 @@ import java.util.List; import depends.javaextractor.Java9Parser.FormalParameterContext; import depends.javaextractor.Java9Parser.FormalParameterListContext; +import depends.javaextractor.Java9Parser.IdentifierContext; +import depends.javaextractor.Java9Parser.UnannTypeContext; +import depends.javaextractor.Java9Parser.VariableDeclaratorIdContext; +import depends.util.Tuple; public class FormalParameterListContextHelper { FormalParameterListContext context; List parameterTypes; + List> varList; public FormalParameterListContextHelper(FormalParameterListContext formalParameterListContext) { this.context = formalParameterListContext; parameterTypes = new ArrayList<>(); + varList = new ArrayList<>(); + if (formalParameterListContext!=null) + extractParameterTypeList(); } /** * @@ -25,34 +33,58 @@ public class FormalParameterListContextHelper { * @param context * @return */ - public Collection extractParameterTypeList() { + public void extractParameterTypeList() { if (context != null) { System.out.println(context.getText()); if (context.formalParameters() != null) { System.out.println(context.formalParameters().getText()); for (FormalParameterContext p : context.formalParameters().formalParameter()) { - // Primitive type will be ignored - addParameterType( new UnannTypeContextHelper().calculateType(p.unannType())); + foundParameterDefintion(p.unannType(),p.variableDeclaratorId()); } } + /** + * lastFormalParameter: + * variableModifier* unannType annotation* '...' variableDeclaratorId + * |formalParameter + */ if (context.lastFormalParameter() != null) { if (context.lastFormalParameter().formalParameter() != null) { - // Primitive type will be ignored - addParameterType( new UnannTypeContextHelper().calculateType( - context.lastFormalParameter().formalParameter().unannType())); + foundParameterDefintion(context.lastFormalParameter().formalParameter().unannType(), + context.lastFormalParameter().formalParameter().variableDeclaratorId()); } + if (context.lastFormalParameter().unannType() != null) { - addParameterType( new UnannTypeContextHelper().calculateType( context.lastFormalParameter().unannType())); + foundParameterDefintion(context.lastFormalParameter().unannType(), + context.lastFormalParameter().variableDeclaratorId()); } } + /** + * receiverParameter : annotation* unannType (identifier '.')? 'this' + */ if (context.receiverParameter() != null) { - addParameterType( new UnannTypeContextHelper().calculateType(context.receiverParameter().unannType())); + UnannTypeContext unannType = context.receiverParameter().unannType(); + String type = new UnannTypeContextHelper().calculateType(unannType); + if (type!=null) + this.parameterTypes.add(type); + IdentifierContext var = context.receiverParameter().identifier(); + if (var!=null) + varList.add(new Tuple(type,var.Identifier().getText())); } } + return; + } + public Collection getParameterTypeList(){ return parameterTypes; } - private void addParameterType(String type) { + public Collection> getVarList(){ + return varList; + } + + private void foundParameterDefintion(UnannTypeContext unannType, VariableDeclaratorIdContext variableDeclaratorId) { + String type = new UnannTypeContextHelper().calculateType(unannType); if (type!=null) this.parameterTypes.add(type); + String var = variableDeclaratorId.identifier().getText(); + varList.add(new Tuple(type,var)); } } diff --git a/src/main/java/depends/extractor/java/context/UnannTypeContextHelper.java b/src/main/java/depends/extractor/java/context/UnannTypeContextHelper.java index a4fc7cc..95fc7ee 100644 --- a/src/main/java/depends/extractor/java/context/UnannTypeContextHelper.java +++ b/src/main/java/depends/extractor/java/context/UnannTypeContextHelper.java @@ -10,7 +10,7 @@ public class UnannTypeContextHelper { public String calculateType(UnannTypeContext type) { if (type == null) - return null; + return "void"; UnannReferenceTypeContext t = type.unannReferenceType(); if (t != null) { if (t.unannArrayType() != null && t.unannArrayType().unannClassOrInterfaceType() != null) { @@ -23,7 +23,7 @@ public class UnannTypeContextHelper { }else if (type.unannPrimitiveType()!=null){ return type.unannPrimitiveType().getText(); } - return null; + return "void"; } public String computeType(UnannClassOrInterfaceTypeContext c) { diff --git a/src/test/java/depends/extractor/java/JavaVarResolveTest.java b/src/test/java/depends/extractor/java/JavaVarResolveTest.java index 9f746d5..a829f1a 100644 --- a/src/test/java/depends/extractor/java/JavaVarResolveTest.java +++ b/src/test/java/depends/extractor/java/JavaVarResolveTest.java @@ -16,7 +16,17 @@ public class JavaVarResolveTest { String src = "./src/test/resources/java-code-examples/FieldVar.java"; JavaFileParser parser = new JavaFileParser(src,repo); parser.parse(); - Entity classEntity = repo.getEntity("FieddVar"); + Entity classEntity = repo.getEntity("FieldVar"); assertEquals(3,classEntity.getVars().size()); } + + @Test + public void test_local_var_should_be_parsed() throws IOException { + EntityRepo repo = new EntityRepo(); + String src = "./src/test/resources/java-code-examples/LocalVar.java"; + JavaFileParser parser = new JavaFileParser(src,repo); + parser.parse(); + assertEquals(1,repo.getEntity("LocalVar").getVars().size()); + assertEquals(2,repo.getEntity("LocalVar.foo").getVars().size()); + } } diff --git a/src/test/resources/java-code-examples/FieldVar.java b/src/test/resources/java-code-examples/FieldVar.java index 02458bd..3d03273 100644 --- a/src/test/resources/java-code-examples/FieldVar.java +++ b/src/test/resources/java-code-examples/FieldVar.java @@ -1,5 +1,5 @@ -public class FieddVar { +public class FieldVar { String a; public String b,c; } \ No newline at end of file diff --git a/src/test/resources/java-code-examples/LocalVar.java b/src/test/resources/java-code-examples/LocalVar.java new file mode 100644 index 0000000..d1c89ec --- /dev/null +++ b/src/test/resources/java-code-examples/LocalVar.java @@ -0,0 +1,7 @@ + +public class LocalVar { + String a; + void foo(String b) { + String a; + } +} \ No newline at end of file