From 0a0135a2562bb4e000a2d351262ed172e3b19e62 Mon Sep 17 00:00:00 2001 From: prabhu Date: Tue, 19 Dec 2023 17:52:37 +0000 Subject: [PATCH] Refactor logic to guess type fullname (#42) Signed-off-by: Prabhu Subramanian --- build.sbt | 2 +- codemeta.json | 2 +- meta.yaml | 2 +- .../javasrc2cpg/passes/AstCreator.scala | 25 +++++++++------- .../querying/AnnotationTests.scala | 30 +++++++++++++++++++ project/build.properties | 2 +- pyproject.toml | 2 +- 7 files changed, 49 insertions(+), 16 deletions(-) diff --git a/build.sbt b/build.sbt index 7f1ab361..cbba33c1 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "chen" ThisBuild / organization := "io.appthreat" -ThisBuild / version := "1.0.9" +ThisBuild / version := "1.0.10" ThisBuild / scalaVersion := "3.3.1" val cpgVersion = "1.4.22" diff --git a/codemeta.json b/codemeta.json index 18050135..e9dc75ab 100644 --- a/codemeta.json +++ b/codemeta.json @@ -7,7 +7,7 @@ "downloadUrl": "https://github.com/AppThreat/chen", "issueTracker": "https://github.com/AppThreat/chen/issues", "name": "chen", - "version": "1.0.9", + "version": "1.0.10", "description": "Code Hierarchy Exploration Net (chen) is an advanced exploration toolkit for your application source code and its dependency hierarchy.", "applicationCategory": "code-analysis", "keywords": [ diff --git a/meta.yaml b/meta.yaml index a78fcf4a..08c4032f 100644 --- a/meta.yaml +++ b/meta.yaml @@ -1,4 +1,4 @@ -{% set version = "1.0.9" %} +{% set version = "1.0.10" %} package: name: chen diff --git a/platform/frontends/javasrc2cpg/src/main/scala/io/appthreat/javasrc2cpg/passes/AstCreator.scala b/platform/frontends/javasrc2cpg/src/main/scala/io/appthreat/javasrc2cpg/passes/AstCreator.scala index 2f44709d..d0b30ae0 100644 --- a/platform/frontends/javasrc2cpg/src/main/scala/io/appthreat/javasrc2cpg/passes/AstCreator.scala +++ b/platform/frontends/javasrc2cpg/src/main/scala/io/appthreat/javasrc2cpg/passes/AstCreator.scala @@ -777,7 +777,7 @@ class AstCreator( val typeFullNameWithoutGenericSplit = typeInfoCalc .fullName(v.getType) .orElse(scope.lookupType(v.getTypeAsString)) - .getOrElse(s"${Defines.UnresolvedNamespace}.${v.getTypeAsString}") + .getOrElse(guessTypeFullName(v.getTypeAsString)) val typeFullName = // Check if the typeFullName is unresolved and if it has generic information to resolve the typeFullName if @@ -979,8 +979,8 @@ class AstCreator( case Success(resolvedType) => typeInfoCalc.fullName(resolvedType) resolvedTypeOption.orElse(exprNameFromStack(expr)) - private def astForAnnotationExpr(annotationExpr: AnnotationExpr): Ast = - val fallbackType = annotationExpr.getNameAsString match + private def guessTypeFullName(initString: String): String = + initString match case x if Seq( "Override", @@ -991,11 +991,14 @@ class AstCreator( "Native" ).contains(x) => s"java.lang.$x" case y if y.startsWith("java.") => y - case _ => s"${Defines.UnresolvedNamespace}.${annotationExpr.getNameAsString}" - val fullName = expressionReturnTypeFullName(annotationExpr).getOrElse(fallbackType) - val code = annotationExpr.toString - val name = annotationExpr.getName.getIdentifier - val node = annotationNode(annotationExpr, code, name, fullName) + case _ => s"${Defines.UnresolvedNamespace}.${initString}" + + private def astForAnnotationExpr(annotationExpr: AnnotationExpr): Ast = + val fallbackType = guessTypeFullName(annotationExpr.getNameAsString) + val fullName = expressionReturnTypeFullName(annotationExpr).getOrElse(fallbackType) + val code = annotationExpr.toString + val name = annotationExpr.getName.getIdentifier + val node = annotationNode(annotationExpr, code, name, fullName) annotationExpr match case _: MarkerAnnotationExpr => annotationAst(node, List.empty) @@ -2261,7 +2264,7 @@ class AstCreator( val typeName = typeFullName .map(TypeNodePass.fullToShortName) - .getOrElse(s"${Defines.UnresolvedNamespace}.${variable.getTypeAsString}") + .getOrElse(guessTypeFullName(variable.getTypeAsString)) val code = s"$typeName $name = ${initializerAsts.rootCodeOrEmpty}" val callNode = newOperatorCallNode( @@ -2493,7 +2496,7 @@ class AstCreator( // A static field represented by a NameExpr must belong to the class in which it's used. Static fields // from other classes are represented by a FieldAccessExpr instead. scope.enclosingTypeDecl.map(_.name).getOrElse( - s"${Defines.UnresolvedNamespace}.$name" + guessTypeFullName(name) ) else NameConstants.This @@ -3395,7 +3398,7 @@ class AstCreator( .fullName(parameter.getType) .orElse(scope.lookupType(parameter.getTypeAsString)) .map(_ ++ maybeArraySuffix) - .getOrElse(s"${Defines.UnresolvedNamespace}.${parameter.getTypeAsString}") + .getOrElse(guessTypeFullName(parameter.getTypeAsString)) val evalStrat = if parameter.getType.isPrimitiveType then EvaluationStrategies.BY_VALUE else EvaluationStrategies.BY_SHARING diff --git a/platform/frontends/javasrc2cpg/src/test/scala/io/appthreat/javasrc2cpg/querying/AnnotationTests.scala b/platform/frontends/javasrc2cpg/src/test/scala/io/appthreat/javasrc2cpg/querying/AnnotationTests.scala index 4cef701c..ebe37e8e 100644 --- a/platform/frontends/javasrc2cpg/src/test/scala/io/appthreat/javasrc2cpg/querying/AnnotationTests.scala +++ b/platform/frontends/javasrc2cpg/src/test/scala/io/appthreat/javasrc2cpg/querying/AnnotationTests.scala @@ -316,4 +316,34 @@ class AnnotationTests extends JavaSrcCode2CpgFixture { } } } + + "lombok annotations" should { + lazy val cpg = code( + """ + |import lombok.Data; + | + |@Data + |public class UserLombokModel { + | private long id; + | private String firstName; + | private String lastName; + | private int age; + | private LocalDate createdDate; + | private LocalDate updatedDate; + | private String gender; + |} + | + |""".stripMargin) + + "test annotation node properties" in { + cpg.typeDecl.name("UserLombokModel").annotation.l match { + case List(data) => + data.name shouldBe "Data" + data.fullName shouldBe "lombok.Data" + data.code shouldBe "@Data" + data.lineNumber shouldBe Some(4) + case result => fail(s"Expected 1 annotations for UserLombokModel but got $result") + } + } + } } diff --git a/project/build.properties b/project/build.properties index e8a1e246..abbbce5d 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.9.7 +sbt.version=1.9.8 diff --git a/pyproject.toml b/pyproject.toml index 10397a3c..014ce5fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "appthreat-chen" -version = "1.0.9" +version = "1.0.10" description = "Code Hierarchy Exploration Net (chen)" authors = ["Team AppThreat "] license = "Apache-2.0"