spring GeneratedMethod 源码

  • 2022-08-08
  • 浏览 (452)

spring GeneratedMethod 代码

文件路径:/spring-core/src/main/java/org/springframework/aot/generate/GeneratedMethod.java

/*
 * Copyright 2002-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aot.generate;

import java.util.function.Consumer;

import org.springframework.javapoet.MethodSpec;
import org.springframework.util.Assert;

/**
 * A generated method.
 *
 * @author Phillip Webb
 * @since 6.0
 * @see GeneratedMethods
 */
public final class GeneratedMethod {

	private final String name;

	private final MethodSpec methodSpec;


	/**
	 * Create a new {@link GeneratedMethod} instance with the given name. This
	 * constructor is package-private since names should only be generated via
	 * {@link GeneratedMethods}.
	 * @param name the generated method name
	 * @param method consumer to generate the method
	 */
	GeneratedMethod(String name, Consumer<MethodSpec.Builder> method) {
		this.name = name;
		MethodSpec.Builder builder = MethodSpec.methodBuilder(getName());
		method.accept(builder);
		this.methodSpec = builder.build();
		Assert.state(this.name.equals(this.methodSpec.name),
				"'method' consumer must not change the generated method name");
	}


	/**
	 * Return the generated name of the method.
	 * @return the name of the generated method
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * Return the {@link MethodSpec} for this generated method.
	 * @return the method spec
	 * @throws IllegalStateException if one of the {@code generateBy(...)}
	 * methods has not been called
	 */
	MethodSpec getMethodSpec() {
		return this.methodSpec;
	}

	@Override
	public String toString() {
		return this.name;
	}

}

相关信息

spring 源码目录

相关文章

spring AccessVisibility 源码

spring AppendableConsumerInputStreamSource 源码

spring ClassNameGenerator 源码

spring DefaultGenerationContext 源码

spring FileSystemGeneratedFiles 源码

spring GeneratedClass 源码

spring GeneratedClasses 源码

spring GeneratedFiles 源码

spring GeneratedMethods 源码

spring GeneratedTypeReference 源码

0  赞