本文整理匯總了Java中com.sun.codemodel.JMethod類的典型用法代碼示例。如果您正苦於以下問題:Java JMethod類的具體用法?Java JMethod怎麽用?Java JMethod使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JMethod類屬於com.sun.codemodel包,在下文中一共展示了JMethod類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addFactoryMethod
import com.sun.codemodel.JMethod; //導入依賴的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);
}
示例2: addPublicGetMethod
import com.sun.codemodel.JMethod; //導入依賴的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;
}
示例3: addPublicSetMethod
import com.sun.codemodel.JMethod; //導入依賴的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;
}
示例4: addPublicWithMethod
import com.sun.codemodel.JMethod; //導入依賴的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;
}
示例5: addToString
import com.sun.codemodel.JMethod; //導入依賴的package包/類
private void addToString(JDefinedClass jclass) {
Map<String, JFieldVar> fields = jclass.fields();
JMethod toString = jclass.method(JMod.PUBLIC, String.class, "toString");
Set<String> excludes = new HashSet<String>(Arrays.asList(ruleFactory.getGenerationConfig().getToStringExcludes()));
JBlock body = toString.body();
Class<?> toStringBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.ToStringBuilder.class : org.apache.commons.lang.builder.ToStringBuilder.class;
JClass toStringBuilderClass = jclass.owner().ref(toStringBuilder);
JInvocation toStringBuilderInvocation = JExpr._new(toStringBuilderClass).arg(JExpr._this());
if (!jclass._extends().fullName().equals(Object.class.getName())) {
toStringBuilderInvocation = toStringBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("toString"));
}
for (JFieldVar fieldVar : fields.values()) {
if (excludes.contains(fieldVar.name()) || (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
toStringBuilderInvocation = toStringBuilderInvocation.invoke("append").arg(fieldVar.name()).arg(fieldVar);
}
body._return(toStringBuilderInvocation.invoke("toString"));
toString.annotate(Override.class);
}
示例6: addHashCode
import com.sun.codemodel.JMethod; //導入依賴的package包/類
private void addHashCode(JDefinedClass jclass, JsonNode node) {
Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node);
JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode");
Class<?> hashCodeBuilder = ruleFactory.getGenerationConfig().isUseCommonsLang3() ? org.apache.commons.lang3.builder.HashCodeBuilder.class : org.apache.commons.lang.builder.HashCodeBuilder.class;
JBlock body = hashCode.body();
JClass hashCodeBuilderClass = jclass.owner().ref(hashCodeBuilder);
JInvocation hashCodeBuilderInvocation = JExpr._new(hashCodeBuilderClass);
if (!jclass._extends().fullName().equals(Object.class.getName())) {
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("appendSuper").arg(JExpr._super().invoke("hashCode"));
}
for (JFieldVar fieldVar : fields.values()) {
if ((fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) {
continue;
}
hashCodeBuilderInvocation = hashCodeBuilderInvocation.invoke("append").arg(fieldVar);
}
body._return(hashCodeBuilderInvocation.invoke("toHashCode"));
hashCode.annotate(Override.class);
}
示例7: buildClassAnnotations
import com.sun.codemodel.JMethod; //導入依賴的package包/類
/**
* @return A list of the class level annotations that the annotator will
* use.
*/
private List<MetaAnnotation> buildClassAnnotations() {
List<MetaAnnotation> anns = Lists.newArrayList();
HashMap<String, Object> annotParams;
// AlwaysValid
JDefinedClass alwaysValid = buildTemplateConstraint("AlwaysValid");
JDefinedClass alwaysValidValidator = buildTemplateConstraintValidator("AlwaysValidValidator", alwaysValid, Object.class);
JMethod isValid = getIsValidMethod(alwaysValidValidator);
isValid.body()._return(JExpr.TRUE);
alwaysValid.annotate(Constraint.class).param("validatedBy", alwaysValidValidator);
annotParams = Maps.newHashMap();
anns.add(new MetaAnnotation(alwaysValid, AnnotationType.JSR_303, annotParams));
return anns;
}
示例8: generateModel
import com.sun.codemodel.JMethod; //導入依賴的package包/類
@BeforeClass
public static void generateModel() throws Exception {
final JCodeModel jCodeModel = new JCodeModel();
final JPackage jp = jCodeModel.rootPackage();
final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
JAnnotationUse jAnnotationUse = jClass.annotate(InitialDataSets.class);
jAnnotationUse.param("value", "Script.file");
jClass.annotate(Cleanup.class);
final JFieldVar jField = jClass.field(JMod.PRIVATE, String.class, "testField");
jField.annotate(PersistenceContext.class);
final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
jAnnotationUse = jMethod.annotate(InitialDataSets.class);
jAnnotationUse.param("value", "InitialDataSets.file");
jAnnotationUse = jMethod.annotate(ApplyScriptsAfter.class);
jAnnotationUse.param("value", "ApplyScriptsAfter.file");
buildModel(testFolder.getRoot(), jCodeModel);
compileModel(testFolder.getRoot());
cut = loadClass(testFolder.getRoot(), jClass.name());
}
示例9: addParameter
import com.sun.codemodel.JMethod; //導入依賴的package包/類
private JVar addParameter(
final JMethod method,
final JExpression returnValue,
final JFieldVar field,
final boolean isImutable,
final boolean isNullable,
final boolean isArrayNullable,
final boolean isCollectionNullable) {
if (isImutable) {
return SourceFactoryUtilities.addParameter(method, field);
}
if (isInstanceOfMap(field.type())) {
return mapSetter(method, returnValue, field, isNullable);
}
if (isInstanceOfList(field.type())) {
return listSetter(method, returnValue, field, isNullable, isCollectionNullable);
}
return objectSetter(method, returnValue, field, isNullable, isArrayNullable);
}
示例10: listSetter
import com.sun.codemodel.JMethod; //導入依賴的package包/類
private JVar listSetter(
final JMethod method,
final JExpression returnValue,
final JFieldVar field,
final boolean isNullable,
final boolean isCollectionNullable) {
if (isNullable) {
if (!isCollectionNullable) {
return addListParameter(method, field, true, createAddIfNullClearListAndReturnClosure(method, returnValue));
}
return addListParameter(method, field, true, createAddIfNullReturnClosure(method, returnValue));
}
return addListParameter(
method,
field,
true,
createEnsureArgumentNotNullClosure(this.ensurePredicateFactory, method));
}
示例11: createCreateBeanMethod
import com.sun.codemodel.JMethod; //導入依賴的package包/類
public JMethod createCreateBeanMethod(final JDefinedClass bean) {
final JMethod method = bean.method(JMod.PRIVATE | JMod.STATIC, bean, "_createBean");
final JVar param = method.param(
_classByNames(java.lang.Class.class.getName(), MessageFormat.format("? extends {0}", bean.name())),
"clazz");
final JClass invokerClass = _classByNames(
"net.anwiba.commons.reflection.ReflectionConstructorInvoker",
bean.name());
final JTryBlock _try = method.body()._try();
final JVar invoker = _try.body().decl(invokerClass, "invoker", JExpr._new(invokerClass).arg(param)); //$NON-NLS-1$
_try.body()._return(invoker.invoke("invoke"));
final JCatchBlock _catch = _try._catch(_classByNames(java.lang.reflect.InvocationTargetException.class.getName()));
final JVar exception = _catch.param("exception"); //$NON-NLS-1$
_catch.body()._throw(JExpr._new(_classByNames(java.lang.RuntimeException.class.getName())).arg(exception));
return method;
}
示例12: addMapParameter
import com.sun.codemodel.JMethod; //導入依賴的package包/類
@SafeVarargs
public static JVar addMapParameter(
final JMethod method,
final JFieldVar field,
final JType nameType,
final String nameVariableName,
final JType valueType,
final String valueVariableName,
final boolean isInjection,
final IProcedure<JVar, RuntimeException>... procedure) {
if (method == null || field == null) {
return null;
}
final JVar nameParam = method.param(JMod.FINAL, nameType, nameVariableName);
final JVar valueParam = method.param(JMod.FINAL, valueType, valueVariableName);
for (final IProcedure<JVar, RuntimeException> closure : procedure) {
closure.execute(nameParam);
closure.execute(valueParam);
}
if (isInjection) {
method.body().invoke("_inject").arg(nameParam).arg(valueParam); //$NON-NLS-1$
}
method.body().add(JExpr.refthis(field.name()).invoke("put").arg(nameParam).arg(valueParam)); //$NON-NLS-1$
return valueParam;
}
示例13: setMapParameters
import com.sun.codemodel.JMethod; //導入依賴的package包/類
@SafeVarargs
public static JVar setMapParameters(
final JMethod method,
final JFieldVar field,
final boolean isClearEnabled,
final IProcedure<JVar, RuntimeException>... procedure) {
if (method == null || field == null) {
return null;
}
final JVar param = method.param(JMod.FINAL, field.type(), field.name());
for (final IProcedure<JVar, RuntimeException> closure : procedure) {
closure.execute(param);
}
if (isClearEnabled) {
method.body().add(JExpr.refthis(field.name()).invoke("clear")); //$NON-NLS-1$
}
method.body().add(JExpr.refthis(field.name()).invoke("putAll").arg(param)); //$NON-NLS-1$
return param;
}
示例14: addListParameter
import com.sun.codemodel.JMethod; //導入依賴的package包/類
@SafeVarargs
public static JVar addListParameter(
final JMethod method,
final JFieldVar field,
final boolean isClearEnabled,
final IProcedure<JVar, RuntimeException>... procedure) {
if (method == null || field == null) {
return null;
}
final JVar param = method.param(JMod.FINAL, field.type(), field.name());
for (final IProcedure<JVar, RuntimeException> closure : procedure) {
closure.execute(param);
}
if (isClearEnabled) {
method.body().add(JExpr.refthis(field.name()).invoke("clear")); //$NON-NLS-1$
}
method.body().add(JExpr.refthis(field.name()).invoke("addAll").arg(param)); //$NON-NLS-1$
return param;
}
示例15: createAddIfNullSetEmptyArrayAndReturnClosure
import com.sun.codemodel.JMethod; //導入依賴的package包/類
public static IProcedure<JVar, RuntimeException> createAddIfNullSetEmptyArrayAndReturnClosure(
final JCodeModel codeModel,
final JMethod method,
final JExpression returnValue) {
ensureThatArgument(method, notNull());
return new IProcedure<JVar, RuntimeException>() {
@Override
public void execute(final JVar param) throws RuntimeException {
ensureThatArgument(param, notNull());
final ValueConverter valueConverter = new ValueConverter(codeModel);
final JInvocation invocation = JExpr._new(param.type());
method
.body()
._if(param.eq(JExpr._null()))
._then()
.block()
.assign(JExpr.refthis(param.name()), valueConverter.convert(invocation))
._return(returnValue);
}
};
}