• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using annotation to put unique value in output

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.currentTimeMillis()??

Of course that will cause confusion if you have two computers whose clocks are not synchronised.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to everyone for their time and thoughts.
I'll let this die here.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic