Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method reference on invocations #57

Merged
merged 1 commit into from
Oct 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/main/java/com/helger/jcodemodel/JLambdaMethodRef.java
Original file line number Diff line number Diff line change
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 ());
}
}