Initial JGit contribution to eclipse.org
[jgit/MarioXXX.git] / org.eclipse.jgit.pgm / src / org / eclipse / jgit / pgm / CommandRef.java
blobeb68ada9b743161d4527cd9a5ad474c3dd94f5ca
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * and other copyright owners as documented in the project's IP log.
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 package org.eclipse.jgit.pgm;
46 import java.lang.reflect.Constructor;
47 import java.lang.reflect.InvocationTargetException;
49 /**
50 * Description of a command (a {@link TextBuiltin} subclass.
51 * <p>
52 * These descriptions are lightweight compared to creating a command instance
53 * and are therefore suitable for catalogs of "known" commands without linking
54 * the command's implementation and creating a dummy instance of the command.
56 public class CommandRef {
57 private final Class<? extends TextBuiltin> impl;
59 private final String name;
61 private String usage;
63 boolean common;
65 CommandRef(final Class<? extends TextBuiltin> clazz) {
66 this(clazz, guessName(clazz));
69 CommandRef(final Class<? extends TextBuiltin> clazz, final Command cmd) {
70 this(clazz, cmd.name().length() > 0 ? cmd.name() : guessName(clazz));
71 usage = cmd.usage();
72 common = cmd.common();
75 private CommandRef(final Class<? extends TextBuiltin> clazz, final String cn) {
76 impl = clazz;
77 name = cn;
78 usage = "";
81 private static String guessName(final Class<? extends TextBuiltin> clazz) {
82 final StringBuilder s = new StringBuilder();
83 if (clazz.getName().startsWith("org.eclipse.jgit.pgm.debug."))
84 s.append("debug-");
86 boolean lastWasDash = true;
87 for (final char c : clazz.getSimpleName().toCharArray()) {
88 if (Character.isUpperCase(c)) {
89 if (!lastWasDash)
90 s.append('-');
91 lastWasDash = !lastWasDash;
92 s.append(Character.toLowerCase(c));
93 } else {
94 s.append(c);
97 return s.toString();
101 * @return name the command is invoked as from the command line.
103 public String getName() {
104 return name;
108 * @return one line description of the command's feature set.
110 public String getUsage() {
111 return usage;
115 * @return true if this command is considered to be commonly used.
117 public boolean isCommon() {
118 return common;
122 * @return name of the Java class which implements this command.
124 public String getImplementationClassName() {
125 return impl.getName();
129 * @return loader for {@link #getImplementationClassName()}.
131 public ClassLoader getImplementationClassLoader() {
132 return impl.getClassLoader();
136 * @return a new instance of the command implementation.
138 public TextBuiltin create() {
139 final Constructor<? extends TextBuiltin> c;
140 try {
141 c = impl.getDeclaredConstructor();
142 } catch (SecurityException e) {
143 throw new RuntimeException("Cannot create command " + getName(), e);
144 } catch (NoSuchMethodException e) {
145 throw new RuntimeException("Cannot create command " + getName(), e);
147 c.setAccessible(true);
149 final TextBuiltin r;
150 try {
151 r = c.newInstance();
152 } catch (InstantiationException e) {
153 throw new RuntimeException("Cannot create command " + getName(), e);
154 } catch (IllegalAccessException e) {
155 throw new RuntimeException("Cannot create command " + getName(), e);
156 } catch (IllegalArgumentException e) {
157 throw new RuntimeException("Cannot create command " + getName(), e);
158 } catch (InvocationTargetException e) {
159 throw new RuntimeException("Cannot create command " + getName(), e);
161 r.setCommandName(getName());
162 return r;