Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add custom metadata to .class files?

We have used liquibase at our company for a while, and we've had a continuous integration environment set up for the database migrations that would break a job when a patch had an error.

An interesting "feature" of that CI environment is that the breakage had a "likely culprit", because all patches need to have an "author", and the error message shows the author name.

If you don't know what liquibase is, that's ok, its not the point.

The point is: having a person name attached to a error is really good to the software development proccess: problems get addressed way faster.

So I was thinking: Is that possible for Java stacktraces?

Could we possibly had a stacktrace with peoples names along with line numbers like the one 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)

That kind of information would have to be pulled out from a SCM system (like performing "svn blame" for each source file).

Now, forget about trashing the compilation time for a minute: Would that be even possible? To add metadata to class files like that?

like image 854
Tony Lâmpada Avatar asked May 22 '12 21:05

Tony Lâmpada


People also ask

Can I add custom metadata to a file?

In addition to seven default metadata fields you can easily add your own custom metadata fields as well. You can do so by applying the custom metadata fields on folder level, so that the same fields are automatically assigned to all files in that folder — and optionally its sub folders.

Are metadata for classes?

A MetaData object can be used to describe the types and properties of the columns in a ResultSet or the existing schema objects in the database. It also provides information about the database as a whole. The parameter types for objects are listed in Table 10-24.

What do .class files contain?

A CLASS file is a compiled . JAVA file created by the Java compiler. It contains bytecode, which is binary program code that is executable when run by a Java Virtual Machine (JVM).


1 Answers

In principle you can add custom information to .class files (there's and attribute section where you can add stuff). You will have to write your own compiler/compiler extension to do so. There is no way to add something to your source code that then will show up in the class file. You will also have major problems in practice:

  1. The way stack-traces a built/printed is not aware of anything you add to the class file. So if you want this stuff printed like you show above, you have to hack some core JDK classes.
  2. How much detail do you want? The last person who committed any change to a given file? That's not precise enough in practice, unless files are owned by a single developer.
  3. Adding "last-committed-by" information at a finer granularity, say per method, or even worse, per line will quickly bloat your class file (and class files are limited in size to 64K)

As a side note, whether or not blaming people for bugs helps getting bugs fixed faster strongly depends on the culture of the development organization. Make sure you work in one where this helps before you spend a lot of time developing something like this.

like image 90
Jochen Avatar answered Oct 25 '22 03:10

Jochen