Skip to content

Commit

Permalink
Add init() method to AbstractEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron1011 committed Aug 27, 2015
1 parent eac455a commit 427a005
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/spongepowered/api/event/AbstractEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public CallbackList getCallbacks() {
return this.callbacks;
}

/**
* Called once all fields have been set by the generated
* constructor in a subclass.
*
* <p>This method should be used
* to initialize any fields that depend on parameters
* passed to the constructor.</p>
*/
protected void init() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ public static boolean hasImplementation(@Nullable Class<?> type, final Method me
return false;
}

public static boolean hasDeclaredMethod(Class<?> type, String name, Class<?>... params) {
while (type != null) {
try {
type.getDeclaredMethod(name, params);
return true;
} catch (NoSuchMethodException ignored) {
// Try the superclass
}

type = type.getSuperclass();
}

return false;
}

/**
* Get the policy regarding how null parameters are handled.
*
Expand Down Expand Up @@ -355,6 +370,12 @@ public byte[] createClass(final Class<?> type, final String name, final Class<?>
mv.visitLabel(afterException);
}

// super.init();
if (hasDeclaredMethod(parentType, "init")) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(parentType), "init", "()V", false);
}

mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
Expand Down

0 comments on commit 427a005

Please sign in to comment.