jeudi 29 octobre 2015

How to forbid create Object via reflection?

Prologue

I have a class :

public final class Session {

    private int userId;
    private String token;

    Session(int userId, String token) {
        this.userId = userId;
        this.token = token;
    }

    public String getToken() {
        return token;
    }

    public int getUserId() {
        return userId;
    }
}

As you see, we can create object via reflection.

  • I know that i can forbid access to methods via reflection with final modificator. But, this modificator can not be applicable to constructor.

  • I know that i can forbid access to constructor via private or package-private modificators.

The goals

  • Forbid creating of object even via reflection way to preserve security.
  • Make access to fill object fields only package-private.

P.S. Maybe it can be complete via OOP/OOD way?





Aucun commentaire:

Enregistrer un commentaire