Parcourir la source

add const printing utility

tags/2.0.1
Jonathan Cobb il y a 5 ans
Parent
révision
fb7fe350cd
2 fichiers modifiés avec 51 ajouts et 0 suppressions
  1. +38
    -0
      src/main/java/org/cobbzilla/util/main/ConstMain.java
  2. +13
    -0
      src/main/java/org/cobbzilla/util/main/ConstOptions.java

+ 38
- 0
src/main/java/org/cobbzilla/util/main/ConstMain.java Voir le fichier

@@ -0,0 +1,38 @@
package org.cobbzilla.util.main;

import static org.cobbzilla.util.daemon.ZillaRuntime.shortError;
import static org.cobbzilla.util.reflect.ReflectionUtil.constValue;
import static org.cobbzilla.util.reflect.ReflectionUtil.forName;

/**
* Write a constant value to stdout.
*
* This will only ever write the constant value to stdout if it can successfully be read.
* If the constant can be read, its value is printed with the .toString() method.
* If an error occurs, nothing is written to stdout, and the error will be written to stderr.
* If the value of the constant is null, nothing is printed to stdout, and "null" is printed to stderr.
*/
public class ConstMain extends BaseMain<ConstOptions> {

public static void main(String[] args) { main(ConstMain.class, args); }

@Override protected void run() throws Exception {
final String cm = getOptions().getClassAndMember();
final int lastDot = cm.lastIndexOf('.');
if (lastDot == -1 || lastDot == cm.length()-1) {
die("invalid value: "+cm);
} else {
try {
final Object val = constValue(forName(cm.substring(0, lastDot)), cm.substring(lastDot + 1));
if (val == null) {
err("null");
} else {
out(val.toString());
}
} catch (Exception e) {
die("Exception getting constant value "+cm+": "+shortError(e), e);
}
}
}

}

+ 13
- 0
src/main/java/org/cobbzilla/util/main/ConstOptions.java Voir le fichier

@@ -0,0 +1,13 @@
package org.cobbzilla.util.main;

import lombok.Getter;
import lombok.Setter;
import org.kohsuke.args4j.Argument;

public class ConstOptions extends BaseMainOptions {

public static final String USAGE_CLASS_AND_MEMBER = "Specify a class.member constant";
@Argument(usage=USAGE_CLASS_AND_MEMBER, required=true)
@Getter @Setter private String classAndMember;

}

Chargement…
Annuler
Enregistrer