本文整理汇总了Java中com.squareup.javapoet.MethodSpec.methodBuilder方法的典型用法代码示例。如果您正苦于以下问题:Java MethodSpec.methodBuilder方法的具体用法?Java MethodSpec.methodBuilder怎么用?Java MethodSpec.methodBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.squareup.javapoet.MethodSpec
的用法示例。
在下文中一共展示了MethodSpec.methodBuilder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPredicate
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
/** Create predicate method declaration. */
@NonNull
protected MethodSpec.Builder createPredicate() {
// TODO: resolve potential name conflict
final String methodName = PREDICATE;
final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
builder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
builder.returns(boolean.class);
builder.addParameter(String.class, "methodName", Modifier.FINAL);
// varargs
builder.varargs(true);
builder.addParameter(Object[].class, "args", Modifier.FINAL);
return builder;
}
示例2: buildFinishWithResult
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
private MethodSpec buildFinishWithResult(ResultModel result, MethodSpec resultSetter) {
MethodSpec.Builder builder = MethodSpec.methodBuilder("finish" + result.getCapitalizeName());
StringBuilder literal = new StringBuilder(resultSetter.name).append("(");
String[] names = new String[result.getParameters().size()];
if (!result.getParameters().isEmpty()) {
for (int i = 0; i < result.getParameters().size(); i++) {
ParameterModel parameter = result.getParameters().get(i);
builder.addParameter(TypeName.get(parameter.getType()), parameter.getName());
//FIXME 重构 parameter writer
names[i] = parameter.getName();
if (i == 0) {
literal.append("$L");
} else {
literal.append(", $L");
}
}
}
literal.append(")");
builder.addStatement(literal.toString(), (Object[]) names);
builder.addStatement("$L.finish()", "activity");
return builder.build();
}
示例3: buildResultSetter
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
private MethodSpec buildResultSetter(ResultModel result) {
MethodSpec.Builder builder = MethodSpec.methodBuilder("result" + result.getCapitalizeName());
if (!result.getParameters().isEmpty()) {
builder.addStatement("$T intent = new $T()", Intent.class, Intent.class);
for (int i = 0; i < result.getParameters().size(); i++) {
ParameterModel parameter = result.getParameters().get(i);
builder.addParameter(TypeName.get(parameter.getType()), parameter.getName());
//FIXME 重构 parameter writer
ParameterWriter writer = ParameterWriter.newWriter(parameter);
writer.writeConsumerSetter(builder);
}
builder.addStatement("$L.setResult($L,intent)", "activity", result.getResultConstant());
} else {
builder.addStatement("$L.setResult($L)", "activity", result.getResultConstant());
}
return builder.build();
}
示例4: getMethodSpec
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
/**
* genera il metodo di test per method di clazz
*
* @param count
* @param method
* @param clazz
* @param infoFromMongoDb
* @param methodOutput
* @return
*/
private MethodSpec getMethodSpec(int count, Method method, Class<?> clazz, Document methodInputs,
Document methodOutput) {
String result = getAssignmentOfMethodResult(method);
String expected = getExpectedResultAsBooleanAssert(method, methodOutput);
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName() + count + TEST);
/*
* for non spring test
*/
String invokerName = getInvokerName(method, clazz, methodInputs, methodBuilder);
String params = getParams(method, methodBuilder, methodInputs);
AnnotationSpec.Builder annSpecBuilder = AnnotationSpec.builder(Test.class);
addExpectedExceptionIfAny(methodInputs, annSpecBuilder);
AnnotationSpec annTestSpec = annSpecBuilder.build();
methodBuilder.addAnnotation(annTestSpec)
.addStatement(result + invokerName + ".$N(" + params + ")", method.getName())
.addModifiers(Modifier.PUBLIC);
methodBuilder.addStatement("$L.assertTrue(" + expected + ")", Assert.class.getName());
methodBuilder.addJavadoc("\n");
return methodBuilder.build();
}
示例5: buildComponentTypeNameMethod
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
@Nullable
private MethodSpec buildComponentTypeNameMethod()
throws ArezProcessorException
{
if ( null == _componentTypeName )
{
return null;
}
final MethodSpec.Builder builder =
MethodSpec.methodBuilder( _componentTypeName.getSimpleName().toString() );
ProcessorUtil.copyAccessModifiers( _componentTypeName, builder );
builder.addModifiers( Modifier.FINAL );
builder.addAnnotation( GeneratorUtil.NONNULL_CLASSNAME );
builder.returns( TypeName.get( String.class ) );
builder.addStatement( "return $S", _type );
return builder.build();
}
示例6: buildPreDispose
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
@Nonnull
private MethodSpec buildPreDispose()
throws ArezProcessorException
{
final MethodSpec.Builder method = MethodSpec.methodBuilder( GeneratorUtil.PRE_DISPOSE_METHOD_NAME );
final CodeBlock.Builder onDisposeCodeBlock = CodeBlock.builder();
onDisposeCodeBlock.beginControlFlow( "if ( null != this.$N )", GeneratorUtil.ON_DISPOSE_FIELD_NAME );
onDisposeCodeBlock.addStatement( "this.$N.onDispose( this )", GeneratorUtil.ON_DISPOSE_FIELD_NAME );
onDisposeCodeBlock.addStatement( "this.$N = null", GeneratorUtil.ON_DISPOSE_FIELD_NAME );
onDisposeCodeBlock.endControlFlow();
method.addCode( onDisposeCodeBlock.build() );
if ( null != _preDispose )
{
method.addStatement( "super.$N()", _preDispose.getSimpleName().toString() );
}
return method.build();
}
示例7: generateHandler
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
SenderGenerator generateHandler() {
MethodSpec.Builder handlerBuilder = MethodSpec.methodBuilder("handle");
TypeVariableName Tvariable = TypeVariableName.get("T");
handlerBuilder.addModifiers(Modifier.PRIVATE)
.addTypeVariable(Tvariable)
.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build());
ParameterSpec futureParameter = ParameterSpec.builder(ParameterizedTypeName.get(ClassName.get(io.vertx.core.Future.class), Tvariable), "future").build();
ParameterSpec asyncResultParameter = ParameterSpec.builder(
ParameterizedTypeName.get(ClassName.get(io.vertx.core.AsyncResult.class),
ParameterizedTypeName.get(ClassName.get(io.vertx.core.eventbus.Message.class), TypeName.get(Object.class))),
"asyncResult").build();
handlerBuilder.addParameter(futureParameter)
.addParameter(asyncResultParameter)
.beginControlFlow("if ($N.succeeded())", asyncResultParameter)
.addStatement("$T reply = ($T) $N.result().body()", RIFAMessage.class, RIFAMessage.class, asyncResultParameter)
.addStatement("$N.complete(($T) reply.get(0))", futureParameter, Tvariable)
.nextControlFlow("else")
.addStatement("$N.fail($N.cause().getMessage())", futureParameter, asyncResultParameter)
.endControlFlow()
.returns(TypeName.VOID);
classBuilder.addMethod(handlerBuilder.build());
return this;
}
示例8: generateUnregisterMethod
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
ReceiverGenerator generateUnregisterMethod() {
MethodSpec.Builder unregisterMB = MethodSpec.methodBuilder("unregisterReceiver");
// Generates cosumers waiting Future for success handler unregistration
unregisterMB.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addStatement("return $T.all($N.stream().map((consumer) -> {"
+ "$T future = $T.future();"
+ "consumer.unregister(future);"
+ "return future;"
+ "}).collect($T.toList()))",
CompositeFuture.class,
consumersField,
ParameterizedTypeName.get(ClassName.get(Future.class), TypeName.get(Void.class)),
TypeName.get(Future.class),
TypeName.get(Collectors.class)
)
.returns(ParameterizedTypeName.get(ClassName.get(Future.class), WildcardTypeName.subtypeOf(Object.class)));
tsb.addMethod(unregisterMB.build());
return this;
}
示例9: buildAutorun
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
/**
* Generate the autorun wrapper.
* This is wrapped in case the user ever wants to explicitly call method
*/
@Nonnull
private MethodSpec buildAutorun()
throws ArezProcessorException
{
final MethodSpec.Builder builder = MethodSpec.methodBuilder( _autorun.getSimpleName().toString() );
ProcessorUtil.copyAccessModifiers( _autorun, builder );
ProcessorUtil.copyExceptions( _autorunType, builder );
ProcessorUtil.copyTypeParameters( _autorunType, builder );
ProcessorUtil.copyDocumentedAnnotations( _autorun, builder );
builder.addAnnotation( Override.class );
final TypeMirror returnType = _autorun.getReturnType();
builder.returns( TypeName.get( returnType ) );
final StringBuilder statement = new StringBuilder();
final ArrayList<Object> parameterNames = new ArrayList<>();
GeneratorUtil.generateNotDisposedInvariant( _componentDescriptor, builder );
statement.append( "$N()." );
parameterNames.add( _componentDescriptor.getContextMethodName() );
statement.append( "safeAction( $T.areNamesEnabled() ? " );
parameterNames.add( GeneratorUtil.AREZ_CLASSNAME );
statement.append( "$N() + $S" );
parameterNames.add( _componentDescriptor.getComponentNameMethodName() );
parameterNames.add( "." + getName() );
statement.append( " : null, " );
statement.append( _mutation );
statement.append( ", () -> super." );
statement.append( _autorun.getSimpleName() );
statement.append( "() )" );
builder.addStatement( statement.toString(), parameterNames.toArray() );
return builder.build();
}
示例10: getDeserializeMethod
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
private MethodSpec getDeserializeMethod(Class<?> clazz) {
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("deserialize");
methodBuilder.addParameter(String.class, "json", Modifier.PRIVATE);
methodBuilder.addStatement("JsonParser parser = new JsonParser()");
methodBuilder.addStatement("JsonObject o = parser.parse(json).getAsJsonObject()");
methodBuilder.addStatement("return new Gson().fromJson(o, " + clazz.getSimpleName() + ".class)");
return methodBuilder.build();
}
示例11: createNavigator
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
private void createNavigator(Set<ActivityIntentModel> activityModels) {
TypeSpec.Builder navigatorBuilder = TypeSpec.classBuilder(navigatorClassName)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
for (ActivityIntentModel model : activityModels) {
createActivityIntent(model);
/**
* public static MainActivityIntent toMainActivity(Context context){
* MainActivityIntent intent = new MainActivityIntent(context, "com.com.MainActivity");
* return intent;
* }
*/
ClassName returnType = ClassName.get(model.getPackageName(), model.getIntentClzName());
MethodSpec.Builder methodSpecBuilder = MethodSpec
.methodBuilder(METHOD_PREFIX + model.getClzName());
methodSpecBuilder.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(classContext, "context")
.returns(ClassName.get(model.getPackageName(), model.getIntentClzName()))
.addStatement("$T intent = new $T($L,$S)", returnType, returnType, "context", model.getQualifiedName())
.addStatement("return intent");
navigatorBuilder.addMethod(methodSpecBuilder.build());
}
addPreGoListener(navigatorBuilder);
try {
JavaFile.builder(navigatorPackageName, navigatorBuilder.build()).build().writeTo(filer);
} catch (IOException e) {
e.printStackTrace();
}
}
示例12: buildComponentNameMethod
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
/**
* Generate the getter for component name.
*/
@Nonnull
private MethodSpec buildComponentNameMethod()
throws ArezProcessorException
{
final MethodSpec.Builder builder;
if ( null == _componentName )
{
builder = MethodSpec.methodBuilder( GeneratorUtil.NAME_METHOD_NAME );
}
else
{
builder = MethodSpec.methodBuilder( _componentName.getSimpleName().toString() );
ProcessorUtil.copyAccessModifiers( _componentName, builder );
builder.addModifiers( Modifier.FINAL );
}
builder.returns( TypeName.get( String.class ) );
if ( _nameIncludesId )
{
builder.addStatement( "return $S + $N()",
_type.isEmpty() ? "" : _type + ".",
null == _componentId ? GeneratorUtil.ID_FIELD_NAME : _componentId.getSimpleName() );
}
else
{
builder.addStatement( "return $S", _type );
}
return builder.build();
}
示例13: createAfterCall
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
/** Create afterCall method declaration. */
@NonNull
protected MethodSpec.Builder createAfterCall() {
final MethodSpec.Builder builder = MethodSpec.methodBuilder(AFTERCALL);
builder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
builder.addTypeVariable(TypeVariableName.get("R", Object.class));
builder.returns(TypeVariableName.get("R"));
builder.addParameter(String.class, "methodName", Modifier.FINAL);
builder.addParameter(TypeVariableName.get("R"), "result", Modifier.FINAL);
return builder;
}
示例14: createMethod
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
@NonNull
protected MethodSpec.Builder createMethod(final Symbol.MethodSymbol ms) throws Exception {
final String methodName = ms.getSimpleName().toString();
final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
builder.addModifiers(Modifier.FINAL, Modifier.PUBLIC);
// extract annotations of return type / method. copy all, except @Yield & @AfterCall
mimicMethodAnnotations(builder, ms);
// extract our own annotations
final Attribute.Compound yield = findYieldMethodAnnotation(ms);
final Attribute.Compound after = findAfterMethodAnnotation(ms);
// extract return type
final Type returnType = ms.getReturnType();
final boolean hasReturn = returnType.getKind() != TypeKind.VOID;
builder.returns(TypeName.get(returnType));
// extract parameters
final StringBuilder arguments = mimicParameters(builder, ms);
// extract throws
mimicThrows(builder, ms);
builder.beginControlFlow("if (!$L( $S$L ))", PREDICATE, methodName,
(arguments.length() == 0 ? "" : ", ") + arguments);
// generate default return value
if (hasReturn || null != yield) {
if (null != yield) builder.addComment("" + yield);
createYieldPart(builder, returnType, yield);
} else {
builder.addStatement("return");
}
builder.endControlFlow();
// generate return
if (null == after) {
builder.addStatement((hasReturn ? "return " : "") + "this.inner.$N($L)", methodName, arguments);
} else {
afterCalls.set(true);
if (hasReturn) {
builder.addStatement("return $L($S, this.inner.$N($L))", AFTERCALL, methodName, methodName, arguments);
} else {
builder.addStatement("this.inner.$N($L)", methodName, arguments);
builder.addStatement("$L($S, null)", AFTERCALL, methodName);
}
}
return builder;
}
示例15: generateMasterJavaFile
import com.squareup.javapoet.MethodSpec; //导入方法依赖的package包/类
private void generateMasterJavaFile(Map<String, Map<String, Element>> groups, List<JavaFile> javaFiles) {
Set<Map.Entry<String, Map<String, Element>>> kvs = groups.entrySet();
for (Map.Entry<String, Map<String, Element>> group : kvs) {
String namespace = group.getKey();
if (isEmpty(namespace)) return;
Map<String, Element> methodMappings = group.getValue();
// constructor build
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addModifiers(Modifier.PUBLIC).addException(Throwable.class);
// constructor body
constructorBuilder.addStatement("this.mapping = new $T()", HashMap.class);
for (Map.Entry<String, Element> mapping : methodMappings.entrySet()) {
String fullName = mapping.getKey();//method name + "." + extra
Element element = mapping.getValue();
SurgeonMethod sm = parseToSurgeonMethod(element);
sm.owner = ClassName.get(((TypeElement) element.getEnclosingElement()));
// add method
constructorBuilder.addStatement(
"mapping.put($S," + "new $T($T.class," + sm.method + ",$S,$S)" + ")",
fullName,
SurgeonMethod,
sm.owner,
//method inner
sm.owner,
element.getSimpleName().toString(),
//method inner end
sm.simpleParamsName,
sm.simpleParamsTypes);
//.addCode("\n");
}
// method build
MethodSpec.Builder invokeBuilder = MethodSpec.methodBuilder("find");
invokeBuilder.addModifiers(Modifier.PUBLIC)
.returns(SurgeonMethod)
.addParameter(String.class, "name");
// method body
invokeBuilder.addStatement("return ($T) mapping.get(name)", SurgeonMethod);
// java file build
String mirror_name_main = PREFIX + namespace.replace(".", "_");
TypeSpec clazz = TypeSpec.classBuilder(mirror_name_main)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(ISurgeonMaster)
// Fields
.addFields(buildRouterModuleFields())
// constructor
.addMethod(constructorBuilder.build())
// Methods
.addMethod(invokeBuilder.build())
// doc
.addJavadoc(FILE_DOC)
.build();
JavaFile javaFile = JavaFile.builder(PACKAGE_NAME, clazz).build();
javaFiles.add(javaFile);
}
}