Skip to content

Commit

Permalink
Found quick solution for #53
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed May 16, 2017
1 parent c0bfb26 commit 384ec5a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/helger/jcodemodel/JBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ public void generate (@Nonnull final JFormatter f)
}
}

void generateBody (@Nonnull final JFormatter f)
protected void generateBody (@Nonnull final JFormatter f)
{
for (final IJObject aContentElement : m_aContentList)
{
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/helger/jcodemodel/JForEach.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

import javax.annotation.Nonnull;

import com.helger.jcodemodel.util.JCValueEnforcer;

/**
* ForEach Statement This will generate the code for statement based on the new
* j2se 1.5 j.l.s.
Expand All @@ -54,23 +56,20 @@ public class JForEach implements IJStatement
private final String m_sVarName;
private JBlock m_aBody; // lazily created
private final IJExpression m_aCollection;
private final JVar m_aLopVar;
private final JVar m_aLoopVar;

protected JForEach (@Nonnull final AbstractJType aVarType,
@Nonnull final String sVarName,
@Nonnull final IJExpression aCollection)
{
if (aVarType == null)
throw new NullPointerException ("Variable Type");
if (sVarName == null)
throw new NullPointerException ("Variable name");
if (aCollection == null)
throw new NullPointerException ("Collection expression");
JCValueEnforcer.notNull (aVarType, "VarType");
JCValueEnforcer.notNull (sVarName, "VarName");
JCValueEnforcer.notNull (aCollection, "Collection");

m_aType = aVarType;
m_sVarName = sVarName;
m_aCollection = aCollection;
m_aLopVar = new JVar (JMods.forVar (JMod.FINAL), m_aType, m_sVarName, aCollection);
m_aLoopVar = new JVar (JMods.forVar (JMod.FINAL), m_aType, m_sVarName, aCollection);
}

@Nonnull
Expand All @@ -85,7 +84,7 @@ public AbstractJType type ()
@Nonnull
public JVar var ()
{
return m_aLopVar;
return m_aLoopVar;
}

@Nonnull
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/helger/jcodemodel/JForLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ public JBlock body ()
public void state (@Nonnull final JFormatter f)
{
f.print ("for (");
boolean first = true;
boolean bFirst = true;
for (final Object o : m_aInitExprs)
{
if (!first)
if (!bFirst)
f.print (',');
if (o instanceof JVar)
f.var ((JVar) o);
else
f.generable ((IJExpression) o);
first = false;
bFirst = false;
}
f.print (';').generable (m_aTestExpr).print (';').generable (m_aUpdateExprs).print (')');
if (m_aBody != null)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/helger/jcodemodel/JLambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public void generate (@Nonnull final JFormatter f)
f.print (" -> ");

// Print body
final boolean bNoBraces = m_aBodyStatement.size () == 1 &&
m_aBodyStatement.getContents ().get (0) instanceof IJExpression;
m_aBodyStatement.bracesRequired (!bNoBraces);
final boolean bBraces = m_aBodyStatement.size () != 1 ||
!(m_aBodyStatement.getContents ().get (0) instanceof IJExpression);
m_aBodyStatement.bracesRequired (bBraces);
f.statement (m_aBodyStatement);
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/helger/jcodemodel/JLambdaBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ public void lambdaExpr (@Nonnull final IJExpression aExpr)
removeAll ();
internalInsert (aExpr);
}

@Override
protected void generateBody (@Nonnull final JFormatter f)
{
if (m_aContentList.size () == 1)
{
// Special handling - prefer expressions over statement in case it is both
final IJObject aContentElement = m_aContentList.get (0);
if (aContentElement instanceof IJStatement && aContentElement instanceof IJExpression)
{
f.generable ((IJGenerable) aContentElement);
return;
}
}

super.generateBody (f);
}
}
12 changes: 2 additions & 10 deletions src/main/java/com/helger/jcodemodel/JReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,9 @@ public IJExpression expr ()
public void state (@Nonnull final JFormatter f)
{
f.print ("return");
boolean bAddSemicolon = true;
if (m_aExpr != null)
{
f.print (' ').generable (m_aExpr);
if (m_aExpr instanceof JLambda)
bAddSemicolon = false;
}
if (bAddSemicolon)
{
f.print (';');
f.newline ();
}
f.print (';');
f.newline ();
}
}

0 comments on commit 384ec5a

Please sign in to comment.