Skip to content

Commit d822cba

Browse files
authored
Merge pull request #57 from heruan/invocation-ref
Method reference on invocations
2 parents 138c7f3 + c175e00 commit d822cba

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/main/java/com/helger/jcodemodel/JLambdaMethodRef.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class JLambdaMethodRef implements IJExpression
5757
private final AbstractJType m_aType;
5858
private final JVar m_aVar;
5959
private final String m_sMethodName;
60+
private final JInvocation m_aInvocation;
6061

6162
/**
6263
* Constructor to reference the passed method
@@ -74,6 +75,7 @@ public JLambdaMethodRef (@Nonnull final JMethod aMethod)
7475
m_aType = null;
7576
m_aVar = null;
7677
m_sMethodName = null;
78+
m_aInvocation = null;
7779
}
7880

7981
/**
@@ -103,6 +105,7 @@ public JLambdaMethodRef (@Nonnull final AbstractJType aType, @Nonnull final Stri
103105
m_aType = JCValueEnforcer.notNull (aType, "Type");
104106
m_aVar = null;
105107
m_sMethodName = JCValueEnforcer.notEmpty (sMethod, "Method");
108+
m_aInvocation = null;
106109
}
107110

108111
/**
@@ -123,6 +126,7 @@ public JLambdaMethodRef (@Nonnull final JVar aVar, @Nonnull final String sMethod
123126
m_aType = null;
124127
m_aVar = aVar;
125128
m_sMethodName = sMethod;
129+
m_aInvocation = null;
126130
}
127131

128132
/**
@@ -144,6 +148,28 @@ public JLambdaMethodRef (@Nonnull final JVar aVar, @Nonnull final JMethod aMetho
144148
m_aType = null;
145149
m_aVar = aVar;
146150
m_sMethodName = null;
151+
m_aInvocation = null;
152+
}
153+
154+
/**
155+
* Constructor for an arbitrary invocation method reference.
156+
*
157+
* @param aInvocation
158+
* Variable containing the invocation. May not be <code>null</code>.
159+
* @param sMethod
160+
* Name of the method to reference. May neither be <code>null</code>
161+
* nor empty.
162+
*/
163+
public JLambdaMethodRef (@Nonnull final JInvocation aInvocation, @Nonnull final String sMethod)
164+
{
165+
JCValueEnforcer.notNull (aInvocation, "Invocation");
166+
JCValueEnforcer.notEmpty (sMethod, "Method");
167+
168+
m_aMethod = null;
169+
m_aType = null;
170+
m_aVar = null;
171+
m_sMethodName = sMethod;
172+
m_aInvocation = aInvocation;
147173
}
148174

149175
/**
@@ -183,14 +209,24 @@ public AbstractJType type ()
183209

184210
/**
185211
* @return The variable for the instance reference. May be <code>null</code>
186-
* if this is a static reference.
212+
* if this is a static or invocation reference.
187213
*/
188214
@Nullable
189215
public JVar var ()
190216
{
191217
return m_aVar;
192218
}
193219

220+
/**
221+
* @return The invocation reference. May be <code>null</code> if this is a
222+
* static or variable reference.
223+
*/
224+
@Nullable
225+
public JInvocation invocation ()
226+
{
227+
return m_aInvocation;
228+
}
229+
194230
/**
195231
* @return The name of the referenced method. Never <code>null</code>.
196232
*/
@@ -200,12 +236,16 @@ public String methodName ()
200236
return m_aMethod != null ? m_aMethod.name () : m_sMethodName;
201237
}
202238

239+
@Override
203240
public void generate (@Nonnull final JFormatter f)
204241
{
205242
if (isStaticRef ())
206243
f.type (type ());
207244
else
208-
f.generable (m_aVar);
245+
if (m_aVar != null)
246+
f.generable (m_aVar);
247+
else
248+
f.generable (m_aInvocation);
209249
f.print ("::").print (methodName ());
210250
}
211251
}

0 commit comments

Comments
 (0)