History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: JANINO-117
Type: Bug Bug
Status: Reopened Reopened
Priority: Major Major
Assignee: Arno Unkrig
Reporter: Matt Fowles
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Janino

UnparseVisitor does not handle Byte or Short Literals

Created: 28/May/08 10:26 AM   Updated: 18/Jun/08 02:50 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 2.5.15

Time Tracking:
Not Specified

File Attachments: 1. File literals.diff (2 kb)


Testcase included: yes
Patch Submitted: Yes


 Description  « Hide
The following test will fail with a RuntimeException. While parsed java cannot create a literal Byte or Short value, it is fairly easy to create them directly from the AST, and handling them is quite simple.
public void testLiterals() throws Exception {
        Object[][] tests = new Object[][] {
                { new Java.Literal(null, new Short((short)1)), "((short)1)" },
                { new Java.Literal(null, new Byte((byte)1)),   "((byte)1)"  },
        };
        for(int i = 0; i < tests.length; ++i) {
            Atom expr = (Atom) tests[i][0];
            String expected = (String) tests[i][1];
            
            StringWriter sw = new StringWriter();
            UnparseVisitor uv = new UnparseVisitor(sw);
            expr.accept(uv);
            Assert.assertEquals(expected, sw.toString());
        }
    }


 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Arno Unkrig - 14/Jun/08 03:33 PM
Applied the patch.

Please test.


Matt Fowles - 16/Jun/08 09:28 AM
Arno, I do not have the ability to close this issue. I think my login to Jira does not have the power. You can consider this closed.

Arno Unkrig - 18/Jun/08 02:40 PM
I see. This is not compatible with my understanding of the JIRA standard workflow.

Arno Unkrig - 18/Jun/08 02:41 PM
However, I have bad news for you: I removed your fix. The reason being is that in Java there is no such thing as a "byte literal" or "short literal". "new Literal(new Byte(8))" worked only accidentially!

But Java's conversion rules (JLS2 5.2) imply that you can legally assign an integer literal to, say, a byte variable, if the constant value of the integer literal is in range:

byte a = 3;
byte b = -128;

I added an argument type check to "Literal(Object)" which throws IllegalArgumentExceptions.


Matt Fowles - 18/Jun/08 02:50 PM
I would strongly urge you to reconsider this choice. While Java does not allow byte literals directly, making your AST support them increases the expressive power of your library, without exposing any bugs.

In fact, this ability greatly simplifies targeting Janino's AST from a different compiler, which is exactly what we are doing.