aspose file tools*
The moose likes Java in General and the fly likes Using annotation to put unique value in output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using annotation to put unique value in output" Watch "Using annotation to put unique value in output" New topic
Author

Using annotation to put unique value in output

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Can you use annotation to put a unique, machine readable value into a .class file? By unique I mean for each compilation without any source changes.
This is for the lazy/forgetful programmer that doesn't update his source with a String to id the class file. By putting a unique value into the .class file, if that file is moved around, it can always be identified by checking/comparing the value.

The Tutorial give some hints but doesn't have a good example:


Thanks,
Norm
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
System.currentTimeMillis()??

Of course that will cause confusion if you have two computers whose clocks are not synchronised.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Thanks for the response.
Since my posting I've found references that say that only String and int constants can be placed in annotations.

Do you have a working code sample that would show the use of
System.currentTimeMillis();
in an annotation?

It's not important what the value in the class file is as long as it is somewhat unique. It would be used to be able easily to determine if two remotely located class files are the same. Is my client using the same class file as I am? Comparing the id would quickly answer that.

Here's a code example I found:
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Time is a bad thing, for the clock sync issues raised above. Use CVS or SVN and have it fill in the $Revision$ value
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
The value of the clock is not important. Uniqueness is the issue. I assume that the value of the clock will be different every time I compile a program.

Getting more software is not a workable solution. I'd like to keep it as simple as possible.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Originally posted by Norm Radder:
Getting more software is not a workable solution. I'd like to keep it as simple as possible.

Are you saying you aren't using source control already?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Well, yes and no. It's something that I've written and is mostly just for backup.
This question is like, how do I ... or how does this work.

[Removed useless comment that was border line not nice - GDB]
[ August 24, 2008: Message edited by: Gregg Bolinger ]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Norm Radder:
The value of the clock is not important. Uniqueness is the issue. I assume that the value of the clock will be different every time I compile a program.


If you really need it to be unique, you can't rely on a clock time. You may have a really fast computer that doesn't resolve to unique times.

The $Revision$ hack works at compile time. If you want it at run time, you can store a value in a file on the disk, and read the file at program startup.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Originally posted by Pat Farrell:
The $Revision$ hack works at compile time. If you want it at run time, you can store a value in a file on the disk, and read the file at program startup.

It could work at runtime too:

You'll just have to make sure to compile it after getting the file with the revision filled in.
[ August 25, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1

If you really need it to be unique, you can't rely on a clock time. You may have a really fast computer that doesn't resolve to unique times.

Not sure what this means. If I get the clock time and store it in a class file when the class file is compiled. Won't the value stored ALWAYS be different from the value stored in a class file the next time I compile it? There is no way I could compile the program, copy the resulting class file somewhere and compile the program again with the clock times for the two compiles to be the same. I'm not fast enough.
compile it after getting the file with the revision filled in.

Getting the revision filled in is what I was looking to have happen automatically by using the annnotation runtime feature.
If I take care to fill in the revision, I might as well change the source when I am editting it and this whole problem goes away.

The problem is, I forget/am lazy and wanted an automatic way to put a unique value into a class file so that when talking to a client with a problem I know which class file he has and would be able to get the same one on my system to test with. The way I'd know I had the same class file was because of the unique ID that was in the file.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Norm Radder:

Getting the revision filled in is what I was looking to have happen automatically by using the annnotation runtime feature.
If I take care to fill in the revision, I might as well change the source when I am editting it and this whole problem goes away.


And the the value you give an Annotation must be static. The Revision hack they are referring to is a feature of some CVS repositories. It will replace the value of "$Revision$" with an actual revision number for you - exactly what you want to do.

The problem is that it works on the source code. You have to put the source code into the repository, then take it out of the repository and then compile it. And to keep yourself from having to re-enter the $Revision$ string all the time you would have to maintain different local copies of the code on your development machine.

You could, of course, write a small application that does the same thing and use it as part of an Ant build script.

Originally posted by Norm Radder:

The problem is, I forget/am lazy and wanted an automatic way to put a unique value into a class file so that when talking to a client with a problem I know which class file he has and would be able to get the same one on my system to test with. The way I'd know I had the same class file was because of the unique ID that was in the file.


You really should be using a code repository.


Steve
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

I echo the comments about source control. Building your own is an accident waiting to happen.

Also, are you distributing the class files loose? If not, why can't you just put a version.txt file in the jar?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Thanks to everyone for their input.
I was looking for a preprocesing capacity in java that's not there.
I think I can use the last mod dates of the class files in the jar file to give me the info I want.

I'm a one man shop and my clients are friends that use what I write. A CVS is a bit too much for my needs.

Thanks again,
Norm
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Norm Radder:
Not sure what this means. If I get the clock time and store it in a class file when the class file is compiled. Won't the value stored ALWAYS be different from the value stored in a class file the next time I compile it? There is no way I could compile the program, copy the resulting class file somewhere and compile the program again with the clock times for the two compiles to be the same


You are making a gross assumption about the length of time required to do your bunch of stuff. This may work, but it is essentially setting up a race condition. Race conditions are hard to find, and hard to fix. So the general rule is: if you can avoid a potential race condition, do it.

Your "i'm not fast enough" may be true today, but later, when you get faster computers and get tired of typing stuff, you will write a script to automate it, and then you may trigger the race condition.


Originally posted by Norm Radder:

The problem is, I forget/am lazy and wanted an automatic way to put a unique value into a class file so that when talking to a client with a problem I know which class file he has and would be able to get the same one on my system to test with. The way I'd know I had the same class file was because of the unique ID that was in the file.


That is exactly what $Revision$ in SVN and CVS are for. Exactly. And even a one man shop should be using SVN or CVS. I've been doing it for years. Its easy and once you have it, you will never consider working without it.

If you don't have one, get the latest SVN (subversion) it runs on all important OS, its free, open source, and can save your butt. Just do it.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Norm Radder:

I'm a one man shop and my clients are friends that use what I write. A CVS is a bit too much for my needs.


That doesn't match my experience. I'm using SVN at home for even the smallest hobby project. Wouldn't want to work without it.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Thanks again for your responses.
Some how I don't think you understand what I want to do: Given two copies of class file for the same java class, how can I easily test to see if they are copies. Ie were generated in a specific, single compile.

You are making a gross assumption about the length of time required to do your bunch of stuff. This may work, but it is essentially setting up a race condition.


I don't know. It usually takes several seconds for me to make a change to a source file and compile it. Can't imagine every doing it in less than one CPU clock cycle.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Originally posted by Norm Radder:
Given two copies of class file for the same java class, how can I easily test to see if they are copies. Ie were generated in a specific, single compile.
You could use a file-comparison utility to see if they are byte-for-byte equivalent. However that will tell you that two classes are the same if all the bytes are the same, whereas they might have been created three weeks apart from source code which had not changed. It's not clear how your requirements apply to that situation.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Its hard to use a file-comparison utility over the telephone or via email when communicating with a client that has your programs and is describing a problem.
My quest was for an easy way to id a class file. It looks like the easiest way is the last mod date of the class file in the jar file. Most people have winzip which will display that info. QES
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Originally posted by Norm Radder:
Given two copies of class file for the same java class, how can I easily test to see if they are copies. Ie were generated in a specific, single compile.


I don't understand why you would care if it was compiled at a specific time. I care that the source was the same, but I trust that the compiler will generate proper code if my source code is right.

I can see caring that the bytecode is the same or not. You can trivially run something like md5sum or shasum, or call an API to calculate the hash.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Sorry, I'm unable to communicate what I'm looking for.
why you would care if it was compiled at a specific time

I don't care when it was compiled. I want an easy way to determine if a file on my computer is the same as a version of the file on another computer. The Last Mod date seems the easiest.

A client calls up and says a verion of my code is not working. I ask what version and ask him to look at the last mod date. When I get that I will be able to find in my archive the same code on my system to be able to test for his problem.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Norm Radder:
Sorry, I'm unable to communicate what I'm looking for.

I don't care when it was compiled. I want an easy way to determine if a file on my computer is the same as a version of the file on another computer. The Last Mod date seems the easiest.

A client calls up and says a verion of my code is not working. I ask what version and ask him to look at the last mod date. When I get that I will be able to find in my archive the same code on my system to be able to test for his problem.


Does the client have your source code? If not, they aren't going to be able to look at the .class file (byte code) and get your version number anyway. You are chasing the wrong method here.

I'll say it again just so maybe you hear it this time.

1. Use version control
2. deploy builds to your clients
3. tag each build in your version control system
4. supply a version.txt with each tag's revision #
5. When a client calls you with a problem get the revision from the version.txt file.
6. checkout that revision/tag from your version control system for debugging.

Look, not only does this completely solve your problem but its standard practice should you ever get out of "lone developer" phase.


GenRocket - Experts at Building Test Data
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
able to look at the .class file

Very easily done with Winzip or XP (by renaming .jar to .jar.zip) with the class file in a jar file.

WAY WAY WAY TOO MUCH other software. I'd like a simple, easy system. Last mod date is all I need.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Norm Radder:
Very easily done with Winzip or XP (by renaming .jar to .jar.zip) with the class file in a jar file.


That's just nonsense. What if I'm not running Windows? I know these are your friends so you probably have a very controlled environment but this is just bad bad practice.

Originally posted by Norm Radder:
WAY WAY WAY TOO MUCH other software.


It's one more piece of software. Oh well, I promise you this will come back to haunt you. Bookmark this thread for when that happens so you can remember what needs to be done.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Thanks to everyone for their time and thoughts.
I'll let this die here.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using annotation to put unique value in output