Skip to content

Commit

Permalink
Refactor logic to guess type fullname (#42)
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhu Subramanian <prabhu@appthreat.com>
  • Loading branch information
prabhu authored Dec 19, 2023
1 parent 54d4fa5 commit 0a0135a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion meta.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set version = "1.0.9" %}
{% set version = "1.0.10" %}

package:
name: chen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.7
sbt.version=1.9.8
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <cloud@appthreat.com>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 0a0135a

Please sign in to comment.