Errors when failing to set appearance attributes correctly

Something missing?
Post Reply
dunmatt
Posts: 22
Joined: Sat 28. Jul 2012, 02:36

Errors when failing to set appearance attributes correctly

Post by dunmatt » Fri 19. Jul 2013, 20:24

I just spent entirely too long figuring out why the following line of code does nothing:

pointsComponent.getAppearance().setAttribute(CommonAttributes.POINT_SHADER + "." + CommonAttributes.POINT_SIZE, 2);

It would be really nice if setAttribute would throw an exception if you've asked for a string that is wrong, or if the argument you've sent isn't the right type for that attribute. Or if throwing exceptions isn't the way jreality prefers to do things, even just returning a true or false to indicate success would be a very useful feature for us fallible programmers.

Andre
Posts: 226
Joined: Fri 18. Sep 2009, 11:30

Re: Errors when failing to set appearance attributes correct

Post by Andre » Tue 23. Jul 2013, 13:48

Puh! This is pretty hard to debug. Since it isn't obvious to me what kind of string-"key"s can be set with the setAttribute-function. This are not only the ones from the CommonAttributes.class. I'll put this on the topic for the next jReality-meeting.

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Re: Errors when failing to set appearance attributes correct

Post by gunn » Tue 23. Jul 2013, 21:50

This is a very unfortunate bug, I agree. As you have discovered, if you use "2.0" instead of "2" it works. It's one of those bugs which I have learned to work-around but after looking into the code for Appearance I think it might be fixable. To be precise, in Appearance's getAttribute() method, one could try to convert the given "value" to the desired class (for example if the desired type is double (as in this case) then values of any numerical type (int, long, or float) would be converted to double and used. As far as I know, in existing jReality code an appearance attribute is designed to work with one value type (class) so this wouldn't break any existing code. Any volunteers to implement this change?
jReality core developer

Andre
Posts: 226
Joined: Fri 18. Sep 2009, 11:30

Re: Errors when failing to set appearance attributes correct

Post by Andre » Fri 26. Jul 2013, 13:16

Sorry, but I don't understand your solution. When I call the getAttribute function I get as class an Object back (class java.lang.Object). So I still don't know what kind of class (double, int, etc.) is needed.

User avatar
gunn
Posts: 323
Joined: Thu 14. Dec 2006, 09:56
Location: TU Berlin
Contact:

Re: Errors when failing to set appearance attributes correct

Post by gunn » Sat 27. Jul 2013, 06:28

Here's what I had in mind. I've edited the existing getAppearance() to try to convert to the desired type.

Code: Select all

  public Object getAttribute(String key, Class type)
  {
    startReader();
    try {
      Object val=getAttribute(key);
      if (val==DEFAULT||type.isInstance(val)) return val;
      // try to convert to desired type
      if (type == Double.class)	{
    	  	if (val instanceof Integer) {
    	  		int i =  ((Integer) val);
    	  		double d = (double) i;
    	  		return d;
    	  	} else if (val instanceof Float) {
    	  		// etc
    	  	}
      }
      return INHERITED;
    } finally {
      finishReader();
    }
  }
jReality core developer

Post Reply