Page 1 of 1

Errors when failing to set appearance attributes correctly

Posted: Fri 19. Jul 2013, 20:24
by dunmatt
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.

Re: Errors when failing to set appearance attributes correct

Posted: Tue 23. Jul 2013, 13:48
by Andre
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.

Re: Errors when failing to set appearance attributes correct

Posted: Tue 23. Jul 2013, 21:50
by gunn
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?

Re: Errors when failing to set appearance attributes correct

Posted: Fri 26. Jul 2013, 13:16
by Andre
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.

Re: Errors when failing to set appearance attributes correct

Posted: Sat 27. Jul 2013, 06:28
by gunn
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();
    }
  }