can store parameter variable

This commit is contained in:
Gang ZHANG 2018-09-23 11:04:11 +08:00
parent 89973f15b8
commit 14facfd0e7
8 changed files with 86 additions and 32 deletions

View File

@ -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);
}

View File

@ -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<String> 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<Tuple<String, String>> varList = helper.getVarList();
for (Tuple<String, String> 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

View File

@ -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);
}
}

View File

@ -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<String> parameterTypes;
List<Tuple<String, String>> 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<String> 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<String, String>(type,var.Identifier().getText()));
}
}
return;
}
public Collection<String> getParameterTypeList(){
return parameterTypes;
}
private void addParameterType(String type) {
public Collection<Tuple<String, String>> 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<String, String>(type,var));
}
}

View File

@ -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) {

View File

@ -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());
}
}

View File

@ -1,5 +1,5 @@
public class FieddVar {
public class FieldVar {
String a;
public String b,c;
}

View File

@ -0,0 +1,7 @@
public class LocalVar {
String a;
void foo(String b) {
String a;
}
}