phax / jcodemodel

A heavily extended fork of the com.sun.codemodel (from 2013/09)
Other
92 stars 34 forks source link

Unnecessary outer block created around two simple blocks #32

Closed WonderCsabo closed 8 years ago

WonderCsabo commented 8 years ago

The following code:

JCodeModel cm = new JCodeModel();

JPackage aPkg1 = cm._package("id.myapp.activity");

JDefinedClass testClass = aPkg1._class("TestClass");

JMethod init_ = testClass.method(JMod.PUBLIC, cm.VOID, "init_");
JBlock first = init_.body().blockSimple();
JBlock second = init_.body().blockSimple();

first.decl(cm.INT, "var");
second.decl(cm.INT, "va2");

cm.build (new SingleStreamCodeWriter(System.out));

Generates the following:

public class TestClass {

    public void init_() {
        {
            int var;
        }
        {
            int va2;
        }
    }
}

I do not really understand why this happening... :confused:

WonderCsabo commented 8 years ago

@phax i just updated my test case, which show the problem maybe not what i first thought. Indent and braces are created in the simple blocks!

phax commented 8 years ago

The "no indentation" and "no braces" is only in effect if there is 0 or at last 1 declaration contained in there. As soon as more than 1 decl is contained, the braces are mandatory.

phax commented 8 years ago

And yes, they are created by the JBlock

phax commented 8 years ago

Off topic: I asked @sviperll whether it would make sense to include his meta.java.model stuff in jcodemodel. What do you think of this?

WonderCsabo commented 8 years ago

OK, i understand a rational, and i know that i use blocks in a non-standard way. Maybe can i request a block type, which does not create braces and indent, ever?

phax commented 8 years ago

So you need something like a "VirtualBlock" that is basically just a list of declarations, statements etc. that is emitted whereever you put it - right?

WonderCsabo commented 8 years ago

Exactly. Of course, this virtual block could contain real blocks.

WonderCsabo commented 8 years ago

The easiest implementation would be adding a new flag (neverCreateIndentBraces) to JBlock, and check it when you set m_bBracesRequired and m_bIndentRequired. Then create a new block factory method which sets neverCreateIndentBraces to true. But maybe there is a better solution. :)

dodgex commented 8 years ago

an option to creates a block that overrids the current behaviour would be enough.

e.g. .blockSimple() keeps current behaviour and .blockSimple(false) does never set the required flags (public JBlock blockSimple(boolean requireBracesOnDecl))

phax commented 8 years ago

I added a "virtual block" mode. See JBlock.blockVirtual(). Or simply call .virtual (true) on any block.

WonderCsabo commented 8 years ago

@phax thanks for your super-fast reaction, again! It was a pain to migrate to jcodemodel from codemodel, but it was worth it! We we can expect a release containing this change?

phax commented 8 years ago

Very soon. I just need to check with @sviperll about the inclusion of meta and than I will release a new version. So today or tomorrow... Expect it to be 2.8.0 if the meta stuff is incorporated

phax commented 8 years ago

2.8.0 is released. Should be on Maven central soon. Requires Java 1.6!