本文整理匯總了Java中com.sun.codemodel.JType類的典型用法代碼示例。如果您正苦於以下問題:Java JType類的具體用法?Java JType怎麽用?Java JType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JType類屬於com.sun.codemodel包,在下文中一共展示了JType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getObjectRule
import com.sun.codemodel.JType; //導入依賴的package包/類
@Override
public org.jsonschema2pojo.rules.Rule<JPackage, JType> getObjectRule() {
final org.jsonschema2pojo.rules.Rule<JPackage, JType> workingRule = super.getObjectRule();
return new org.jsonschema2pojo.rules.Rule<JPackage, JType>() {
@Override
public JType apply(String nodeName, JsonNode node, JPackage generatableType, Schema currentSchema) {
JType objectType = workingRule.apply(nodeName, node, generatableType, currentSchema);
if( objectType instanceof JDefinedClass ) {
JDefinedClass jclass = (JDefinedClass)objectType;
jclass.method(JMod.PUBLIC, jclass.owner().BOOLEAN, "brokenMethod").body();
}
return objectType;
}
};
}
示例2: addFactoryMethod
import com.sun.codemodel.JType; //導入依賴的package包/類
private void addFactoryMethod(JDefinedClass _enum, JType backingType) {
JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType);
JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue");
JVar valueParam = fromValue.param(backingType, "value");
JBlock body = fromValue.body();
JVar constant = body.decl(_enum, "constant");
constant.init(quickLookupMap.invoke("get").arg(valueParam));
JConditional _if = body._if(constant.eq(JExpr._null()));
JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class));
JExpression expr = valueParam;
// if string no need to add ""
if(!isString(backingType)){
expr = expr.plus(JExpr.lit(""));
}
illegalArgumentException.arg(expr);
_if._then()._throw(illegalArgumentException);
_if._else()._return(constant);
ruleFactory.getAnnotator().enumCreatorMethod(fromValue);
}
示例3: getDefaultSet
import com.sun.codemodel.JType; //導入依賴的package包/類
/**
* Creates a default value for a set property by:
* <ol>
* <li>Creating a new {@link LinkedHashSet} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
* correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link Set} with
* some generic type argument)
* @param node
* the node containing default values for this set
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultSet(JType fieldType, JsonNode node) {
JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
setImplClass = setImplClass.narrow(setGenericType);
JInvocation newSetImpl = JExpr._new(setImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
}
newSetImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newSetImpl;
}
示例4: addPublicGetMethod
import com.sun.codemodel.JType; //導入依賴的package包/類
private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) {
JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME);
JTypeVar returnType = method.generify("T");
method.type(returnType);
Models.suppressWarnings(method, "unchecked");
JVar nameParam = method.param(String.class, "name");
JBlock body = method.body();
JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value",
invoke(internalGetMethod).arg(nameParam).arg(notFoundValue));
JConditional found = method.body()._if(notFoundValue.ne(valueVar));
found._then()._return(cast(returnType, valueVar));
JBlock notFound = found._else();
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam)));
} else {
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
return method;
}
示例5: addPublicSetMethod
import com.sun.codemodel.JType; //導入依賴的package包/類
private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) {
JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
// if we have additional properties, then put value.
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
.arg(cast(additionalPropertiesType, valueParam)));
}
// else throw exception.
else {
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
return method;
}
示例6: addPublicWithMethod
import com.sun.codemodel.JType; //導入依賴的package包/類
private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) {
JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar valueParam = method.param(Object.class, "value");
JBlock body = method.body();
JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then();
// if we have additional properties, then put value.
JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {});
if (getAdditionalProperties != null) {
JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1);
notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam)
.arg(cast(additionalPropertiesType, valueParam)));
}
// else throw exception.
else {
notFound._throw(illegalArgumentInvocation(jclass, nameParam));
}
body._return(_this());
return method;
}
示例7: addEnumConstants
import com.sun.codemodel.JType; //導入依賴的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());
}
}
}
示例8: getDefaultList
import com.sun.codemodel.JType; //導入依賴的package包/類
/**
* Creates a default value for a list property by:
* <ol>
* <li>Creating a new {@link ArrayList} with the correct generic type
* <li>Using {@link Arrays#asList(Object...)} to initialize the list with
* the correct default values
* </ol>
*
* @param fieldType
* the java type that applies for this field ({@link List} with
* some generic type argument)
* @param node
* the node containing default values for this list
* @return an expression that creates a default value that can be assigned
* to this field
*/
private JExpression getDefaultList(JType fieldType, JsonNode node) {
JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0);
JClass listImplClass = fieldType.owner().ref(ArrayList.class);
listImplClass = listImplClass.narrow(listGenericType);
JInvocation newListImpl = JExpr._new(listImplClass);
if (node instanceof ArrayNode && node.size() > 0) {
JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
for (JsonNode defaultValue : node) {
invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
}
newListImpl.arg(invokeAsList);
} else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
return JExpr._null();
}
return newListImpl;
}
示例9: visitDecimal9Constant
import com.sun.codemodel.JType; //導入依賴的package包/類
@Override
public HoldingContainer visitDecimal9Constant(Decimal9Expression e, ClassGenerator<?> generator)
throws RuntimeException {
MajorType majorType = e.getMajorType();
JBlock setup = generator.getBlock(BlockType.SETUP);
JType holderType = generator.getHolderType(majorType);
JVar var = generator.declareClassField("dec9", holderType);
JExpression valueLiteral = JExpr.lit(e.getIntFromDecimal());
JExpression scaleLiteral = JExpr.lit(e.getScale());
JExpression precisionLiteral = JExpr.lit(e.getPrecision());
setup.assign(
var,
generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getDecimal9Holder").arg(valueLiteral)
.arg(scaleLiteral).arg(precisionLiteral));
return new HoldingContainer(majorType, var, null, null);
}
示例10: createClassInstances
import com.sun.codemodel.JType; //導入依賴的package包/類
private List<Partitioner> createClassInstances(int actualPartitions) throws SchemaChangeException {
// set up partitioning function
final LogicalExpression expr = config.getExpr();
final ClassGenerator<Partitioner> cg = context.getClassProducer().createGenerator(Partitioner.TEMPLATE_DEFINITION).getRoot();
ClassGenerator<Partitioner> cgInner = cg.getInnerGenerator("OutgoingRecordBatch");
final LogicalExpression materializedExpr = context.getClassProducer().materialize(expr, incoming);
// generate code to copy from an incoming value vector to the destination partition's outgoing value vector
JExpression bucket = JExpr.direct("bucket");
// generate evaluate expression to determine the hash
ClassGenerator.HoldingContainer exprHolder = cg.addExpr(materializedExpr);
cg.getEvalBlock().decl(JType.parse(cg.getModel(), "int"), "bucket", exprHolder.getValue().mod(JExpr.lit(outGoingBatchCount)));
cg.getEvalBlock()._return(cg.getModel().ref(Math.class).staticInvoke("abs").arg(bucket));
CopyUtil.generateCopies(cgInner, incoming, incoming.getSchema().getSelectionVectorMode() == SelectionVectorMode.FOUR_BYTE);
// compile and setup generated code
List<Partitioner> subPartitioners = cg.getCodeGenerator().getImplementationClass(actualPartitions);
return subPartitioners;
}
示例11: visitDecimal18Constant
import com.sun.codemodel.JType; //導入依賴的package包/類
@Override
public HoldingContainer visitDecimal18Constant(Decimal18Expression e, ClassGenerator<?> generator)
throws RuntimeException {
MajorType majorType = e.getMajorType();
JBlock setup = generator.getBlock(BlockType.SETUP);
JType holderType = generator.getHolderType(majorType);
JVar var = generator.declareClassField("dec18", holderType);
JExpression valueLiteral = JExpr.lit(e.getLongFromDecimal());
JExpression scaleLiteral = JExpr.lit(e.getScale());
JExpression precisionLiteral = JExpr.lit(e.getPrecision());
setup.assign(
var,
generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getDecimal18Holder").arg(valueLiteral)
.arg(scaleLiteral).arg(precisionLiteral));
return new HoldingContainer(majorType, var, null, null);
}
示例12: applyGeneratesDate
import com.sun.codemodel.JType; //導入依賴的package包/類
@Test
public void applyGeneratesDate() {
JPackage jpackage = new JCodeModel()._package(getClass().getPackage().getName());
ObjectNode objectNode = new ObjectMapper().createObjectNode();
objectNode.put("type", "string");
TextNode formatNode = TextNode.valueOf("date-time");
objectNode.set("format", formatNode);
JType mockDateType = mock(JType.class);
FormatRule mockFormatRule = mock(FormatRule.class);
when(mockFormatRule.apply(eq("fooBar"), eq(formatNode), Mockito.isA(JType.class), isNull(Schema.class))).thenReturn(mockDateType);
when(ruleFactory.getFormatRule()).thenReturn(mockFormatRule);
JType result = rule.apply("fooBar", objectNode, jpackage, null);
assertThat(result, equalTo(mockDateType));
}
示例13: visitDecimalConstant
import com.sun.codemodel.JType; //導入依賴的package包/類
@Override
public HoldingContainer visitDecimalConstant(DecimalExpression e, ClassGenerator<?> generator)
throws RuntimeException {
CompleteType majorType= e.getCompleteType();
JBlock setup = generator.getBlock(BlockType.SETUP);
JType holderType = majorType.getHolderType(generator.getModel());
JVar var = generator.declareClassField("dec", holderType);
JExpression valueLiteral = JExpr.lit(e.getIntFromDecimal());
JExpression scaleLiteral = JExpr.lit(e.getScale());
JExpression precisionLiteral = JExpr.lit(e.getPrecision());
setup.assign(
var,
generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getNullableDecimalHolder").arg(valueLiteral)
.arg(scaleLiteral).arg(precisionLiteral));
return new HoldingContainer(majorType, var, var.ref("value"), var.ref("isSet"));
}
示例14: generate
import com.sun.codemodel.JType; //導入依賴的package包/類
public JType generate(JCodeModel codeModel, String className, String packageName, String json) throws IOException {
JPackage jpackage = codeModel._package(packageName);
JsonNode schemaNode = null;
if (ruleFactory.getGenerationConfig().getSourceType() == SourceType.JSON) {
JsonNode jsonNode = objectMapper().readTree(json);
schemaNode = schemaGenerator.schemaFromExample(jsonNode);
} else {
schemaNode = objectMapper().readTree(json);
}
return ruleFactory.getSchemaRule().apply(className, schemaNode, jpackage, new Schema(null, schemaNode, schemaNode));
}
示例15: addInternalGetMethodJava7
import com.sun.codemodel.JType; //導入依賴的package包/類
private JMethod addInternalGetMethodJava7(JDefinedClass jclass, JsonNode propertiesNode) {
JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME);
JVar nameParam = method.param(String.class, "name");
JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue");
JBlock body = method.body();
JSwitch propertySwitch = body._switch(nameParam);
if (propertiesNode != null) {
for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) {
Map.Entry<String, JsonNode> property = properties.next();
String propertyName = property.getKey();
JsonNode node = property.getValue();
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node);
JType propertyType = jclass.fields().get(fieldName).type();
addGetPropertyCase(jclass, propertySwitch, propertyName, propertyType, node);
}
}
JClass extendsType = jclass._extends();
if (extendsType != null && extendsType instanceof JDefinedClass) {
JDefinedClass parentClass = (JDefinedClass) extendsType;
JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME,
new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) });
propertySwitch._default().body()
._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam));
} else {
propertySwitch._default().body()
._return(notFoundParam);
}
return method;
}