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

Ö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>








22 Ocak 2024 Pazartesi

Project Panama - Bridge The Gap Between Java And Native Code

Giriş
Açıklaması şöyle
Project Panama represents a significant advancement in the Java ecosystem, primarily focusing on enhancing Java’s interaction with foreign APIs, particularly those written in languages like C and C++. Traditionally, Java used the Java Native Interface (JNI) to invoke foreign functions, but JNI had several limitations. Project Panama addresses these by removing the need to write native code wrappers in Java, replacing the ByteBuffer API with a more advanced Memory API, and introducing a secure, memory-efficient method to invoke native code from Java. The project comprises several key components, including the Foreign-Function and Memory API, the Vector API, and the JExtract tool​​.

19 Ocak 2024 Cuma

JEP-401 - Project Valhalla - Improve Java’s Memory Efficiency

Giriş
Java'nın kullandığı Memory Model özellikle Integer[] gibi yapılarda işlemcinin israf edilmesine sebep oluyor. Bunu gösteren bir şekil burada

Sebebi ise şöyle
Accessing the value of an Integer (non-primitive) includes an extra memory access
Çünkü her bir Integer için kullanılan Metadata nesnesi bellekte rastgele bir yerde duruyor
Bu da günümüzdeki modern işlemcilerde CPU cache israfına sebep oluyor

Project Valhalla
İlk fikir 2014 yılında ortaya atıldı. Açıklaması şöyle
... give the user the ability to flatten his objects and enjoy the performance of primitives in exchange for some limitations ...
Kod olarak şöyle
// Before
public class Point {
  private final int x;
  private final int y;
}

// After
public primitive class Point {
  private final int x;
  private final int y;
}



7 Ocak 2024 Pazar

GSON Kullanımı

Maven
Şu satırı dahil ederiz
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.9</version>
</dependency>
Gson Sınıfı
Gson Sınıfı toJson() ile nesneyi JSON String'e çevirir. fromJson() ile JSON String'den nesneye çevirir

@SerializedName Anotasyonu
@SerializedName Anotasyonu yazısına taşıdım

@Expose Anotasyonu
@Expose Anotasyonu yazısına taşıdım

@Since Anotasyonu
@Since Anotasyonu yazısına taşıdım

Gson @Since Anotasyonu - Specify Version Information For Fields

Örnek
Şöyle yaparız
import com.google.gson.annotations.Since;
import com.google.gson.annotations.Until;

public class Product {
    @Since(1.0)
    private String name;
    @Until(2.0)
    private double price;

    // getters and setters
}


2 Ocak 2024 Salı

Hikari API HikariDataSource Sınıfı

Giriş
Şu satırı dahil ederiz
import com.zaxxer.hikari.HikariDataSource;
constructor
Örnek
Hikari sayfasındaki örnek şöyle
HikariConfig config = ...
HikariDataSource ds = new HikariDataSource(config);
isClosed metodu
Sınıf şöyle
public class HikariDataSource extends HikariConfig implements DataSource, Closeable {
  ...
  private final AtomicBoolean isShutdown = new AtomicBoolean();
  ...
}
Metodun içi şöyle
public boolean isClosed() {
  return isShutdown.get();
}

1 Ocak 2024 Pazartesi

JEP 359 record ve Builder

Örnek
Elimizde şöyle bir kod olsun
public record FilmWithRecord(String title, String director, int releaseYear) {
  
public static class Builder {
  private String title;
  private String director;
  private int releaseYear;

  public Builder title(String title) {
   this.title = title;
   return this;
  }

  public Builder director(String director) {
   this.director = director;
   return this;
  }

  public Builder releaseYear(int releaseYear) {
   this.releaseYear = releaseYear;
   return this;
  }

  public FilmWithRecord build() {
   return new FilmWithRecord(title, director, releaseYear);
  }
  }
}
Kullanmak için şöyle yaparız
// Example of using the builder:
FilmWithRecord film = new FilmWithRecord
 .Builder()
 .title("The Dark Knight")
 .director("Christopher Nolan")
 .releaseYear(2008)
 .build();