You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandRef.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.pgm;
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.text.MessageFormat;
  14. import org.eclipse.jgit.pgm.internal.CLIText;
  15. /**
  16. * Description of a command (a {@link org.eclipse.jgit.pgm.TextBuiltin}
  17. * subclass).
  18. * <p>
  19. * These descriptions are lightweight compared to creating a command instance
  20. * and are therefore suitable for catalogs of "known" commands without linking
  21. * the command's implementation and creating a dummy instance of the command.
  22. */
  23. public class CommandRef {
  24. private final Class<? extends TextBuiltin> impl;
  25. private final String name;
  26. private String usage;
  27. boolean common;
  28. CommandRef(Class<? extends TextBuiltin> clazz) {
  29. this(clazz, guessName(clazz));
  30. }
  31. CommandRef(Class<? extends TextBuiltin> clazz, Command cmd) {
  32. this(clazz, cmd.name().length() > 0 ? cmd.name() : guessName(clazz));
  33. usage = cmd.usage();
  34. common = cmd.common();
  35. }
  36. private CommandRef(Class<? extends TextBuiltin> clazz, String cn) {
  37. impl = clazz;
  38. name = cn;
  39. usage = ""; //$NON-NLS-1$
  40. }
  41. private static String guessName(Class<? extends TextBuiltin> clazz) {
  42. final StringBuilder s = new StringBuilder();
  43. if (clazz.getName().startsWith("org.eclipse.jgit.pgm.debug.")) //$NON-NLS-1$
  44. s.append("debug-"); //$NON-NLS-1$
  45. boolean lastWasDash = true;
  46. for (char c : clazz.getSimpleName().toCharArray()) {
  47. if (Character.isUpperCase(c)) {
  48. if (!lastWasDash)
  49. s.append('-');
  50. lastWasDash = !lastWasDash;
  51. s.append(Character.toLowerCase(c));
  52. } else {
  53. s.append(c);
  54. lastWasDash = false;
  55. }
  56. }
  57. return s.toString();
  58. }
  59. /**
  60. * Get the <code>name</code>.
  61. *
  62. * @return name the command is invoked as from the command line.
  63. */
  64. public String getName() {
  65. return name;
  66. }
  67. /**
  68. * Get <code>usage</code>.
  69. *
  70. * @return one line description of the command's feature set.
  71. */
  72. public String getUsage() {
  73. return usage;
  74. }
  75. /**
  76. * Is this command commonly used
  77. *
  78. * @return true if this command is considered to be commonly used.
  79. */
  80. public boolean isCommon() {
  81. return common;
  82. }
  83. /**
  84. * Get implementation class name
  85. *
  86. * @return name of the Java class which implements this command.
  87. */
  88. public String getImplementationClassName() {
  89. return impl.getName();
  90. }
  91. /**
  92. * Get implementation class loader
  93. *
  94. * @return loader for {@link #getImplementationClassName()}.
  95. */
  96. public ClassLoader getImplementationClassLoader() {
  97. return impl.getClassLoader();
  98. }
  99. /**
  100. * Create an instance of the command implementation
  101. *
  102. * @return a new instance of the command implementation.
  103. */
  104. public TextBuiltin create() {
  105. final Constructor<? extends TextBuiltin> c;
  106. try {
  107. c = impl.getDeclaredConstructor();
  108. } catch (SecurityException | NoSuchMethodException e) {
  109. throw new RuntimeException(MessageFormat
  110. .format(CLIText.get().cannotCreateCommand, getName(), e));
  111. }
  112. c.setAccessible(true);
  113. final TextBuiltin r;
  114. try {
  115. r = c.newInstance();
  116. } catch (InstantiationException | IllegalAccessException
  117. | IllegalArgumentException | InvocationTargetException e) {
  118. throw new RuntimeException(MessageFormat
  119. .format(CLIText.get().cannotCreateCommand, getName(), e));
  120. }
  121. r.setCommandName(getName());
  122. return r;
  123. }
  124. /** {@inheritDoc} */
  125. @SuppressWarnings("nls")
  126. @Override
  127. public String toString() {
  128. return "CommandRef [impl=" + impl + ", name=" + name + ", usage="
  129. + CLIText.get().resourceBundle().getString(usage) + ", common="
  130. + common + "]";
  131. }
  132. }