10 Mart 2024 Pazar

OpenRewrite

Giriş
OpenRewrite bir static kod analiz aracı. Diğer araçlardan farklı olarak sadece rapor üretmiyor aynı zamanda kodu da değiştirebiliyor.

1. dryRun Goal
target/rewrite dizini altında bir patch dosyası oluşturur

2. run Goal
Kodları direkt değiştirir

3. Recipe Listesi

3.1 CommonStaticAnalysis Recipe
Sanırım en işe yarayan recipe'lerden bir tanesi. Bur recipe ile yapılabilecek işlerin listesi burada

Benim hazelcast kodunda çalıştırdıklarım şöyle
  1. org.openrewrite.staticanalysis.UseDiamondOperator
  2. org.openrewrite.staticanalysis.UnncessaryThrows
  3. org.openrewrite.staticanalysis.NoRedundantJumpStatements
  4. org.openrewrite.staticanalysis.NestedEnumsAreNotStatic
  5. org.openrewrite.staticanalysis.RemoveRedundantTypeCast
  6. org.openrewrite.staticanalysis.LambdaBlockToExpression. EE için koşmadım
  7. org.openrewrite.staticanalysis.UnnecessaryExplicitTypeArguments
  8. org.openrewrite.staticanalysis.SimplifyTernaryRecipes
  9. org.openrewrite.staticanalysis.ReplaceRedundantFormatWithPrintf
  10. org.openrewrite.staticanalysis.InstanceOfPatternMatch.  EE için koşmadım. OS için yarım koştum
  11. org.openrewrite.staticanalysis.JavaApiBestPractices
  12. org.openrewrite.staticanalysis.PrimitiveWrapperClassConstructorToValueOf

Örnek
Şöyle yaparız
<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>5.23.1</version>
  <configuration>
    <activeRecipes>
      <recipe>org.openrewrite.staticanalysis.CommonStaticAnalysis</recipe>
    </activeRecipes>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.openrewrite.recipe</groupId>
      <artifactId>rewrite-static-analysis</artifactId>
      <version>1.3.1</version>
    </dependency>
  </dependencies>
</plugin>
Eğer proje çok büyükse sadece bazı şeyleri çalıştırmak için proje kök dizininde bir tane rewrite.yml dosyası oluştururuz.  Şöyle yaparız. Eğer proje multi-module ise yine en tepedeki dizine koymak lazım
---
type: specs.openrewrite.org/v1beta/recipe
name: orcun.CommonStaticAnalysis
displayName: Common static analysis issues
description: Resolve common static analysis issues discovered through 3rd party tools.
recipeList:
  - org.openrewrite.staticanalysis.UseDiamondOperator
pom.xml şöyle olur.
<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>5.23.1</version>
  <configuration>
    <activeRecipes>
      <recipe>orcun.CommonStaticAnalysis</recipe>
    </activeRecipes>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.openrewrite.recipe</groupId>
      <artifactId>rewrite-static-analysis</artifactId>
      <version>1.3.1</version>
    </dependency>
  </dependencies>
</plugin>








Hiç yorum yok:

Yorum Gönder