jaxb-codemodel icon indicating copy to clipboard operation
jaxb-codemodel copied to clipboard

Inline JBlock

Open glassfishrobot opened this issue 11 years ago • 3 comments

I would like the ability to create a JBlock which will remain "inline". That is, if you declare a variable inside it, the braces+indents will NOT be activated.

Background:

My code creates a skeleton of an ORM library and then populates it with fields afterwards. It was necessary to structure it this way because of the complexity of the ORM code that I had to generate (it's for an old legacy system).

The skeleton contains a number of placeholders (JBlocks) which are used afterwards to inject code to handle the fields. Some methods can contain two or more blocks of this kind. Variables might be declared in the first block, to be used in the second block.

Unfortunately, as soon as a variable is declared inside a JBlock, it activates braces which turn it into a local block, effectively making the new variable local as well. So it cannot be used later on in the same method.

The end result is to produce this:

public Orange convertFromAppleToOrange(Apple apple) {
        {
            Integer id = apple.getId();
        }
        Orange orange = orangeRepository.create(id);
        {
            ThatThing thatThingValue = apple.getThatThing();
            orange.setThatThing(thatThingValue);
        }
        orange.setThisThing(apple.getThisThing());
        return orange;
    }

But what I needed was this:

public Orange convertFromAppleToOrange(Apple apple) {
        Integer id = apple.getId();
        Orange orange = orangeRepository.create(id);
        ThatThing thatThingValue = apple.getThatThing();
        orange.setThatThing(thatThingValue);
        orange.setThisThing(apple.getThisThing());
        return orange;
    }

I will try to attach a small ZIP containing a highly simplified, stripped down example of the problem. The generated source is structurally identical to a part of the real project, but the generating code was written quickly in order to demonstrate the problem, so please ignore any weird stuff there.

The project also contains a proposed new JBlock class, based on the 2.6 release, which achieves the result that I need. It is only a guideline, as I imagine that you prefer to write code in your own way.

Affected Versions

[2.6]

glassfishrobot avatar Dec 08 '14 19:12 glassfishrobot

Reported by andyraffle

glassfishrobot avatar Dec 08 '14 19:12 glassfishrobot

andyraffle said: I have a 97k file which I would like to attach, but I don't see the option anywhere. Please let me know how to attach it or where to email it. Thanks!

glassfishrobot avatar Dec 08 '14 19:12 glassfishrobot

This issue was imported from java.net JIRA CODEMODEL-26

glassfishrobot avatar Apr 24 '17 07:04 glassfishrobot