Skip to content

Commit

Permalink
Added test for #62
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 22, 2018
1 parent 2e77d20 commit 97946eb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/helger/jcodemodel/AbstractJClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public final IJExpression dotclass ()
@Nonnull
public final JInvocation staticInvoke (@Nonnull final JMethod aMethod)
{
return new JInvocation (this, aMethod);
return new JInvocation (owner (), this, aMethod);
}

/**
Expand All @@ -494,7 +494,7 @@ public final JInvocation staticInvoke (@Nonnull final JMethod aMethod)
@Nonnull
public final JInvocation staticInvoke (@Nonnull final String sMethod)
{
return new JInvocation (this, sMethod);
return new JInvocation (owner (), this, sMethod);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/helger/jcodemodel/JAtom.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ protected JAtom (@Nonnull final String sWhat)
m_sWhat = sWhat;
}

/**
* @return The text representation of this atom content. The same as what as
* passed in the constructor.
*/
@Nonnull
public String what ()
{
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/com/helger/jcodemodel/JBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,28 @@ public JBlock assignDivide (@Nonnull final IJAssignmentTarget aLhs, @Nonnull fin
@Nonnull
public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final String sMethod)
{
return internalInsert (new JInvocation (aExpr, sMethod));
return invoke ((JCodeModel) null, aExpr, sMethod);
}

/**
* Creates an invocation statement and adds it to this block.
*
* @param aCM
* CodeModel to use. May be <code>null</code>.
* @param aExpr
* {@link IJExpression} evaluating to the class or object upon which
* the named method will be invoked
* @param sMethod
* Name of method to invoke
* @return Newly generated {@link JInvocation}
* @since 3.0.5
*/
@Nonnull
public JInvocation invoke (@Nullable final JCodeModel aCM,
@Nonnull final IJExpression aExpr,
@Nonnull final String sMethod)
{
return internalInsert (new JInvocation (aCM, aExpr, sMethod));
}

/**
Expand Down Expand Up @@ -434,7 +455,7 @@ public JInvocation invokeSuper ()
@Nonnull
public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final JMethod aMethod)
{
return internalInsert (new JInvocation (aExpr, aMethod));
return internalInsert (new JInvocation (aMethod.owner (), aExpr, aMethod));
}

/**
Expand Down Expand Up @@ -462,7 +483,7 @@ public JInvocation invokeThis (@Nonnull final JMethod aMethod)
@Nonnull
public JInvocation staticInvoke (@Nonnull final AbstractJClass aType, @Nonnull final String sMethod)
{
return internalInsert (new JInvocation (aType, sMethod));
return internalInsert (new JInvocation (aType.owner (), aType, sMethod));
}

/**
Expand All @@ -475,7 +496,7 @@ public JInvocation staticInvoke (@Nonnull final AbstractJClass aType, @Nonnull f
@Nonnull
public JInvocation invoke (@Nonnull final String sMethod)
{
return internalInsert (new JInvocation ((IJExpression) null, sMethod));
return internalInsert (new JInvocation ((JCodeModel) null, (IJExpression) null, sMethod));
}

/**
Expand All @@ -488,7 +509,7 @@ public JInvocation invoke (@Nonnull final String sMethod)
@Nonnull
public JInvocation invoke (@Nonnull final JMethod aMethod)
{
return internalInsert (new JInvocation ((IJExpression) null, aMethod));
return internalInsert (new JInvocation (aMethod.owner (), (IJExpression) null, aMethod));
}

@Nonnull
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/helger/jcodemodel/JExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,34 @@ public static JInvocation _new (@Nonnull final AbstractJType aType)
@Nonnull
public static JInvocation invoke (@Nonnull final String sMethod)
{
return invoke (null, sMethod);
return invoke ((IJExpression) null, sMethod);
}

@Nonnull
public static JInvocation invoke (@Nonnull final JMethod aMethod)
{
return invoke (null, aMethod);
return invoke ((IJExpression) null, aMethod);
}

@Nonnull
public static JInvocation invoke (@Nullable final IJExpression aLhs, @Nonnull final JMethod aMethod)
{
return new JInvocation (aLhs, aMethod);
return new JInvocation (aMethod.owner (), aLhs, aMethod);
}

@Nonnull
public static JInvocation invoke (@Nullable final IJExpression aLhs, @Nonnull final String sMethod)
{
return new JInvocation (aLhs, sMethod);
// No owner available
return new JInvocation ((JCodeModel) null, aLhs, sMethod);
}

@Nonnull
public static JInvocation invoke (@Nullable final JCodeModel aOwner,
@Nullable final IJExpression aLhs,
@Nonnull final String sMethod)
{
return new JInvocation (aOwner, aLhs, sMethod);
}

@Nonnull
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/com/helger/jcodemodel/JInvocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ public class JInvocation implements IJExpressionStatement, IJOwnedMaybe
* @param sName
* Name of method to invoke
*/
@Deprecated
protected JInvocation (@Nullable final IJExpression aObject, @Nonnull final String sName)
{
// Not possible to determine an owner :(
this (null, aObject, sName);
}

@Deprecated
protected JInvocation (@Nullable final IJExpression aObject, @Nonnull final JMethod aMethod)
{
this (aMethod.owner (), aObject, aMethod);
Expand All @@ -120,6 +122,7 @@ protected JInvocation (@Nullable final IJExpression aObject, @Nonnull final JMet
* @param sMethodName
* Method name to be invoked
*/
@Deprecated
protected JInvocation (@Nonnull final AbstractJClass aType, @Nonnull final String sMethodName)
{
this (aType.owner (), aType, sMethodName);
Expand All @@ -133,14 +136,15 @@ protected JInvocation (@Nonnull final AbstractJClass aType, @Nonnull final Strin
* @param aMethod
* Method to be invoked
*/
@Deprecated
protected JInvocation (@Nonnull final AbstractJClass aType, @Nonnull final JMethod aMethod)
{
this (aType.owner (), aType, aMethod);
}

private JInvocation (@Nullable final JCodeModel aOwner,
@Nullable final IJGenerable aObject,
@Nonnull final String sName)
protected JInvocation (@Nullable final JCodeModel aOwner,
@Nullable final IJGenerable aObject,
@Nonnull final String sName)
{
JCValueEnforcer.notNull (sName, "Name");
JCValueEnforcer.isFalse (sName.indexOf ('.') >= 0, () -> "method name contains '.': " + sName);
Expand All @@ -152,9 +156,9 @@ private JInvocation (@Nullable final JCodeModel aOwner,
m_aConstructorType = null;
}

private JInvocation (@Nonnull final JCodeModel aOwner,
@Nullable final IJGenerable aObject,
@Nonnull final JMethod aMethod)
protected JInvocation (@Nonnull final JCodeModel aOwner,
@Nullable final IJGenerable aObject,
@Nonnull final JMethod aMethod)
{
JCValueEnforcer.notNull (aOwner, "Owner");
JCValueEnforcer.notNull (aMethod, "Method");
Expand Down Expand Up @@ -443,7 +447,8 @@ public void generate (@Nonnull final JFormatter f)
if (m_aObject != null)
{
// object.<generics> name (
f.generable (m_aObject).print ('.');
f.generable (m_aObject);
f.print ('.');
_addTypeVars (f);
f.print (name);
f.print ('(');
Expand Down Expand Up @@ -514,19 +519,19 @@ public boolean equals (final Object o)
@Override
public int hashCode ()
{
JCHashCodeGenerator hashCodeGenerator = new JCHashCodeGenerator (this).append (m_aObject).append (m_bIsConstructor);
JCHashCodeGenerator aHCGen = new JCHashCodeGenerator (this).append (m_aObject).append (m_bIsConstructor);
if (!m_bIsConstructor)
hashCodeGenerator = hashCodeGenerator.append (_methodName ());
hashCodeGenerator = hashCodeGenerator.append (m_aArgs).append (_typeFullName ());
aHCGen = aHCGen.append (_methodName ());
aHCGen = aHCGen.append (m_aArgs).append (_typeFullName ());
if (m_aTypeVariables != null)
{
hashCodeGenerator = hashCodeGenerator.append (m_aTypeVariables.size ());
aHCGen = aHCGen.append (m_aTypeVariables.size ());
for (final JTypeVar typeVariable : m_aTypeVariables)
{
hashCodeGenerator = hashCodeGenerator.append (typeVariable.fullName ());
aHCGen = aHCGen.append (typeVariable.fullName ());
}
}
return hashCodeGenerator.getHashCode ();
return aHCGen.getHashCode ();
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/helger/jcodemodel/JInvocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,22 @@ public void testWithGenerics () throws Exception

CodeModelTestsHelper.parseCodeModel (cm);
}

@Test
public void testChainedInvoke () throws Exception
{
final JCodeModel cm = new JCodeModel ();
final JDefinedClass cls = cm._class ("TestInvocation2");

final JMethod m1 = cls.method (JMod.PUBLIC, cls, "foo1");
m1.body ()._return (JExpr._this ());

final JMethod m2 = cls.method (JMod.PUBLIC, cls, "foo2");
m2.body ()._return (JExpr._this ());

final JMethod minvoke = cls.method (JMod.PUBLIC, cm.VOID, "bar");
minvoke.body ().invoke (m1).invoke (m2);

CodeModelTestsHelper.printCodeModel (cm);
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/helger/jcodemodel/JLambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,29 @@ public void testStatementBasicType ()
assertEquals ("(int x) -> {" + CRLF + " return (x + 1);" + CRLF + "}" + CRLF,
CodeModelTestsHelper.toString (aLambda));
}

@Test
public void testIssue62 ()
{
final JCodeModel cm = new JCodeModel ();

final JVar holder = new JVar (JMods.forVar (0), cm.ref (Object.class), "a", null);
final JLambda aLambda = new JLambda ();
final JLambdaParam arr = aLambda.addParam ("arr");
final JBlock setBody = aLambda.body ().synchronizedBlock (holder).body ();
setBody.add (JExpr.invoke (cm, holder, "entrySet").invoke ("retainAll").arg (arr.invoke ("entrySet")));
assertEquals ("arr -> {" +
CRLF +
" synchronized (a)" +
CRLF +
" {" +
CRLF +
" a.entrySet().retainAll(arr.entrySet());" +
CRLF +
" }" +
CRLF +
"}" +
CRLF,
CodeModelTestsHelper.toString (aLambda));
}
}

0 comments on commit 97946eb

Please sign in to comment.