Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
God, that's so cool! And it just goes and does what you want it to...

I get it - in one thread Swing does what it's supposed to do - hide the list on click - and in another thread my code runs. Well, there goes the mystery.

Adbot
ADBOT LOVES YOU

Volguus
Mar 3, 2009

supermikhail posted:

God, that's so cool! And it just goes and does what you want it to...

I get it - in one thread Swing does what it's supposed to do - hide the list on click - and in another thread my code runs. Well, there goes the mystery.

Actually, the code in the Runnable still runs in the Event Dispatch Thread, but just after every other event has been completed (in this case, the item selection event). Be careful to not abuse invokeLater, as weird stuff can happen.

ToxicFrog
Apr 26, 2008


rhag posted:

You don't have those guarantees on linux either. I personally have my jdks installed in /home/<user>/java/jdk-version . Now, I do set PATH, JAVA_HOME, etc. properly in my .bashrc to not have to deal with those issues, but I don't have to.

rrrrrrrrrrrt posted:

If your assumption on Linux is that java will be on the PATH, why can't you make the same assumption for Windows?

On linux, the user has probably installed it through the package manager, which I generally trust to set up paths correctly; if they're knowledgeable enough to install it by hand, I assume they're also knowledgeable enough to know how to use their hand-installed java to run a jar.

rhag posted:

If you want to keep things simple, and not go the jsmooth/launch4j way, you can make a bat file, in which you can check for the existence of the required variables, or the java executable. If that's not found, just open the browser and go to java.com . Then the users just have to install that. If they install it from the installer, it will be in the PATH and everything will be fine.

Tesseraction posted:

Looking through all my Windows environmental variables (User and System) I can't see Java on it anywhere; Java is installed, though, and java -jar works fine. I admit it's hardly a solution, but have you encountered a system where Java is installed but a call to java doesn't work?

This is exactly what I tried last time, and is the reason I'm in here using phrases like "waking nightmare" and trying desperately to avoid having to do it again. I started out assuming that if java was installed, it was in %PATH%. Then I got reports of systems where that didn't work, but where at least one of the %JAVA_*% environment variables was set properly. So I added checks for those, too. Then I got reports of systems where those weren't set either, but you could locate it through the registry. All of these systems, I note, had Java 1.5 or later installed via one an official installer.

The end result was 150 lines of batch file fuckery and there were still two or three users who had the JRE installed - as in it showed up in add/remove programs, the files were present in the filesystem, and double-clicking on jar files would run them - but for whom the batch file did not work. I mean, I guess they could have copied java from another system and set up the file associations manually and just lied about it, but why?

Using jsmooth/launch4j, if they work as advertised, is the simple way.

Adventure Pigeon
Nov 8, 2005

I am a master storyteller.

Gravity Pike posted:

I hate to tell you to RTFM, but Java has really good documentation. You can basically google any classname, and among the first few results will be a detailed description of the class, it's complete hierarchy, and a complete description of all of it's methods.

Alright, always found that stuff overwhelming, but it's time to sit down and learn it. Anyways, I did manage to get the program working and it is using multiple threads, but it's still too slow. I think it just comes down to the size of the dataset making this method unfeasible. Still, I learned a lot programming it. Thanks again for the help folks.

Pollyanna
Mar 5, 2005

Milk's on them.


Not specifically a Java question, but it's the language I'm working on at the moment, so I put it here.

I've been reading up on OOP. I think I get the idea behind it, so I wrote something up as an example of classes and objects. I made a list of elements you'd find in a basic RPG. Each line contains the type of class or object, the name of the class or object, and certain parameters of the class or object (in brackets). e.g.:

The superclass Items has one parameter: [amount]. The Weapons subclass of Items, however, has two parameters: [amount, power]. It inherits [amount] from Items (all Items have an amount), and innately has [power] (all Weapons have damage dealing power, but not all Items do).

Similarly, the Potions subclass only has the parameter [amount], inherited from Items. It has no inherent parameters. Certain Objects of class Potions, however, have the parameters [amount, bar, points], where [points] is how many points of which [bar] (HP or MP) is recovered upon using the item. Both [bar] and [points] are inherent parameters of the Objects Health and Mana.

On the other hand, the Object Water has no parameters either, as it is neither associated with HP or MP, nor restores any points. It has only an [amount] of Water potions held.

Here's the list of stuff:

code:
Mobiles (Superclass)
	Villagers (Class) [name]
		Baker (Subclass) [name, hat]
		Blacksmith (Subclass) [name, hat]
		Chief (Subclass) [name, hat]
	Monsters (Class) [name, HP]
		Rat (Subclass) [name, HP]
		Orc (Subclass) [name, HP]
		Dragon (Subclass) [name, HP]

Items (Superclass) [amount]
	Weapons (Class) [amount, power]
		Knife (Subclass) [amount, power]
		Dagger (Subclass) [amount, power]
		Sword (Subclass) [amount, power]
	Potions (Class) [amount]
		Health (Subclass) [amount, bar, points]
		Mana (Subclass) [amount, bar, points]
		Water (Subclass) [amount]
Do I have the basic idea of objects and classes down? I know it probably gets more complicated than this, but I want to make sure I've got it right before moving on.

vvvv Thanks, I changed it around. I don't think I've gotten to interfaces yet, but I'll get to it.

Pollyanna fucked around with this message at 21:23 on Sep 17, 2013

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

Almost. The items in your third level are also classes, don't forget. An object would be an instance of any of those classes, so Knife is a class, but if you give a knife to the player you would make an instance of the Knife class which would have values for its member variables (and the member variables of its superclasses.) This is where it gets tricky, since I don't think Classes are considered objects in OOP, yet most programming languages will have some method of obtaining an object representing a class in the system (and even modifying that class.)

The next concept you will likely read about will be interfaces and, if the text is Java oriented, abstract classes, which you may find better fit your modeling of an RPG.

Paolomania
Apr 26, 2006

Superclass and subclass are relationships that one class has to another, rather than a specific location in a class hierarchy. In your example as written (where 'Baker' is a class), Baker is a subclass of Villagers but then Villagers is in turn a subclass of Mobiles. Reciprocally, Mobiles is a superclass of Villagers and Villagers is in turn a superclass of Baker.

A note on terminology: usually when we discuss objects, the word 'member' is used to describe the data that belongs to that object and is kept around as long as that object exists. 'Parameter' is more typically used to describe data that is passed into a method (aka 'member function') and is kept around only for one call on that method. It can be confusing because members usually begin their lives as parameters passed into a constructor or method which then are saved by the object in members.

Also, I am inferring from your use of plural class names such as 'Mobiles' and 'Villagers' that singular things such as 'Baker' and 'Blacksmith' should really be objects (instances of 'Villagers' in this case) rather than classes themselves. If this is incorrect, consider renaming your various Villager subclasses to be plural in order to be consistent with your other naming conventions. So then you would have a class hierarchy something like:

class Mobiles -> class Villagers -> class Bakers -> object Alice, object Bob, object Cindy (all instances of Bakers)

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.
Is plural class names a convention that anyone actually follows? I mean, it's not Lists<Dog> bestDogsList = new ArrayLists<Dog>();. The only place you see plural class names are the static "Utility" classes in Commons/Guava, where they really want to put a bunch of static methods in the parent class. (ie Lists.newArrayListWithExepectedSize(10);)

Amarkov
Jun 21, 2010
Plural class names are a standard for precisely those static utility classes. It's definitely inappropriate to pluralize a class that's designed to be instantiated; "bestDogsList IS-A ArrayLists" wouldn't be right.

Paolomania
Apr 26, 2006

Gravity Pike posted:

Is plural class names a convention that anyone actually follows?

I was just speaking to the convention in the example along with a conjecture about Pollyanna's mental model of classes and objects. I generally don't use plural class names, and would more likely use plurals for collections, but even then I might go with "villagersList" over "villagers" if the container type wasn't likely to change.

pigdog
Apr 23, 2004

by Smythe

Pollyanna posted:

The superclass Items has one parameter: [amount]. The Weapons subclass of Items, however, has two parameters: [amount, power]. It inherits [amount] from Items (all Items have an amount), and innately has [power] (all Weapons have damage dealing power, but not all Items do).

Similarly, the Potions subclass only has the parameter [amount], inherited from Items. It has no inherent parameters. Certain Objects of class Potions, however, have the parameters [amount, bar, points], where [points] is how many points of which [bar] (HP or MP) is recovered upon using the item. Both [bar] and [points] are inherent parameters of the Objects Health and Mana.

On the other hand, the Object Water has no parameters either, as it is neither associated with HP or MP, nor restores any points. It has only an [amount] of Water potions held.
The [amount] doesn't really make sense and seems pretty unnecessary. A bottle of water is a bottle of water, it doesn't have or need an "amount" property. A villager might have an inventory of Item-s of which she has an amount, and we might be talking about the amount of water bottles in the whole gameworld, but the potion itself doesn't need it. [bar] and [points] are rather nebulous as well; are you sure you wouldn't a HealingPotion subclass with an amountHealed property or such?

A rule of thumb is that class inheritance is for sharing common implementations of functionality, but as far as behavior, you should take a good look at interfaces once your inheritance topology starts feeling limited and strange. A subclass can only extend one parent class, but can implement several interfaces and thus "be" many things at once. For example a Cloak and a Blacksmith can both implement Burnable and needn't have anything else in common rather than having implemented burn() or whatever the Burnable interface requires, at some point of their respective class hierarchy. Definitely read up and mess around with interfaces, they're more useful than they appear. :)

pigdog fucked around with this message at 15:34 on Sep 19, 2013

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
I've got writer's block.

This application that I want to store preferences for, I just realized that I have to start with something - I have to have a default path where I would store all the non-default paths. I could slap it right in the home folder like the amateur I am, but maybe there's some trick that adult apps use? I'm also worried that if I slap the file there I might overwrite another one that accidentally has the same name but doesn't belong to my program.

Brethren (and sistren), what say ye?

TheresaJayne
Jul 1, 2011

supermikhail posted:

I've got writer's block.

This application that I want to store preferences for, I just realized that I have to start with something - I have to have a default path where I would store all the non-default paths. I could slap it right in the home folder like the amateur I am, but maybe there's some trick that adult apps use? I'm also worried that if I slap the file there I might overwrite another one that accidentally has the same name but doesn't belong to my program.

Brethren (and sistren), what say ye?

How about you have code in your app that on startup looks in current folder for the file,
if it doesnt exist create it

As an example look at minecraft server, when you first run it will check for the ops.txt file and create it if it is not there.

Defenestrategy
Oct 24, 2010

So I'm trying to figure out how to put two conditions inside of an if statement, and I can't phrase the question well enough to get an answer from google.

Java code:
char que;

que = input.next( );
if(que = y or que = Y)
{
do something
}
else {
System.exit(0)
}
Any help?

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."

TheresaJayne posted:

How about you have code in your app that on startup looks in current folder for the file,
if it doesnt exist create it

As an example look at minecraft server, when you first run it will check for the ops.txt file and create it if it is not there.

The perfectionist says that the executable file might be in the home folder, in which case the default paths file will be there, too, and we're back at the start (could overwrite something important)... Dammit. Although, I'll gladly look up to Minecraft for good coding practices (after all, it brought Java into mainstream gaming :rolleyes:).

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

KildarX posted:

So I'm trying to figure out how to put two conditions inside of an if statement, and I can't phrase the question well enough to get an answer from google.

Java code:
char que;

que = input.next( );
if(que = y or que = Y)
{
do something
}
else {
System.exit(0)
}
Any help?

Googling 'java or operator' seems to be pretty easy. You're looking for ||
You also have some issues with your usage of =. If you're testing whether something is equivalent, you use ==. Also, single characters must be enclosed in single quotation marks.
This is what you want:
code:
if (que == 'y' || que == 'Y')

Defenestrategy
Oct 24, 2010

Kilson posted:

Googling 'java or operator' seems to be pretty easy. You're looking for ||
You also have some issues with your usage of =. If you're testing whether something is equivalent, you use ==. Also, single characters must be enclosed in single quotation marks.
This is what you want:
code:
if (que == 'y' || que == 'Y')

Still learning the language so not really sure what to call things, thanks a lot.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

supermikhail posted:

The perfectionist says that the executable file might be in the home folder, in which case the default paths file will be there, too, and we're back at the start (could overwrite something important)... Dammit. Although, I'll gladly look up to Minecraft for good coding practices (after all, it brought Java into mainstream gaming :rolleyes:).

Pointless snark aside, if you're that worried about it, have backup names and check if a file exists by the first name and open it, and if it doesn't look like your file then choose a different name.

"Adult" apps probably use either the Preferences API or the Properties API, and the former seems like overkill for storing one list of paths, while the latter is usually placed relative to the working directory.

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

KildarX posted:

Still learning the language so not really sure what to call things, thanks a lot.

Here's a list of all the operators in Java: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html You probably won't use some of these unless you're doing a lot of operations on raw bits, but it's good to have if you every come across a use of >>> and want to know what it does.

supermikhail
Nov 17, 2012


"It's video games, Scully."
Video games?"
"He enlists the help of strangers to make his perfect video game. When he gets bored of an idea, he murders them and moves on to the next, learning nothing in the process."
"Hmm... interesting."
YES!

So I think I've finally pinned down the directory that the running jar is in. It involves StackOverflow's notes in the margins and
code:
ClassLoader.getSystemClassLoader().getResource(".").toURI()
Anything funny about that? (as in, dangerous to the health of computers close to me)

Imazul
Sep 3, 2006

This was actually a lot more bearable than most of you made it out to be.
Anyone has any book recommendation for Aspect Oriented Programming in java. I started slowly incorporating it into some of our Spring applications but I could definitely used a good resource to pass around our development team.

Zeether
Aug 26, 2011

I'm working on an assignment for a Java course where I make a program that reads in a word search puzzle from a text file (2 numbers for the number of rows and columns, then all the characters in the text file as an array) along with a list of the words it needs to find, then it checks for the words in all 8 directions and outputs them to a text file along with the coordinates of the first letter of the word.

I figured out that I would have to use a method to check in each direction for a word because doing 8 different if statements wouldn't be clean enough, but I'm not sure if how I'm doing the method is right in terms of parameters and what I'm supposed to put into the method to get the word.

This is what I have so far, I put it into Pastebin because the amount of parameters in the method break tables when I use [code] for them: http://pastebin.com/E2Yk0A93

carry on then
Jul 10, 2010

by VideoGames

(and can't post for 10 years!)

You're on the right track with creating a method to search for a words, but I think it needs to encapsulate a simpler functionality than to check for all words. I would actually create two methods: one to scan through the puzzle in order until it comes to a letter that is the first letter of a word (or multiple words!) in the list (that hasn't been found yet.) When a matching letter is found, the method can call another method, which takes in a word to look for, coordinates to start with, and a direction to look in, and returns whether the word is found in that direction. That latter method could be called up to 8 times, one for each direction, until one returns true or you've exhausted directions to look, then you move on to the next word that starts with that letter, or if no other words are found, move to the next letter in the puzzle. Just remember to handle what happens if you hit the edge of the puzzle.

The only edge case I can think of to handle would be if one of the words in the list was also a substring of another word in the list (think "the" and "there", for instance), which could be taken care of by searching for longer words before shorter ones.

DholmbladRU
May 4, 2006
Having some trouble utlizing the GSON library on a JSP website. I used this library without any problems in some stand alone java code I was using. I migrated that code to another machine and am invoking it on some jsp pages. Now this is where I am hving trouble, it seems as if the code breaks when I call

code:
 new GsonBuilder() 
Old machine
JDK 1.7
JRE 1.7

new machine
JDK 1.7
JRE 1.6

I am unable to update the JRE on the new machine.

The weird thing is no errors are thrown, and if I put a break point on that line its never reached. I have tried with the Gson 2.2 and 2.4 and encounter the same problem.

Does anyone have troubleshooting ideas?



edit: nevermind. I needed to add the file in the lib directory on the JSP website....

DholmbladRU fucked around with this message at 22:11 on Sep 23, 2013

Zeether
Aug 26, 2011

I went back and wrote a method for the word search which uses loops for each direction, but it doesn't work right and I tried debugging with println() to see what was wrong and got nothing out of it. The loops are most likely wrong but I'm not sure which way and I haven't made ones to check diagonals yet.

http://pastebin.com/tzUi0ZiE

leftist heap
Feb 28, 2013

Fun Shoe
Did you try debugging it with, you know, a debugger?

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Zeether posted:

I went back and wrote a method for the word search which uses loops for each direction, but it doesn't work right and I tried debugging with println() to see what was wrong and got nothing out of it. The loops are most likely wrong but I'm not sure which way and I haven't made ones to check diagonals yet.

http://pastebin.com/tzUi0ZiE

Did you make a copy/paste error? wordCheck is never getting called. wordToFind is getting set within a loop, but never used. You've also got logic errors, but I kind of feel like you should work those out on your own.

Learning a debugger is like a super essential skill, and it's pretty dang easy for straightforward cases like your own. Are you using an IDE, like Eclipse? Read this tutorial.

horse_ebookmarklet
Oct 6, 2003

can I play too?
When I annotate a method/field/parameter/type, where are is the annotation's equals and hashcode defined? I'm digging through the OpenJDK sources and I can't figure it out.
I'm curious to see the definition of equals() and hashCode(), as we're getting into a slapfight with our JVM vendor on what logical equivalence is. In HotSpot, it seems a normal annotation instance (via .getAnnotations()) can be logically equivalent to a user defined implementation of that annotation interface (AnnotationImpl implements OurAnnotation), but in our production JVM they can't be.
This problem, of course, manifested itself as crazy undefined behaviors when working with HashMaps.

Volguus
Mar 3, 2009

horse_ebookmarklet posted:

When I annotate a method/field/parameter/type, where are is the annotation's equals and hashcode defined? I'm digging through the OpenJDK sources and I can't figure it out.
I'm curious to see the definition of equals() and hashCode(), as we're getting into a slapfight with our JVM vendor on what logical equivalence is. In HotSpot, it seems a normal annotation instance (via .getAnnotations()) can be logically equivalent to a user defined implementation of that annotation interface (AnnotationImpl implements OurAnnotation), but in our production JVM they can't be.
This problem, of course, manifested itself as crazy undefined behaviors when working with HashMaps.

I don't think I understand.
So, you have @OurAnnotation, and a class that implements it, AnnotationImpl. All good so far. You can implement in AnnotationImpl hashCode and equals without a problem. However ... what does that have to do with the HashMap? Are you storing annotations in a HashMap? Or ... how does this is supposed to work then?

Amarkov
Jun 21, 2010
Annotations inherit equals() and hashCode() directly from Object, and AccessibleObject.getAnnotations() is not specified to ensure reference equality with anything else. I'm also not quite clear on what you're trying to do, but I think the right answer is to override those two methods.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

supermikhail posted:

YES!

So I think I've finally pinned down the directory that the running jar is in. It involves StackOverflow's notes in the margins and
code:
ClassLoader.getSystemClassLoader().getResource(".").toURI()
Anything funny about that? (as in, dangerous to the health of computers close to me)

That's standard code for getting the working directory, its very common. It won't do anything dangerous in of itself, but as you said, you're getting files, so there's a chance you try to grab something afterwards and overwrite it or whatever.

This is running in a browser, right? If so you're pretty drat safe, unlikely user would have anything to do with those folders.

If you wanna be extra careful just give it a fairly unique filename, and you've done your due diligence.

Zaphod42 fucked around with this message at 22:23 on Sep 26, 2013

horse_ebookmarklet
Oct 6, 2003

can I play too?

rhag posted:

I don't think I understand.
So, you have @OurAnnotation, and a class that implements it, AnnotationImpl. All good so far. You can implement in AnnotationImpl hashCode and equals without a problem. However ... what does that have to do with the HashMap? Are you storing annotations in a HashMap? Or ... how does this is supposed to work then?

We are storing them in HashMaps, which is how I discovered the problem, and not really the problem itself. The hashmap is calling equals() which I believe is behaving incorrectly.

Amarkov posted:

Annotations inherit equals() and hashCode() directly from Object, and AccessibleObject.getAnnotations() is not specified to ensure reference equality with anything else. I'm also not quite clear on what you're trying to do, but I think the right answer is to override those two methods.

I'm not attempting to check reference equality, I'm attempting to check logical equality using .equals().

My AnnotationImpl implements equals and hashCode per the description in the Javadoc. The @OurAnnotation that the runtime creates seemingly doesn't implement equals properly. With a known "logically equal" annotation, the runtime equals() returns false. In the AnnotationImpl we created equals() returns true. In the Oracle JVM, both return true as expected.

I'm looking for either a specification or a reference implementation in the OpenJDK of equals() and hashCode() for Annotations. Our JDK vendor is saying that its implementation defined, which doesn't seem correct.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
I really don't understand why you would store an annotation in a hashmap :iiam:

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

horse_ebookmarklet posted:

Our JDK vendor is saying that its implementation defined, which doesn't seem correct.

Is your annotation actually an instance of the same annotation type, with all those same properties?

Does that javadoc describe the specification, or just one implementation?

If you want a hashmap that uses a custom definition of equality ... just use a hashmap with a custom comparator?

Jabor fucked around with this message at 05:26 on Sep 27, 2013

horse_ebookmarklet
Oct 6, 2003

can I play too?
It is an instance of an implementation of the same type, with the exact same properties. The annotationType()s of both return the same class.

I don't know if that javadoc describes the specification or an implementation, which is the point of contention with our vendor. Thats why I'm either looking for a real spec, or something in the OpenJDK reference implementation.

Why? At startup we scan a .properties file and create annotations based off the .properties keys. These annotations and their values are inserted into a hashmap. Later, after we create a new instance of something, we call a static method that scans for Fields that have annotations. If that field's annotation is in the hashmap we set that field equal to the value in the map. Its pretty crude, and could be replaced with a real DI framework.

So I could change it, but if its technically allowable via specification or reference implementation I'd rather our vendor fix their JVM.

Max Facetime
Apr 18, 2009

horse_ebookmarklet posted:

It is an instance of an implementation of the same type, with the exact same properties. The annotationType()s of both return the same class.

I don't know if that javadoc describes the specification or an implementation, which is the point of contention with our vendor. Thats why I'm either looking for a real spec, or something in the OpenJDK reference implementation.

Why? At startup we scan a .properties file and create annotations based off the .properties keys. These annotations and their values are inserted into a hashmap. Later, after we create a new instance of something, we call a static method that scans for Fields that have annotations. If that field's annotation is in the hashmap we set that field equal to the value in the map. Its pretty crude, and could be replaced with a real DI framework.

So I could change it, but if its technically allowable via specification or reference implementation I'd rather our vendor fix their JVM.

What ends up being called on my machine is a package-visible class sun.reflect.annotation.AnnotationInvocationHandler and its equalsImpl() method. This is called by a runtime generated proxy-class ($Proxy3) instance which marks one location that was annotated in the source code.

There's something weird in the equalsImpl() method. It has a check to see if the other object can be treated :airquote: as one of us :airquote: but if not it does a member-wise compare anyway. This smells of something.

Then Java 7 API docs say

Annotation.equals() posted:

Returns true if the specified object represents an annotation that is logically equivalent to this one. In other words, returns true if the specified object is an instance of the same annotation type as this instance
and

Annotation posted:

The common interface extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type.

So my reading is that if you are mixing your own classes implementing Annotation and generated Annotations then the Oracle implementation is wrong if it says generated Annotations are equal to subclasses of Annotations.

1337JiveTurkey
Feb 17, 2005

horse_ebookmarklet posted:

It is an instance of an implementation of the same type, with the exact same properties. The annotationType()s of both return the same class.

I don't know if that javadoc describes the specification or an implementation, which is the point of contention with our vendor. Thats why I'm either looking for a real spec, or something in the OpenJDK reference implementation.

Why? At startup we scan a .properties file and create annotations based off the .properties keys. These annotations and their values are inserted into a hashmap. Later, after we create a new instance of something, we call a static method that scans for Fields that have annotations. If that field's annotation is in the hashmap we set that field equal to the value in the map. Its pretty crude, and could be replaced with a real DI framework.

So I could change it, but if its technically allowable via specification or reference implementation I'd rather our vendor fix their JVM.

Just use Guice's @Named("Foo") annotation, and for that matter just use Guice.

Volguus
Mar 3, 2009

1337JiveTurkey posted:

Just use Guice's @Named("Foo") annotation, and for that matter just use Guice.

What actually he needs to use is the MapBinder or Multibinder from Guice.
The usage of annotations in this case looks a bit ... weird to say the least, but there could be a solution (kind of):

You read properties from a properties file. That is, key,value pairs. You can store that anywhere (in a map, properties file or simply a resource bundle). Now, via reflection you scan for fields, and you look for certain annotations (you have to know what annotations you're looking for). Then you can read the properties of that annotation (value, or whatever other things it may have), and then do whatever you need to do (update the field's value for example).

Even in your "not invented here" approach, you still don't need to store annotations in a map. Don't think of annotations as POJOs, think about them as metadata (which they are). An annotation doesn't make any sense nor does it have any reason to exist (logically) if not applied to a class, field or method.

But really, just use a CDI library (google's guice is lightweight and easy to setup and work with, you don't need to call the big boys like spring or weld).

Doctor w-rw-rw-
Jun 24, 2008

Max Facetime posted:

There's something weird in the equalsImpl() method. It has a check to see if the other object can be treated :airquote: as one of us :airquote: but if not it does a member-wise compare anyway. This smells of something.
Annotations from different classloaders would be different classes wouldn't they?

Adbot
ADBOT LOVES YOU

Max Facetime
Apr 18, 2009

Doctor w-rw-rw- posted:

Annotations from different classloaders would be different classes wouldn't they?

You know, I've never actually thought about how classloading of annotations happens. Intuitively it feels like annotations would have to end up in different classloaders and classes to support things like unloadable plugins, but then on the same token removing a field from some @AnnotationThingy version 2.0 shouldn't suddenly make it compare equal to some unrelated @FooBarAnnotation in version 1.0.

The most straightforward thing might be to write some test code to just see what happens.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply