Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Facilitate analysis of JavaParser AST by Scala pattern matching

License

NotificationsYou must be signed in to change notification settings

Portfoligno/JavaParser-for-Scala

Repository files navigation

JavaParser for Scala

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.

Project setup

Please refer to instructions onJitPack.

sbt

resolvers+="jitpack" at"https://jitpack.io"libraryDependencies+="io.github.portfoligno"%"javaparser-for-scala"%VERSION

Gradle (Kotlin DSL)

repositories {  maven(url="https://jitpack.io")}dependencies {  implementation("io.github.portfoligno:javaparser-for-scala:$VERSION")}

Usage

// 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 _=>}

[8]ページ先頭

©2009-2025 Movatter.jp