From 10b13b688da6e38547c6a6dc5331788f4ec81673 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 21 Jul 2008 01:32:57 -0400 Subject: [PATCH] Refactor SubcommandHandler to use CommandCatalog instead of reflection Now that all commands are known to the CommandCatalog we do not need to perform direct reflection inside of the SubcommandHandler. Instead we can reuse the lookup table already known to the CommandCatalog. Signed-off-by: Shawn O. Pearce --- .../src/org/spearce/jgit/pgm/TextBuiltin.java | 8 +-- .../spearce/jgit/pgm/opt/SubcommandHandler.java | 65 ++-------------------- 2 files changed, 6 insertions(+), 67 deletions(-) diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java index e2eef847..5c066cb0 100644 --- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java +++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java @@ -83,13 +83,7 @@ public abstract class TextBuiltin { /** RevWalk used during command line parsing, if it was required. */ protected RevWalk argWalk; - /** - * Set the name this command can be invoked as on the command line. - * - * @param name - * the name of the command. - */ - public void setCommandName(final String name) { + final void setCommandName(final String name) { commandName = name; } diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java index c7e1bf61..86004bb1 100644 --- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java +++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/opt/SubcommandHandler.java @@ -37,8 +37,6 @@ package org.spearce.jgit.pgm.opt; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.text.MessageFormat; import org.kohsuke.args4j.CmdLineException; @@ -47,7 +45,8 @@ import org.kohsuke.args4j.OptionDef; import org.kohsuke.args4j.spi.OptionHandler; import org.kohsuke.args4j.spi.Parameters; import org.kohsuke.args4j.spi.Setter; -import org.spearce.jgit.pgm.Main; +import org.spearce.jgit.pgm.CommandCatalog; +import org.spearce.jgit.pgm.CommandRef; import org.spearce.jgit.pgm.TextBuiltin; /** @@ -57,12 +56,6 @@ import org.spearce.jgit.pgm.TextBuiltin; * we can execute at runtime with the remaining arguments of the parser. */ public class SubcommandHandler extends OptionHandler { - private static String mypackage() { - final String p = Main.class.getName(); - final int dot = p.lastIndexOf('.'); - return p.substring(0, dot); - } - /** * Create a new handler for the command name. *

@@ -80,65 +73,17 @@ public class SubcommandHandler extends OptionHandler { @Override public int parseArguments(final Parameters params) throws CmdLineException { final String name = params.getParameter(0); - final StringBuilder s = new StringBuilder(); - s.append(mypackage()); - s.append('.'); - boolean upnext = true; - for (int i = 0; i < name.length(); i++) { - final char c = name.charAt(i); - if (c == '-') { - upnext = true; - continue; - } - if (upnext) - s.append(Character.toUpperCase(c)); - else - s.append(c); - upnext = false; - } - - final Class clazz; - try { - clazz = Class.forName(s.toString()); - } catch (ClassNotFoundException e) { - throw new CmdLineException(MessageFormat.format( - "{0} is not a jgit command", name)); - } - - if (!TextBuiltin.class.isAssignableFrom(clazz)) + final CommandRef cr = CommandCatalog.get(name); + if (cr == null) throw new CmdLineException(MessageFormat.format( "{0} is not a jgit command", name)); - final Constructor cons; - try { - cons = clazz.getDeclaredConstructor(); - } catch (SecurityException e) { - throw new CmdLineException("Cannot create " + name, e); - } catch (NoSuchMethodException e) { - throw new CmdLineException("Cannot create " + name, e); - } - cons.setAccessible(true); - - final TextBuiltin cmd; - try { - cmd = (TextBuiltin) cons.newInstance(); - } catch (InstantiationException e) { - throw new CmdLineException("Cannot create " + name, e); - } catch (IllegalAccessException e) { - throw new CmdLineException("Cannot create " + name, e); - } catch (InvocationTargetException e) { - throw new CmdLineException("Cannot create " + name, e); - } - - cmd.setCommandName(name); - setter.addValue(cmd); - // Force option parsing to stop. Everything after us should // be arguments known only to this command and must not be // recognized by the current parser. // owner.stopOptionParsing(); - + setter.addValue(cr.create()); return 1; } -- 2.11.4.GIT