|
Reflection! And Generics! I'd like to get the type parameter, as a Class, Type, or even just the canonical name String, used in a generic class. For example, I'd like to get the Class object for java.lang.String from a Vector<String> object, or a Class object for java.lang.Integer from a Vector<Integer> object. I found the Class.getTypeParameters() method, but the best I can get out of that is a java.lang.Object Type object. This isn't crucial to my project, so if it's impossible it's impossible. It would really simplify maintenance and documentation if there is a way to get it, though. Any ideas?
|
# ¿ Mar 18, 2008 01:42 |
|
|
# ¿ Apr 28, 2024 13:31 |
|
F'Nog posted:Just to double check what you're meaning, given any Vector<?> you want to retrieve the class of ? at runtime? Sorry to say but you can't. Java uses type erasure for generics which means at runtime all the type information is gone. Lame. Then what's the point of Class.getTypeParameters()?
|
# ¿ Mar 18, 2008 02:04 |
|
Is there a "Java way" to accomplish the same thing as bitflags? I know it's possible to do bitflags in Java, but it just doesn't feel right. I'd like the same kind of functionality, but without significantly more complicated syntax. For example, I'd rather avoid making a whole new class containing a half-dozen Booleans, then have to construct this class every time I want to call a function. What I have now is basically: code:
|
# ¿ Apr 9, 2008 00:45 |
|
TRex EaterofCars posted:Yeah, please let us know a bit more about your problem. There is almost always a more elegant way of handling that sort of multiplexing. Basically a utility function that compares two strings in different ways based on the bitflags. The code's for work, so I don't have it on me, but the flags are something like COMP_CASE_INSENSITIVE, COMP_BEGINS_WITH, COMP_ENDS_WITH, COMP_CONTAINS and so on. It's a whole lot easier to maintain one function and a couple bitflags than a few dozen functions with various combinations of the above options. The functions signature is essentially static void compare(String expected, String found, int flags).
|
# ¿ Apr 9, 2008 04:56 |
|
zootm posted:Yeah, you probably want enums, assuming you're using Java 5 or newer. The EnumSet class will let you do the 'bitflag' thing in a typesafe way while also getting the full functionality of the Set interface. It's implemented as bitflags too so you get that time efficiency. Neat, that looks like a good solution. Thanks.
|
# ¿ Apr 9, 2008 15:24 |
|
This should be super easy, but I'm just having a dog of a time figuring this out. Java doesn't have unsigned types, which makes reading binary files from languages that do have unsigned types a pain. I'm using a java.nio.ByteBuffer to read a file, and need to do a conversion from an unsigned byte into a Java type (I figure just an int). I've got b = b < 0 ? b + 256 : b;, which seems like an obvious fix, except if b=128. In that case, the binary reads 1000 0000, which when converted to signed types is -0, which fails that test and returns 0 instead of 128 like it should. I feel like I'm overthinking this. What am I missing?
|
# ¿ Jul 7, 2008 03:43 |
|
|
# ¿ Apr 28, 2024 13:31 |
|
TRex EaterofCars posted:You want to do bitwise and'ing on it and store it in a type that is larger than necesary. In your example, you want to put those bytes in an int after you & 0xFF them to yank the sign extension. Well, that worked perfectly. Thank you.
|
# ¿ Jul 7, 2008 04:10 |