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

Add displayName to Signature for debugging and JRuby fun #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
32 changes: 30 additions & 2 deletions src/main/java/com/headius/invokebinder/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
*/
public class Signature {
private final MethodType methodType;
private final String displayName;
private final String[] argNames;

/**
Expand Down Expand Up @@ -93,25 +94,32 @@ public class Signature {
* @param argNames the argument names for the new signature
*/
Signature(MethodType methodType, String... argNames) {
this(methodType, null, argNames);
}

Signature(MethodType methodType, String displayName, String... argNames) {
assert methodType.parameterCount() == argNames.length : "arg name count " + argNames.length + " does not match parameter count " + methodType.parameterCount();
this.methodType = methodType;
this.argNames = argNames;
this.displayName = displayName;
}


/**
* Construct a new signature with the given method type and argument names.
*
* @param methodType the method type for the new signature
* @param displayName used for toString and possibly for non-MH uses.
* @param firstName the first argument name for the new signature; for eventual instance methods, it can be "this"
* @param restNames the remaining argument names for the new signature
*/
Signature(MethodType methodType, String firstName, String... restNames) {
Signature(MethodType methodType, String displayName, String firstName, String... restNames) {
assert methodType.parameterCount() == (restNames.length + 1) : "arg name count " + (restNames.length + 1) + " does not match parameter count " + methodType.parameterCount();
this.methodType = methodType;
this.argNames = new String[restNames.length + 1];
this.argNames[0] = firstName;
System.arraycopy(restNames, 0, this.argNames, 1, restNames.length);
this.displayName = displayName;
}

/**
Expand All @@ -121,7 +129,8 @@ public class Signature {
* @return a human-readable representation of the signature
*/
public String toString() {
StringBuilder sb = new StringBuilder("(");
StringBuilder sb = new StringBuilder(displayName == null ? "" : displayName);
sb.append("(");
for (int i = 0; i < argNames.length; i++) {
sb.append(methodType.parameterType(i).getSimpleName()).append(' ').append(argNames[i]);
if (i + 1 < argNames.length) {
Expand All @@ -143,6 +152,16 @@ public static Signature returning(Class<?> retval) {
return sig;
}

/**
* Add a new signature with the given displayName..
*
* @param displayName the return type for the new signature
* @return the new signature
*/
public Signature displayName(String displayName) {
return new Signature(methodType, displayName, argNames);
}

/**
* Create a new signature based on the given return value, argument types, and argument names
*
Expand Down Expand Up @@ -528,6 +547,15 @@ public MethodType type() {
return methodType;
}

/**
* The display name for this Signature.
*
* @return the current method type
*/
public String displayName() {
return displayName;
}

/**
* The current argument count.
*
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/com/headius/invokebinder/SignatureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void tearDown() {
@Test
public void testToString() {
assertEquals("(Object obj, int num)String", stringObjectInt.toString());
assertEquals("foo(Object obj, int num)String", namedStringObjectInt.toString());
}

/**
Expand Down Expand Up @@ -405,7 +406,13 @@ public void testCollect() {
assertEquals("bs", newSig.argName(1));
assertEquals(2, newSig.argCount());
}


private static final Signature namedStringObjectInt = Signature
.returning(String.class)
.appendArg("obj", Object.class)
.appendArg("num", int.class)
.displayName("foo");

private static final Signature stringObjectInt = Signature
.returning(String.class)
.appendArg("obj", Object.class)
Expand Down