- Notifications
You must be signed in to change notification settings - Fork0
Facilitate analysis of JavaParser AST by Scala pattern matching
License
Portfoligno/JavaParser-for-Scala
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This library implements extractor objects along withapply
factory methodsfor theJavaParser AST.
For example:
// package jp4s.ast.statementtypeIf=IfStmt// com.github.javaparser.ast.stmt.IfStmtobjectIf {defapply(condition:Expression,thenStmt:Statement,elseStmt:Optional[Statement]):If=newIf(condition, thenStmt, elseStmt.orElseNull)defunapply(i:If):Some[(Expression,Statement,Optional[Statement])]=Some((i.getCondition, i.getThenStmt, i.getElseStmt))}
Generally, we prefer a shortened name of the factory objectto improve code conciseness for matching with multi-level patterns.There are suffices likeStmt
andExpr
in the original AST classesthat are dropped to form shorter names.You may see below for a detailed example.
Type aliases under the same namesare also provided together as 1-to-1 mapping to the original AST typesin a manner to minimize issues in importing factory objectswhile it is still necessary to refer to the original AST types.
Please refer to instructions onJitPack.
resolvers+="jitpack" at"https://jitpack.io"libraryDependencies+="io.github.portfoligno"%"javaparser-for-scala"%VERSION
repositories { maven(url="https://jitpack.io")}dependencies { implementation("io.github.portfoligno:javaparser-for-scala:$VERSION")}
// Join variable declarations and assignmentscompilationUnit.walk {caseblock:Block=>valvariableTypes= mutable.Map[String,Type]() block.getStatements.removeIf {// Search for variables initializing as `null`caseExecute(Variables(JavaList(),// No modifiersJavaList(),// No annotations t,// The variable typeNonEmptyJavaList(// Only a variable is declared in the statementVariable(JavaList(),// No array brackets on the name side lhs,// The variable namePresent(NullLiteral())// Initializing as null ) ) ))=> variableTypes+= lhs-> t// Store the variable type for later usetrue// Search for variable reassignmentscase execute@Execute(Assign.Plain(Name(lhs), rhs))=>// Turn it into a declaration execute.setExpression(Variables(JavaList(),JavaList(), variableTypes(lhs),// Use the stored variable typeNonEmptyJavaList(Variable(JavaList(), lhs,// Keep the variable namePresent(rhs)// Keep the value expression ) ) ))false// Ignore anything elsecase _=>false }case _=>}
About
Facilitate analysis of JavaParser AST by Scala pattern matching