add simple set relation
This commit is contained in:
parent
14facfd0e7
commit
6f31e63fc0
|
@ -10,5 +10,9 @@ public class VarEntity extends Entity {
|
|||
super(fullName, parentId,id);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -41,6 +41,14 @@ public abstract class GenericHandler {
|
|||
entityRepo.addRelation(context().currentFunction().getId(), returnTypeName, DependencyType.RELATION_RETURN);
|
||||
}
|
||||
|
||||
public void addSetRelation(String type) {
|
||||
Entity currentFunction = context().currentFunction();
|
||||
if (currentFunction!=null)
|
||||
entityRepo.addRelation(currentFunction.getId(), type, DependencyType.RELATION_SET);
|
||||
else
|
||||
entityRepo.addRelation(context().currentType().getId(), type, DependencyType.RELATION_SET);
|
||||
}
|
||||
|
||||
public void addVars(String type, String var) {
|
||||
entityRepo.addRelation(context().lastContainer().getId(), type, DependencyType.RELATION_DEFINE);
|
||||
}
|
||||
|
|
|
@ -121,4 +121,15 @@ public class HandlerContext{
|
|||
public Entity lastContainer() {
|
||||
return entityStack.peek();
|
||||
}
|
||||
public String inferType(String varName) {
|
||||
for (int i=entityStack.size()-1;i>=0;i--) {
|
||||
Entity t = entityStack.get(i);
|
||||
for (VarEntity var:t.getVars()) {
|
||||
if (var.getFullName().equals(varName)){
|
||||
return var.getType();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ import depends.extractor.java.context.VariableDeclaratorContextHelper;
|
|||
import depends.javaextractor.Java9BaseListener;
|
||||
import depends.javaextractor.Java9Parser.AnnotationTypeDeclarationContext;
|
||||
import depends.javaextractor.Java9Parser.AnnotationTypeElementDeclarationContext;
|
||||
import depends.javaextractor.Java9Parser.AssignmentContext;
|
||||
import depends.javaextractor.Java9Parser.BlockContext;
|
||||
import depends.javaextractor.Java9Parser.ClassDeclarationContext;
|
||||
import depends.javaextractor.Java9Parser.ClassTypeContext;
|
||||
|
@ -27,6 +28,10 @@ import depends.javaextractor.Java9Parser.MethodHeaderContext;
|
|||
import depends.javaextractor.Java9Parser.NormalClassDeclarationContext;
|
||||
import depends.javaextractor.Java9Parser.NormalInterfaceDeclarationContext;
|
||||
import depends.javaextractor.Java9Parser.PackageDeclarationContext;
|
||||
import depends.javaextractor.Java9Parser.PostIncrementExpressionContext;
|
||||
import depends.javaextractor.Java9Parser.PostIncrementExpression_lf_postfixExpressionContext;
|
||||
import depends.javaextractor.Java9Parser.PreDecrementExpressionContext;
|
||||
import depends.javaextractor.Java9Parser.PreIncrementExpressionContext;
|
||||
import depends.javaextractor.Java9Parser.ResourceContext;
|
||||
import depends.javaextractor.Java9Parser.ResultContext;
|
||||
import depends.javaextractor.Java9Parser.SingleStaticImportDeclarationContext;
|
||||
|
@ -203,6 +208,50 @@ public class JavaAdapterListener extends Java9BaseListener{
|
|||
super.enterResource(ctx);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
// Assignment or In(De)Cremental
|
||||
@Override
|
||||
public void enterAssignment(AssignmentContext ctx) {
|
||||
if (ctx.leftHandSide().expressionName()!=null) {
|
||||
handler.foundVariableSet(ctx.leftHandSide().expressionName().identifier().getText());
|
||||
}
|
||||
if (ctx.leftHandSide().fieldAccess()!=null) {
|
||||
//TODO
|
||||
}
|
||||
if (ctx.leftHandSide().arrayAccess()!=null) {
|
||||
//TODO
|
||||
}
|
||||
super.enterAssignment(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterPreIncrementExpression(PreIncrementExpressionContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
super.enterPreIncrementExpression(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterPreDecrementExpression(PreDecrementExpressionContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
super.enterPreDecrementExpression(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterPostIncrementExpression(PostIncrementExpressionContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
System.out.println(ctx.postfixExpression().expressionName().identifier().getText());
|
||||
super.enterPostIncrementExpression(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterPostIncrementExpression_lf_postfixExpression(
|
||||
PostIncrementExpression_lf_postfixExpressionContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
super.enterPostIncrementExpression_lf_postfixExpression(ctx);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////
|
||||
// Block
|
||||
@Override
|
||||
public void enterBlock(BlockContext ctx) {
|
||||
// TODO Auto-generated method stub
|
||||
|
@ -218,3 +267,10 @@ public class JavaAdapterListener extends Java9BaseListener{
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
unaryExpressionNotPlusMinus
|
||||
: postfixExpression
|
||||
| '~' unaryExpression
|
||||
| '!' unaryExpression
|
||||
| castExpression
|
||||
*/
|
||||
|
|
|
@ -64,6 +64,12 @@ public class JavaHandler extends GenericHandler {
|
|||
addVars(context().resolveTypeNameRef(type),var);
|
||||
}
|
||||
|
||||
public void foundVariableSet(String varName) {
|
||||
String type = context().inferType(varName);
|
||||
if (type!=null) {
|
||||
addSetRelation(type);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -29,4 +29,14 @@ public class JavaVarResolveTest {
|
|||
assertEquals(1,repo.getEntity("LocalVar").getVars().size());
|
||||
assertEquals(2,repo.getEntity("LocalVar.foo").getVars().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_local_var_type_could_be_inferred() throws IOException {
|
||||
EntityRepo repo = new EntityRepo();
|
||||
String src = "./src/test/resources/java-code-examples/LocalVarInferExample.java";
|
||||
JavaFileParser parser = new JavaFileParser(src,repo);
|
||||
parser.parse();
|
||||
assertEquals(2,repo.getEntity("LocalVarInferExample.setExample").getVars().size());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue