mardi 5 octobre 2021

How to resolve this reflection error? java.lang.reflect.Field.get(Field.java:393)

I am trying to change the value of @ColumnTransformer at runtime so that I can store my encryption keys somewhere else on a server. How can I achieve this ?

Below is my main code:

List<Field> fields = Arrays.asList(User.class.getDeclaredFields());
for(Field field:fields)
{
    if(field.toString().endsWith("secretfield"))
    {
        System.out.println(field);
        List<Annotation> annotations = Arrays.asList(field.getDeclaredAnnotations());
        for(Annotation annotation : annotations)
        {
            if(annotation.annotationType().toString().endsWith("ColumnTransformer"))
            {
                Annotation newAnnotation = new ColumnTransformer(){
                    @Override
                    public String forColumn() {
                        return "secretfield";
                    }

                    @Override
                    public String read(){
                        return "This is Sparta";
                    }

                    @Override
                    public String write(){
                        return "This is also sparta";
                    }

                    @Override
                    public Class<? extends Annotation> annotationType() {
                        return annotation.annotationType();
                    }
                };
                Field field1 = User.class.getDeclaredField("secretfield");
                field1.setAccessible(true);
                Map<Class<? extends Annotation>, Annotation> annotations1 = (Map<Class<? extends Annotation>, Annotation>) field1.get(User.class);
                annotations1.put(ColumnTransformer.class, newAnnotation);
            }
        }
    }
}

And below is my Pojo:

package Model;

import org.hibernate.annotations.ColumnTransformer;


import javax.persistence.*;
import javax.sql.rowset.serial.SerialBlob;
import java.sql.Blob;
import java.io.Serializable;

@Entity(name = "user")
@Table(name = "user")
public class User implements Serializable {

    @Column(name = "secretfield", nullable = true)
    @ColumnTransformer(read = "TEST")
    private String secretfield;

    public String getSecretfield() {
        return secretfield;
    }

    public void setSecretfield(String secretfield) {
        this.secretfield = secretfield;
    }
}

I am getting this error:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.String field Model.User.secretfield to null value
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:393)
    at Runner.main(Runner.java:65)




Aucun commentaire:

Enregistrer un commentaire