简体   繁体   中英

Java write Code that is just run at compile time?

is it possible to write a "maven plugin" or something like that which changes the .java at compile time?

For example:

@Entity
... class ... {
  @Id
  long id
}

I would like to comment out all the Annotations and Create a ORM-Mapping.xml / Persistence according to the entitys

The annotations are mostly based on the configuration files which enable/disable their recognition.

Use the Spring profiles to distinguish the configuration classes. If the special profile will appear, no configuration classes will be recognized. The solution requires to make all the configurations configurable according to the Spring profiles.

@Configuration
@Profile("noAnnotations")
public class SomeConfigurationClass

It's possible. Project lombok modifies compiled code however there's no exposed API to do that. (See How does lombok work? ) Which is what you want if you have to strip annotations. Disabling all annotations at runtime is probably not reasonable. If you just want to generate something from annotations found in compiled classes that is much simpler

What you describe sounds like you're looking for something like a C/C++ preprocessor/templating - feature?

Maybe Annotation Processing can help you https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html

Apart from that - inferring a persistence.xml from your entities annotations is something the Spring Framework does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM