Skip to content

Commit

Permalink
Merge pull request #57 from heruan/invocation-ref
Browse files Browse the repository at this point in the history
Method reference on invocations
  • Loading branch information
phax committed Oct 25, 2017
2 parents 138c7f3 + c175e00 commit d822cba
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/main/java/com/helger/jcodemodel/JLambdaMethodRef.java
Expand Up @@ -57,6 +57,7 @@ public class JLambdaMethodRef implements IJExpression
private final AbstractJType m_aType;
private final JVar m_aVar;
private final String m_sMethodName;
private final JInvocation m_aInvocation;

/**
* Constructor to reference the passed method
Expand All @@ -74,6 +75,7 @@ public JLambdaMethodRef (@Nonnull final JMethod aMethod)
m_aType = null;
m_aVar = null;
m_sMethodName = null;
m_aInvocation = null;
}

/**
Expand Down Expand Up @@ -103,6 +105,7 @@ public JLambdaMethodRef (@Nonnull final AbstractJType aType, @Nonnull final Stri
m_aType = JCValueEnforcer.notNull (aType, "Type");
m_aVar = null;
m_sMethodName = JCValueEnforcer.notEmpty (sMethod, "Method");
m_aInvocation = null;
}

/**
Expand All @@ -123,6 +126,7 @@ public JLambdaMethodRef (@Nonnull final JVar aVar, @Nonnull final String sMethod
m_aType = null;
m_aVar = aVar;
m_sMethodName = sMethod;
m_aInvocation = null;
}

/**
Expand All @@ -144,6 +148,28 @@ public JLambdaMethodRef (@Nonnull final JVar aVar, @Nonnull final JMethod aMetho
m_aType = null;
m_aVar = aVar;
m_sMethodName = null;
m_aInvocation = null;
}

/**
* Constructor for an arbitrary invocation method reference.
*
* @param aInvocation
* Variable containing the invocation. May not be <code>null</code>.
* @param sMethod
* Name of the method to reference. May neither be <code>null</code>
* nor empty.
*/
public JLambdaMethodRef (@Nonnull final JInvocation aInvocation, @Nonnull final String sMethod)
{
JCValueEnforcer.notNull (aInvocation, "Invocation");
JCValueEnforcer.notEmpty (sMethod, "Method");

m_aMethod = null;
m_aType = null;
m_aVar = null;
m_sMethodName = sMethod;
m_aInvocation = aInvocation;
}

/**
Expand Down Expand Up @@ -183,14 +209,24 @@ public AbstractJType type ()

/**
* @return The variable for the instance reference. May be <code>null</code>
* if this is a static reference.
* if this is a static or invocation reference.
*/
@Nullable
public JVar var ()
{
return m_aVar;
}

/**
* @return The invocation reference. May be <code>null</code> if this is a
* static or variable reference.
*/
@Nullable
public JInvocation invocation ()
{
return m_aInvocation;
}

/**
* @return The name of the referenced method. Never <code>null</code>.
*/
Expand All @@ -200,12 +236,16 @@ public String methodName ()
return m_aMethod != null ? m_aMethod.name () : m_sMethodName;
}

@Override
public void generate (@Nonnull final JFormatter f)
{
if (isStaticRef ())
f.type (type ());
else
f.generable (m_aVar);
if (m_aVar != null)
f.generable (m_aVar);
else
f.generable (m_aInvocation);
f.print ("::").print (methodName ());
}
}

0 comments on commit d822cba

Please sign in to comment.