Created
May 13, 2013 12:48
-
-
Save RomanSaveljev/5568090 to your computer and use it in GitHub Desktop.
Generate Java class, which overrides all public methods of the ancestor and prints them as a code with empty bodies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String EXAMPLE_PARAMETER_NAMES = "abcdefghijklmnopqrstuvwxyz"; | |
private void dumpMethods(Class<?> cl) | |
{ | |
System.out.println("// Methods from " + cl.getCanonicalName()); | |
Method[] methods = cl.getMethods(); | |
for (int i = 0; i < methods.length; i++ ) | |
{ | |
Method m = methods[i]; | |
if (Modifier.isPublic(m.getModifiers()) || Modifier.isProtected(m.getModifiers())) | |
{ | |
String methodBody = m.getReturnType().getCanonicalName() == "void" ? "super." : "return super."; | |
System.out.print(Modifier.toString(m.getModifiers()) + " "); | |
System.out.print(m.getReturnType().getCanonicalName() + " "); | |
System.out.print(m.getName() + "("); | |
methodBody += m.getName() + "("; | |
Class<?>[] parameters = m.getParameterTypes(); | |
for (int j = 0; j < parameters.length; j++) | |
{ | |
Class<?> p = parameters[j]; | |
System.out.print(p.getCanonicalName() + " " + EXAMPLE_PARAMETER_NAMES.charAt(j)); | |
methodBody += EXAMPLE_PARAMETER_NAMES.charAt(j); | |
if (j < parameters.length - 1) | |
{ | |
System.out.print(", "); | |
methodBody += ", "; | |
} | |
} | |
System.out.println(")"); | |
methodBody += ");"; | |
System.out.println("{"); | |
System.out.println("\t" + methodBody); | |
System.out.println("}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment