public void addBean(final Object bean) { discoverDescriptors(bean); }
protected PreferenceDescriptor(final Preferences base, final Preference pref, final Setter setter) { // base could be null this.base = base; assert pref != null; this.spec = pref; assert setter != null; this.setter = setter; // Handle "" = null, since default values in annotations cannot be set to null if (pref.name() != null && pref.name().length() == 0) { this.name = null; } else { this.name = pref.name(); } this.system = pref.system(); // On IBM JDK, the value passed is null instead of the default value, so fix it in case this.type = pref.type() != null ? pref.type() : UNINITIALIZED_CLASS; // Handle "" = null, since default values in annotations cannot be set to null if (pref.path() != null && pref.path().length() == 0) { this.path = null; } else { this.path = pref.path(); } }
public String buildPath() { StringBuffer buff = new StringBuffer(); if (getBasePath() != null) { buff.append(getBasePath()); } if (base != null) { if (buff.length() != 0) { buff.append("/"); } buff.append(base.path()); } if (getPath() != null) { if (buff.length() != 0) { buff.append("/"); } buff.append(getPath()); } if (buff.length() == 0) { return null; } else { return buff.toString(); } }
@SuppressWarnings("unused") public String getPreferences() { if (pp.getDescriptors().isEmpty()) { return ""; } PrintBuffer buff = new PrintBuffer(); printHeader(buff, "section.preferences"); for (PreferenceDescriptor pd : pp.getDescriptors()) { String text = String.format(" %s @|bold %s|@ (%s)", pd.getPreferences().absolutePath(), pd.getId(), pd.getSetter().getType().getSimpleName()); buff.println(AnsiRenderer.render(text)); } return buff.toString(); }
@Preferences(path = "commands/help") public class HelpCommand extends CommandActionSupport @Preference @Option(name="c", longName="include-commands", optionalArg=true) private Boolean includeCommands = true; @Preference @Option(name="a", longName="include-aliases", optionalArg=true) private Boolean includeAliases = true; @Preference @Option(name="g", longName="include-groups", optionalArg=true) private Boolean includeGroups = true; @Preference @Option(name="m", longName="include-meta", optionalArg=true) private Boolean includeMeta = true; @Preference @Option(name="A", longName="include-all", optionalArg=true) private Boolean includeAll;
public void process() throws Exception { log.trace("Processing preference descriptors"); for (PreferenceDescriptor desc : descriptors) { log.trace("Descriptor: {}", desc); java.util.prefs.Preferences prefs = desc.getPreferences(); log.trace("Using preferences: {}", prefs); String key = desc.getId(); String value = prefs.get(key, null); log.trace(" {}={}", key, value); if (value != null) { Setter setter = desc.getSetter(); setter.set(Converters.getValue(setter.getType(), value)); } } } }
@Preference(name = "color") @Option(name = "C", longName = "color") protected void enableAnsiColors(final Boolean flag) { Ansi.setEnabled(flag); }
private void discoverDescriptor(final Preferences base, final Object bean, final AnnotatedElement element) { // base could be null assert bean != null; assert element != null; Preference pref = element.getAnnotation(Preference.class); if (pref != null) { log.trace("Discovered preference for: {}", element); PreferenceDescriptor desc = new PreferenceDescriptor(base, pref, SetterFactory.create(element, bean)); desc.setBasePath(getBasePath()); descriptors.add(desc); } }
public static PreferenceProcessor createProcessor(final CommandAction command) { assert command != null; PreferenceProcessor pp = new PreferenceProcessor(); Branding branding = ShellHolder.get().getBranding(); pp.setBasePath(branding.getPreferencesBasePath()); pp.addBean(command); return pp; } }
public String getId() { if (name != null) { return name; } return getSetter().getName(); }
public java.util.prefs.Preferences getPreferences() { String path = buildPath(); if (path == null) { Class type = getType(); if (system) { return java.util.prefs.Preferences.systemNodeForPackage(type); } else { return java.util.prefs.Preferences.userNodeForPackage(type); } } else { if (system) { return java.util.prefs.Preferences.systemRoot().node(path); } else { return java.util.prefs.Preferences.userRoot().node(path); } } } }
private void discoverDescriptors(final Object bean) { assert bean != null; // Recursively process all the methods/fields (@Inherited won't work here) for (Class<?> type = bean.getClass(); type != null; type = type.getSuperclass()) { Preferences base = type.getAnnotation(Preferences.class); for (Method method : type.getDeclaredMethods()) { discoverDescriptor(base, bean, method); } for (Field field : type.getDeclaredFields()) { discoverDescriptor(base, bean, field); } } }
@Preferences(path="commands/info") public class InfoCommand extends CommandActionSupport @Preference @Argument() private List<Section> sections;
@Preference(name = "terminal") @Option(name = "T", longName = "terminal") protected void setTerminalType(final String type) { TerminalFactory.configure(type); }
public Class getType() { Class type = this.type; if (type == UNINITIALIZED_CLASS) { type = getSetter().getBean().getClass(); } return type; }
@Preferences(path = "cli") public abstract class MainSupport protected boolean version; @Preference @Option(name = "e", longName = "errors") protected boolean showErrorTraces = false;
@Preference(name = "quiet") @Option(name = "q", longName = "quiet") protected void setQuiet(final boolean flag) { if (flag) { setConsoleLogLevel(Level.ERROR); io.setVerbosity(IO.Verbosity.QUIET); } }
@Preference(name = "debug") @Option(name = "d", longName = "debug") protected void setDebug(final boolean flag) { if (flag) { setConsoleLogLevel(Level.DEBUG); io.setVerbosity(IO.Verbosity.NORMAL); showErrorTraces = true; } }
@Preference(name = "trace") @Option(name = "X", longName = "trace") protected void setTrace(final boolean flag) { if (flag) { setConsoleLogLevel(Level.TRACE); io.setVerbosity(IO.Verbosity.NORMAL); showErrorTraces = true; } }