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
ColdPie
Jun 9, 2006

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?

Adbot
ADBOT LOVES YOU

ColdPie
Jun 9, 2006

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()?

ColdPie
Jun 9, 2006

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:
static final int Flag_01 = 0x000000001;
static final int Flag_02 = 0x000000002;
static final int Flag_03 = 0x000000004;
static final int Flag_04 = 0x000000008;
static final int Flag_05 = 0x000000010;
static final int Flag_06 = 0x000000020;

public void someMethod(int flags){
    if( (flags & Flag_01) != 0){
        //do something
    }
    if( (flags & Flag_02) != 0){
        //do something else
    }
    //etc.
}

//invoking:
    someMethod(Flag_01 | Flag_03);
How "should" I be doing this in Java?

ColdPie
Jun 9, 2006

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).

ColdPie
Jun 9, 2006

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.

ColdPie
Jun 9, 2006

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?

Adbot
ADBOT LOVES YOU

ColdPie
Jun 9, 2006

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.

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