6

I have a large number of Java classes using JPA property access instead of field access, and converting them to use field access (moving the annotations from getters to fields) is time consuming. Re-generating them from database tables is also not an option.

So I was looking at using IDEA's structural search and replace to try move the annotations automatically. I'm wanting to change a class that looks like this:

@Entity
@Table
public class Make {

    private Long id;
    private String makeCode;
    ...other properties, with more involved JPA annotations

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 10, unique = true)
    public String getMakeCode() {
        return makeCode;
    }

    public void setMakeCode(String makeCode) {
        this.makeCode = makeCode;
    }
    ...
}

to one that looks like this:

@Entity
@Table
public class Make {

    @Id
    @GeneratedValue
    private Long id;

    @Column(length = 10, unique = true)
    private String makeCode;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMakeCode() {
        return makeCode;
    }

    public void setMakeCode(String makeCode) {
        this.makeCode = makeCode;
    }
    ...
}

From there I'll be able to do additional refactoring, but I'm finding that moving the annotations manually is very time-consuming.

I've played around with structural search & replace, but its quite dense and lacking documentation, so I'd appreciate any tips on how to configure the different templates.

I imagine it would be something like:

find all methods starting with "get" that are annotated with at least one javax.persistence.* annotation, along with a matching private field, and replace with the same field and method, but move the annotations from method to field.

I'm just struggling to put that into the structural search expressions.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Barry Pitman
  • 3,087
  • 1
  • 24
  • 32
  • This will not be a built in action I doubt. Of course you can always write your plugins that have such actions. Or use something like https://github.com/dkandalov/live-plugin to help satisfy your needs. – aristotll Aug 13 '17 at 11:03

1 Answers1

0

It's been 5 years since this has been asked, and perhaps there is a solution to move your annotations from method level to field level.

It will involve Lombok and if you're OK with that, then the latest IntelliJ can refactor that for you under Refactor->Lombok->Default @Data

Although, we believe the best practice is to leave the annotations at the method level.