diff --git a/core/adapter/identifier/java_identifier_listener.go b/core/adapter/identifier/java_identifier_listener.go index 83ea6eed1c30401516499cff8aae24857ce9f2f8..99445c6e534adda9644a7019757b033bec4050f5 100644 --- a/core/adapter/identifier/java_identifier_listener.go +++ b/core/adapter/identifier/java_identifier_listener.go @@ -59,6 +59,8 @@ func (s *JavaIdentifierListener) EnterClassDeclaration(ctx *parser.ClassDeclarat } } } + + currentMethod = models.NewJMethod() } func (s *JavaIdentifierListener) ExitClassBody(ctx *parser.ClassBodyContext) { @@ -215,8 +217,7 @@ func (s *JavaIdentifierListener) EnterExpression(ctx *parser.ExpressionContext) statementCtx := ctx.GetParent().(*parser.StatementContext) firstChild := statementCtx.GetChild(0).(antlr.ParseTree).GetText() if strings.ToLower(firstChild) == "return" { - hasNull := strings.Contains(ctx.GetText(), "null") - currentMethod.IsReturnNull = hasNull + currentMethod.IsReturnNull = strings.Contains(ctx.GetText(), "null") } } } diff --git a/core/domain/evaluate/analyser_test.go b/core/domain/evaluate/analyser_test.go index ce896fcf8f73ebfb431d724669653d0a29d88e46..88dc31cca772a70686a5e214cd257a08dd8b0f67 100644 --- a/core/domain/evaluate/analyser_test.go +++ b/core/domain/evaluate/analyser_test.go @@ -81,5 +81,5 @@ func TestNullPointException(t *testing.T) { analyser := NewEvaluateAnalyser() result := analyser.Analysis(callNodes, identifiers) - g.Expect(result.Nullable.Items[0]).To(Equal("nonnull.Name.orElseNull")) + g.Expect(len(result.Nullable.Items)).To(Equal(2)) } diff --git a/core/domain/evaluate/evaluator/null_exception.go b/core/domain/evaluate/evaluator/null_exception.go index 45ac7b33bc7119ede12bb684d8ab9d67a9697523..e8999d003b85a9e44406d96261f4631a49e91a60 100644 --- a/core/domain/evaluate/evaluator/null_exception.go +++ b/core/domain/evaluate/evaluator/null_exception.go @@ -12,13 +12,16 @@ func (NullPointException) Evaluate(*EvaluateModel, models.JClassNode) { } func (n NullPointException) EvaluateList(evaluateModel *EvaluateModel, nodes []models.JClassNode, nodeMap map[string]models.JClassNode, identifiers []models.JIdentifier) { - var nullableList []string + var nullableList []string = nil for _, ident := range identifiers { for _, method := range ident.Methods { - for _, annotation := range method.Annotations { - if annotation.QualifiedName == "Nullable" { - methodPath := ident.Package + "." + ident.ClassName + "." + method.Name - nullableList = append(nullableList, methodPath) + if method.IsReturnNull { + nullableList = append(nullableList, buildMethodPath(ident, method)) + } else { + for _, annotation := range method.Annotations { + if annotation.QualifiedName == "Nullable" { + nullableList = append(nullableList, buildMethodPath(ident, method)) + } } } } @@ -26,3 +29,7 @@ func (n NullPointException) EvaluateList(evaluateModel *EvaluateModel, nodes []m evaluateModel.Nullable.Items = nullableList } + +func buildMethodPath(ident models.JIdentifier, method models.JMethod) string { + return ident.Package + "." + ident.ClassName + "." + method.Name +}