本文整理匯總了Java中com.sun.codemodel.JClass.narrow方法的典型用法代碼示例。如果您正苦於以下問題:Java JClass.narrow方法的具體用法?Java JClass.narrow怎麽用?Java JClass.narrow使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.codemodel.JClass
的用法示例。
在下文中一共展示了JClass.narrow方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getDefaultSet
import com.sun.codemodel.JClass; //導入方法依賴的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;
}
示例2: getDefaultList
import com.sun.codemodel.JClass; //導入方法依賴的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;
}
示例3: generateObjectFieldAndAccessors
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
private void generateObjectFieldAndAccessors(JDefinedClass klass, TypeDeclaration property) {
String fieldName = Names.buildVariableName(property);
JType jtype = findTypeVar(klass, property).orElse(context.getJavaType(property));
List<String> args = Annotations.getStringAnnotations(property, TYPE_ARGS);
if (!args.isEmpty()) {
JClass jclass = (JClass) jtype;
for (String arg : args) {
JType typeArg = findTypeParam(klass, arg).get();
jclass = jclass.narrow(typeArg);
}
jtype = jclass;
}
JFieldVar field = klass.field(JMod.PRIVATE, jtype, fieldName);
annotateFieldWithPropertyName(field, property);
generateGetter(property, klass, field, this::getGetterName);
generateSetter(klass, jtype, fieldName);
}
示例4: loadParameterizedType
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
private JClass loadParameterizedType(JsonNode node, JCodeModel codeModel) {
String className = node.get("javaType").asText();
JClass ret = codeModel.ref(className);
if (node.has("javaTypeParams")) {
List<JClass> paramTypes = new ArrayList<JClass>();
JsonNode paramNode = node.get("javaTypeParams");
if (paramNode.isArray()) {
Iterator<JsonNode> it = paramNode.elements();
while (it.hasNext())
paramTypes.add(loadParameterizedType(it.next(), codeModel));
} else {
paramTypes.add(loadParameterizedType(paramNode, codeModel));
}
ret = ret.narrow(paramTypes);
}
return ret;
}
示例5: typeToJType
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
/**
* Convert a generic {@link Type} to {@link JType}.
*
* @param rawType
* the raw type
* @param type
* the generic type
* @param jCodeModel
* the code model
*
* @return converted {@link JType}.
*/
private JType typeToJType(final Class<?> rawType, final Type type, final JCodeModel jCodeModel) {
final JType jType = jCodeModel._ref(rawType);
if (jType instanceof JPrimitiveType) {
return jType;
}
JClass result = (JClass) jType;
if (type instanceof ParameterizedType) {
for (final Type typeArgument : ((ParameterizedType) type).getActualTypeArguments()) {
if (typeArgument instanceof WildcardType) {
result = result.narrow(jCodeModel.wildcard());
} else if (typeArgument instanceof Class) {
result = result.narrow(jCodeModel._ref((Class<?>) typeArgument));
}
}
}
return result;
}
示例6: apply
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
@Override
public JType apply(ApiActionMetadata endpointMetadata, JDefinedClass generatableType) {
JClass responseType = generatableType.owner().ref(ResponseEntity.class);
if (!endpointMetadata.getResponseBody().isEmpty()) {
ApiBodyMetadata apiBodyMetadata = endpointMetadata.getResponseBody().values().iterator().next();
JClass genericType = findFirstClassBySimpleName(apiBodyMetadata.getCodeModel(), apiBodyMetadata.getName());
if (apiBodyMetadata.isArray()) {
JClass arrayType = generatableType.owner().ref(List.class);
return arrayType.narrow(genericType);
} else {
return genericType;
}
}
return responseType
.narrow(generatableType.owner().wildcard());
}
示例7: apply
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
@Override
public JType apply(ApiActionMetadata endpointMetadata, JDefinedClass generatableType) {
JClass callable = generatableType.owner().ref(Callable.class);
JClass responseType = generatableType.owner().ref(ResponseEntity.class);
if (!endpointMetadata.getResponseBody().isEmpty()) {
ApiBodyMetadata apiBodyMetadata = endpointMetadata.getResponseBody().values().iterator().next();
JClass genericType = findFirstClassBySimpleName(apiBodyMetadata.getCodeModel(), apiBodyMetadata.getName());
if (apiBodyMetadata.isArray()) {
JClass arrayType = generatableType.owner().ref(List.class);
responseType = arrayType.narrow(genericType);
} else {
return callable.narrow(genericType);
}
}
return callable.narrow(responseType
.narrow(generatableType.owner().wildcard()));
}
示例8: apply
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
@Override
public JType apply(ApiActionMetadata endpointMetadata, JDefinedClass generatableType) {
if (!endpointMetadata.getResponseBody().isEmpty()) {
ApiBodyMetadata apiBodyMetadata =
endpointMetadata.getResponseBody().values().iterator().next();
JClass returnType =
findFirstClassBySimpleName(apiBodyMetadata.getCodeModel(), apiBodyMetadata.getName());
if (apiBodyMetadata.isArray()) {
JClass arrayType = generatableType.owner().ref(List.class);
return arrayType.narrow(returnType);
}
return returnType;
}
return generatableType.owner().ref(Object.class);
}
示例9: apply
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
@Override
public JType apply(ApiActionMetadata endpointMetadata, JDefinedClass generatableType) {
JClass responseEntity = generatableType.owner().ref(ResponseEntity.class);
if (!endpointMetadata.getResponseBody().isEmpty()) {
ApiBodyMetadata apiBodyMetadata =
endpointMetadata.getResponseBody().values().iterator().next();
JClass genericType =
findFirstClassBySimpleName(apiBodyMetadata.getCodeModel(), apiBodyMetadata.getName());
if (apiBodyMetadata.isArray()) {
JClass arrayType = generatableType.owner().ref(List.class);
return responseEntity.narrow(arrayType.narrow(genericType));
}
return responseEntity.narrow(genericType);
}
return responseEntity
.narrow(generatableType.owner().wildcard());
}
示例10: apply
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
@Override
public JType apply(ApiActionMetadata endpointMetadata, JDefinedClass generatableType) {
JClass callable = generatableType.owner().ref(Callable.class);
JClass responseEntity = generatableType.owner().ref(ResponseEntity.class);
if (!endpointMetadata.getResponseBody().isEmpty()) {
ApiBodyMetadata apiBodyMetadata =
endpointMetadata.getResponseBody().values().iterator().next();
JClass genericType =
findFirstClassBySimpleName(apiBodyMetadata.getCodeModel(), apiBodyMetadata.getName());
if (apiBodyMetadata.isArray()) {
JClass arrayType = generatableType.owner().ref(List.class);
return callable.narrow(responseEntity.narrow(arrayType.narrow(genericType)));
}
return callable.narrow(responseEntity.narrow(genericType));
}
return callable.narrow(responseEntity
.narrow(generatableType.owner().wildcard()));
}
示例11: buildClass
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
private static JClass buildClass(JClassContainer _package, ClassOrInterfaceType c, int arrayCount) {
final String packagePrefix = (c.getScope() != null) ? c.getScope().toString() + "." : "";
JClass _class;
try {
_class = _package.owner().ref(Thread.currentThread().getContextClassLoader().loadClass(packagePrefix + c.getName()));
} catch (ClassNotFoundException e) {
_class = _package.owner().ref(packagePrefix + c.getName());
}
for (int i=0; i<arrayCount; i++) {
_class = _class.array();
}
List<Type> typeArgs = c.getTypeArgs();
if (typeArgs != null && typeArgs.size() > 0) {
JClass[] genericArgumentClasses = new JClass[typeArgs.size()];
for (int i=0; i<typeArgs.size(); i++) {
genericArgumentClasses[i] = buildClass(_package, (ClassOrInterfaceType) ((ReferenceType) typeArgs.get(i)).getType(), ((ReferenceType) typeArgs.get(i)).getArrayCount());
}
_class = _class.narrow(genericArgumentClasses);
}
return _class;
}
示例12: buildTemplateConstraintValidator
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
private JDefinedClass buildTemplateConstraintValidator(String name, JDefinedClass constraint, Class<?> param) {
try {
JClass cv = (JClass) codeModel._ref(ConstraintValidator.class);
cv = cv.narrow(constraint, (JClass) codeModel._ref(param));
JDefinedClass validator = constraint._class(JMod.STATIC | JMod.PUBLIC, name);
validator._implements(cv);
validator.method(JMod.PUBLIC, void.class, "initialize").param(constraint, "parameters");
JMethod isValid = validator.method(JMod.PUBLIC, boolean.class, "isValid");
isValid.param(Object.class, "value");
isValid.param(ConstraintValidatorContext.class, "context");
return validator;
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException("Tried to create an already existing class: " + name, e);
}
}
示例13: generatePopulationCode
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
/**
* Generates a Holder class that will hold an {@link ArrayList} with all the
* first level beans in the graph (contents of {@link #beans}) on it.
*/
private void generatePopulationCode() {
try {
// Generate the holder class
JDefinedClass holderClass = cm._class(Config.CFG.getBasePackageName() + ".Holder");
JClass alObject = (JClass) cm._ref(ArrayList.class);
alObject = alObject.narrow(Object.class);
JFieldVar beansField = holderClass.field(JMod.PUBLIC, alObject, "beans", JExpr._new(alObject));
JMethod defConstructor = holderClass.constructor(JMod.PUBLIC);
JBlock body = defConstructor.body();
// Init the beans and add them to the array
for (MetaJavaBean mjb : beans) {
mjb.generateStaticInitCode();
JVar beanDecl = generateBeanNonStaticInitCode(mjb, body, 0);
body.add(beansField.invoke("add").arg(beanDecl));
}
// Init the base beans but don't add them to the array
for (MetaJavaBean bmjb : baseBeans) {
bmjb.generateStaticInitCode();
}
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException("Error generating the holder class.", e);
}
}
示例14: addBaseClass
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
private void addBaseClass(JDefinedClass klass, ObjectTypeDeclaration type) {
TypeDeclaration parentType = type.parentTypes().get(0);
if (!parentType.name().equals(OBJECT)) {
JClass baseClass = pkg._getClass(parentType.name());
List<String> typeArgs = Annotations.getStringAnnotations(type, TYPE_ARGS);
if (!typeArgs.isEmpty()) {
baseClass = baseClass
.narrow(typeArgs.stream().map(this::toJavaClass).toArray(JClass[]::new));
}
klass._extends(baseClass);
}
}
示例15: addTypeArguments
import com.sun.codemodel.JClass; //導入方法依賴的package包/類
private JType addTypeArguments(JType resultType, TypeDeclaration body) {
List<String> args = Annotations.getStringAnnotations(body, TYPE_ARGS);
JClass jclass = (JClass) resultType;
for (String arg : args) {
JType typeArg = context.getJavaType(arg);
jclass = jclass.narrow(typeArg);
}
return jclass;
}