type as parameter.

So I’m writing this code. I would like to check if say my object is instanceof class that I put in as parameter. My code should explain itself.

		public boolean same(Item item, Class cls) {
			//this is were I don't know if it is possible to achieve.
			return item instanceof cls.getType(); // cls.getType() doesn't exist. 
		}

So is there a way to convert that cls object to a type ?
I could do this other way and stuff, but this seems to be the “cleanest way” if it would be possible to achieve.

Yes you can do this, but the big question here is why.
One should probably only use reflection hackery deep down in some library and not in your main business logic.

Tell us more about your problem and I promise you that we can find a way better solution.

Here is the code for my Inventory

	public static class Inventory {

		public int width, height;
		private Item[] items;

		public Inventory(int width, int height) {
			this.width = width;
			this.height = height;
			items = new Item[width * height];
		}

		public Item getItem(int x, int y) {
			if (x < 0 || x >= width || y < 0 || y >= height) return null;
			return items[x + y * width];
		}

		public void addItem(int x, int y, Item item) {
			if (x < 0 || x >= width || y < 0 || y >= height) return;
			items[x + y * width] = item;
		}

		public void addItem(Item item) {

			for (int y = 0; y < height; y++) {
				boolean br = false;

				for (int x = 0; x < width; x++) {

					Item itemInSlot = getItem(x, y);

					if (itemInSlot == null) {
						addItem(x, y, item);
						br = true;
						break;
					}

				}

				if (br) break;
			}
		}

		public boolean contains(Class<Item> cls) {
			for (int y = 0; y < height; y++) {
				for (int x = 0; x < width; x++) {
					Item item = items[x+y*width];
					// if item instanceof cls.getType() return true;
				}
			}
			return false;
			
			if(inventory.contains(Axe.class)) {
				chopTree();
			}
		}
		

	}

I want to be able to check if my items array contains an item that is instanceof specified class.
So for example:

			if(inventory.contains(Axe.class)) {
				chopTree();
			}

Ummm is it possible to pass in a ReferenceType? I could then just do item instanceof ReferenceType if I could have referenceType as an object.

arg because atm I can’t find of a better general solution here you go:


public boolean contains(Class<Item> cls) {
         for (Item i: items) {
            if(i != null && cls.isAssignableFrom(i.getClass()) return true;
         }
         return false;
}

which will work for [icode]contains(Weapon.class)[/icode] when you have a Sword item.

On the other hand, if your special items where singletons (only one axt item in the game, no different stats. works also for special keys and so on). You could just reference check.

btw, in your addItem method you can just call return after the successful insertion, no need for the breaks and the boolean flag.

Thanks.

Off topic:
take a look at this speech :smiley:
kinda funny.

Did you try using Object and then casting it?

I need to stop looking for new posts and do some coding :smiley:

@cannonball
Could you explain yourself ?

Oops sorry, I misread your post :frowning:

I knew something like that happened. Just trolling you as my name suggests :smiley: