本文整理汇总了Java中com.sun.codemodel.JEnumConstant类的典型用法代码示例。如果您正苦于以下问题:Java JEnumConstant类的具体用法?Java JEnumConstant怎么用?Java JEnumConstant使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JEnumConstant类属于com.sun.codemodel包,在下文中一共展示了JEnumConstant类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addEnumConstants
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private void addEnumConstants(JsonNode node, JDefinedClass _enum, JsonNode customNames, JType type) {
Collection<String> existingConstantNames = new ArrayList<String>();
for (int i = 0; i < node.size(); i++) {
JsonNode value = node.path(i);
if (!value.isNull()) {
String constantName = getConstantName(value.asText(), customNames.path(i).asText());
constantName = makeUnique(constantName, existingConstantNames);
existingConstantNames.add(constantName);
JEnumConstant constant = _enum.enumConstant(constantName);
constant.arg(DefaultRule.getDefaultValue(type, value));
ruleFactory.getAnnotator().enumConstant(constant, value.asText());
}
}
}
示例2: shouldFindEnumValues
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldFindEnumValues() throws IllegalAccessException {
klass = modelPackage._getClass("Colour");
assertThat(klass.getClassType()).isEqualTo(ClassType.ENUM);
Map<String,JEnumConstant> enums = (Map<String, JEnumConstant>) FieldUtils.readField(klass, "enumConstantsByName", true);
assertThat(enums.keySet()).contains("LIGHT_BLUE", "RED");
JEnumConstant lightBlue = enums.get("LIGHT_BLUE");
assertThat(lightBlue.javadoc().get(0)).isEqualTo("Colour of the sky");
List<JExpression> args = (List<JExpression>) FieldUtils.readField(lightBlue, "args", true);
assertThat(args).hasSize(1);
assertThat(args.get(0)).isInstanceOf(JStringLiteral.class);
String literal = (String) FieldUtils.readField(args.get(0), "str", true);
assertThat(literal).isEqualTo("lightBlue");
}
示例3: caseAEnumConstant
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
@Override
public void caseAEnumConstant(AEnumConstant node) {
// TypeModifierAdapter modap = new TypeModifierAdapter();
// for (PModifier m : node.getModifier()) {
// m.apply(modap);
// }
// int mods = modap.getMods();
if (!node.getModifier().isEmpty()) {
Logger.error(context.getFile(), node.getIdentifier(), "CodeModel does not support modifiers for enum constants, yet...");
}
JEnumConstant ec = clazz.enumConstant(node.getIdentifier().getText());
EnumArgumentable ea = new EnumArgumentable(ec);
for (PArgument arg : node.getArgument()) {
ArgumentAdapter aa = new ArgumentAdapter(ea, context);
arg.apply(aa);
}
if (node.getClassBody() != null) {
Logger.error(context.getFile(), node.getIdentifier(), "CodeModel does not support class bodies for enum constants, yet...");
}
}
示例4: defineAction
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private JEnumConstant defineAction(Action a){
String name = this.getActionEnumName(a);
JEnumConstant actionConst = actionsEnum.enumConstant(name);
if(definedActions.contains(a)){
return actionConst;
}
JBlock body = advanceActionSwitch._case(JExpr.ref(name)).body();
switch(a.id()){
case ACCEPT:
body._return(f.getStatusEnum().staticRef("COMPLETE"));
break;
case GOTO:
this.defineShiftGotoAction(a, body);
break;
case REDUCE:
this.defineReduceAction(a, body);
break;
case SHIFT:
this.defineShiftGotoAction(a, body);
break;
default:
throw new Error();
}
definedActions.add(a);
return actionConst;
}
示例5: buildEnumClass
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private JDefinedClass buildEnumClass(String enumName, List<String> values) throws JClassAlreadyExistsException {
if (pkg.isDefined(enumName)) {
return pkg._getClass(enumName);
}
JDefinedClass valueEnum = pkg._enum(enumName);
JFieldVar valField = valueEnum.field(JMod.PRIVATE | JMod.FINAL, String.class, "value");
JMethod ctor = valueEnum.constructor(JMod.PRIVATE);
JVar param = ctor.param(String.class, "val");
ctor.body().assign(valField, param);
JMethod toString = valueEnum.method(JMod.PUBLIC, String.class, "toString");
toString.annotate(Override.class);
toString.body()._return(valField);
for (String value : values) {
JEnumConstant sortCnst = valueEnum.enumConstant(value.toUpperCase().replace(' ', '_'));
sortCnst.arg(JExpr.lit(value));
}
return valueEnum;
}
示例6: generateEnumConstant
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private void generateEnumConstant(JDefinedClass klass, EnumValue enumValue) {
JEnumConstant constant = klass.enumConstant(Names.buildConstantName(enumValue.getName()))
.arg(JExpr.lit(enumValue.getName()));
if (context.getConfig().isJacksonPropertyName()) {
constant.annotate(JsonProperty.class).param(VALUE, enumValue.getName());
}
if (enumValue.getDescription() != null) {
constant.javadoc().add(enumValue.getDescription());
}
}
示例7: shouldFindEnumValues
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void shouldFindEnumValues() throws IllegalAccessException {
klass = modelPackage._getClass("Colour");
assertThat(klass.getClassType()).isEqualTo(ClassType.ENUM);
Map<String,JEnumConstant> enums = (Map<String, JEnumConstant>) FieldUtils.readField(klass, "enumConstantsByName", true);
assertThat(enums.keySet()).containsExactly("LIGHT_BLUE", "RED", "YELLOW", "GREEN");
}
示例8: parseEnumMember
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
@SuppressWarnings("unused")
private void parseEnumMember(Element root, ParsingContext context) {
JDefinedClass enumClass = (JDefinedClass) context.getCmNode();
JEnumConstant enumConstant = enumClass.enumConstant(
NameUtils.camelToUpper(enumClass.name()) + '_' + root.getAttributeValue("name").toUpperCase()
);
long value = Long.parseLong(root.getAttributeValue("value"));
enumConstant.arg(JExpr.lit(value));
System.out.println("--> " + enumConstant.getName() + " (" + value + ")");
}
示例9: withEnum
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
public <T> EnumBuilder withEnum(T name, Class<T> type) {
pojoCreationCheck();
String cleaned = NamingHelper.cleanNameForJavaEnum(name.toString());
if (!doesEnumContainField(type, cleaned)) {
withValueField(type);
ENUM_CACHE.put(cleaned, true);
logger.debug("Adding field: " + name + " to " + this.pojo.name());
if (StringUtils.hasText(cleaned)) {
JEnumConstant enumConstant = this.pojo.enumConstant(cleaned);
if (type.equals(Integer.class)) {
enumConstant.arg(JExpr.lit((Integer)name));
} else if (type.equals(Boolean.class)) {
enumConstant.arg(JExpr.lit((Boolean)name));
} else if (type.equals(Double.class)) {
enumConstant.arg(JExpr.lit((Double)name));
} else if (type.equals(Float.class)) {
enumConstant.arg(JExpr.lit((Float)name));
} else if (type.equals(Long.class)) {
enumConstant.arg(JExpr.lit((Long)name));
} else {
enumConstant.arg(JExpr.lit(name.toString()));
enumConstant.annotate(JsonProperty.class).param("value", name.toString());
}
}
}
return this;
}
示例10: CMEnumConstantOutline
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
public CMEnumConstantOutline(MEnumOutline enumOutline,
MEnumConstantInfo<NType, NClass> target, JEnumConstant code) {
Validate.notNull(enumOutline);
Validate.notNull(target);
Validate.notNull(code);
this.enumOutline = enumOutline;
this.target = target;
this.code = code;
}
示例11: generateEnum
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private JDefinedClass generateEnum(EnumDataSchema schema) throws JClassAlreadyExistsException
{
JDefinedClass enumClass = getPackage(schema.getNamespace())._class(JMod.PUBLIC, escapeReserved(schema.getName()), ClassType.ENUM);
enumClass.javadoc().append(schema.getDoc());
setDeprecatedAnnotationAndJavadoc(schema, enumClass);
registerGeneratedClass(schema, enumClass);
generateSchemaField(enumClass, schema);
for (String value: schema.getSymbols())
{
if (isReserved(value))
{
throw new IllegalArgumentException("Enum contains Java reserved symbol: " + value + " schema: " + schema);
}
JEnumConstant enumConstant = enumClass.enumConstant(value);
String enumConstantDoc = schema.getSymbolDocs().get(value);
if (enumConstantDoc != null)
{
enumConstant.javadoc().append(enumConstantDoc);
}
setDeprecatedAnnotationAndJavadoc(schema, value, enumConstant);
}
enumClass.enumConstant(DataTemplateUtil.UNKNOWN_ENUM);
return enumClass;
}
示例12: setDeprecatedAnnotationAndJavadoc
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private void setDeprecatedAnnotationAndJavadoc(EnumDataSchema enumSchema, String symbol, JEnumConstant constant)
{
Object deprecatedSymbolsProp = enumSchema.getProperties().get(DEPRECATED_SYMBOLS_KEY);
if (deprecatedSymbolsProp instanceof DataMap)
{
DataMap deprecatedSymbols = (DataMap)deprecatedSymbolsProp;
Object deprecatedProp = deprecatedSymbols.get(symbol);
setDeprecatedAnnotationAndJavadoc(deprecatedProp, constant, constant);
}
}
示例13: createStateActionMethod
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private JMethod createStateActionMethod(State s){
JMethod method = clazz.method(JMod.PRIVATE, actionsEnum,
"getState"+stateIndex.get(s)+"Action");
JVar lookahead = method.param(f.getNodeInterface().narrow(nodes.getVisitorInterface()), "lookahead");
JSwitch sw = method.body()._switch(symbolGen.castTo(JExpr.invoke(lookahead, "symbol")));
List<JExpression> expected = new ArrayList<JExpression>();
//add a case statement for each valid lookahead symbol
for(String symbol : nodes.getAllSymbols()){
Action a = table.getAction(s, symbol);
if(a != null){
JEnumConstant actionConst = this.defineAction(a);
sw._case(symbolGen.getRef(symbol)).body()._return(actionConst);
expected.add(symbolGen.getSymbolObj(symbol));
}
}
//the default case should return error action
sw._default().body()._return(actionsEnum.enumConstant("ERROR"));
JArray array = JExpr.newArray(f.getSymbolInterface());
for(JExpression exp : expected){
array.add(exp);
}
expectedStateSwitch._case(JExpr.lit(stateIndex.get(s)))
.body()._return(array);
return method;
}
示例14: getSymbolObj
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
public JExpression getSymbolObj(String symbol){
JEnumConstant obj = enums.get(symbol);
if(obj == null){
obj = clazz.enumConstant(symbol.toUpperCase());
String pseudo = g.getPseudonym(symbol);
if(pseudo != null){
obj.arg(JExpr.lit(pseudo));
}
enums.put(symbol, obj);
}
return obj;
}
示例15: getParamTypeEnum
import com.sun.codemodel.JEnumConstant; //导入依赖的package包/类
private JClass getParamTypeEnum(Filter filter) throws JClassAlreadyExistsException {
String enumName = camel(filter.getName(), true) + "Option";
if (pkg.isDefined(enumName)) {
return pkg._getClass(enumName);
}
List<Option> options = filter.getOption();
if (options.size() == 1) {/*turn into '*Only' method?*/}
if (options.size() == 2) {
if (ImmutableSet.of("true","false").equals(
ImmutableSet.of(options.get(0).getValue().toLowerCase(),
options.get(1).getValue().toLowerCase()
)
)) {
return model.ref(Boolean.class);
}
}
JDefinedClass valueEnum = pkg._enum(enumName);
JFieldVar valField = valueEnum.field(JMod.PRIVATE|JMod.FINAL, String.class, "value");
JMethod ctor = valueEnum.constructor(JMod.PRIVATE);
JVar param = ctor.param(String.class, "val");
ctor.body().assign(valField, param);
JMethod toString = valueEnum.method(JMod.PUBLIC, String.class, "toString");
toString.annotate(Override.class);
toString.body()._return(valField);
for (Option option : options) {
String optionName = option.getValue().toUpperCase().replace(' ', '_');
JEnumConstant optionCst = valueEnum.enumConstant(optionName);
optionCst.arg(JExpr.lit(option.getValue()));
}
return valueEnum;
}