Can I add custom metadata to .class files?

We used Liquibase in our company for a while, and we had a continuous integration environment configured to migrate the database, which would break the job when the patch had an error.

An interesting “feature” of this CI environment is that the gap had a “likely culprit,” because all patches must have an “author”, and the author’s name is displayed in the error message.

If you do not know what the educational base is, this is normal, this is not the point.

The point is: the presence of the person name attached to the error is really suitable for the software development process: problems are addressed faster faster .

So I thought: is this possible for Java stacks?

Is it possible we would have a stacktrace with the names of people along with line numbers, as shown below?

java.lang.NullPointerException at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372:john) at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121:mike) at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232:bob) at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173:bob) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87:bob) at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862:john) 

Such information should be output from the SCM system (for example, executing "svn blame" for each source file).

Now, forget that compilation time will be destroyed: is this possible? How to add metadata to class files?

+6
source share
4 answers

Typically, this feature can be implemented on top of the version control system. You need to find out the version of your file in your version control system, then you can call the blame / annotate command to get information about who changed each person's line. You do not need to store this information in the class file if you can identify the revision of each class that you are deploying (for example, you are only deploying a specific tag or label).

If you do not want to go into version control when examining a stack trace, you can save string annotation information to a class file, for example. using the post post-class processor during assembly, which can add custom annotation at the class level (this is relatively trivial to implement using ASM ). Then, the logger that prints the stack trace can read this annotation at runtime, similarly showing the version of jar .

+1
source

In principle, you can add user information to .class files (there is also an attribute section where you can add material). To do this, you will need to write your own compiler / compiler extension. It is not possible to add something to the source code that will be displayed in the class file. In practice, you will also have serious problems:

  • The stack tracking method built / printed does not know anything that you add to the class file. Therefore, if you want to print this material as shown above, you will have to crack some of the core JDK classes.
  • How many parts do you want? The last one to make any change to this file? This is not accurate enough in practice if the files do not belong to the same developer.
  • Adding information with the “last message” in finer granularity, say, by the method or, even worse, your class file will be inflated quickly for each line (and class files are limited to 64 KB).

As a side note, regardless of whether people are blamed for errors, error correction is more dependent on the culture of the development organization. Make sure you work in one where it helps, before spending a lot of time developing something like that.

+3
source

One way to add added user information to class files is to use annotations in the source code. I don’t know how you would reliably put this information in the stack trace, but you could create a tool to extract it.

+1
source

As @theglauber correctly pointed out, you can use annotations to add custom metadata. Althougth, I'm not sure that you cannot get this information from your beans database and decorate your own exception manager.

0
source

Source: https://habr.com/ru/post/916402/


All Articles