/*
 * Copyright 2002-2019 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.core.type.classreading;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.SpringAsmInfo;
import org.springframework.asm.Type;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.lang.Nullable;

/**
 * ASM method visitor that creates {@link SimpleMethodMetadata}.
 *
 * @author Phillip Webb
 * @since 5.2
 */
final class SimpleMethodMetadataReadingVisitor extends MethodVisitor {

	@Nullable
	private final ClassLoader classLoader;

	private final String declaringClassName;

	private final int access;

	private final String methodName;

	private final String descriptor;

	private final List<MergedAnnotation<?>> annotations = new ArrayList<>(4);

	private final Consumer<SimpleMethodMetadata> consumer;

	@Nullable
	private Source source;


	SimpleMethodMetadataReadingVisitor(@Nullable ClassLoader classLoader, String declaringClassName,
			int access, String methodName, String descriptor, Consumer<SimpleMethodMetadata> consumer) {

		super(SpringAsmInfo.ASM_VERSION);
		this.classLoader = classLoader;
		this.declaringClassName = declaringClassName;
		this.access = access;
		this.methodName = methodName;
		this.descriptor = descriptor;
		this.consumer = consumer;
	}


	@Override
	@Nullable
	public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
		return MergedAnnotationReadingVisitor.get(this.classLoader, this::getSource,
				descriptor, visible, this.annotations::add);
	}

	@Override
	public void visitEnd() {
		if (!this.annotations.isEmpty()) {
			String returnTypeName = Type.getReturnType(this.descriptor).getClassName();
			MergedAnnotations annotations = MergedAnnotations.of(this.annotations);
			SimpleMethodMetadata metadata = new SimpleMethodMetadata(this.methodName,
					this.access, this.declaringClassName, returnTypeName, annotations);
			this.consumer.accept(metadata);
		}
	}

	private Object getSource() {
		Source source = this.source;
		if (source == null) {
			source = new Source(this.declaringClassName, this.methodName, this.descriptor);
			this.source = source;
		}
		return source;
	}


	/**
	 * {@link MergedAnnotation} source.
	 */
	static final class Source {

		private final String declaringClassName;

		private final String methodName;

		private final String descriptor;

		@Nullable
		private String toStringValue;

		Source(String declaringClassName, String methodName, String descriptor) {
			this.declaringClassName = declaringClassName;
			this.methodName = methodName;
			this.descriptor = descriptor;
		}

		@Override
		public int hashCode() {
			int result = 1;
			result = 31 * result + this.declaringClassName.hashCode();
			result = 31 * result + this.methodName.hashCode();
			result = 31 * result + this.descriptor.hashCode();
			return result;
		}

		@Override
		public boolean equals(@Nullable Object other) {
			if (this == other) {
				return true;
			}
			if (other == null || getClass() != other.getClass()) {
				return false;
			}
			Source otherSource = (Source) other;
			return (this.declaringClassName.equals(otherSource.declaringClassName) &&
					this.methodName.equals(otherSource.methodName) && this.descriptor.equals(otherSource.descriptor));
		}

		@Override
		public String toString() {
			String value = this.toStringValue;
			if (value == null) {
				StringBuilder builder = new StringBuilder();
				builder.append(this.declaringClassName);
				builder.append(".");
				builder.append(this.methodName);
				Type[] argumentTypes = Type.getArgumentTypes(this.descriptor);
				builder.append("(");
				for (Type type : argumentTypes) {
					builder.append(type.getClassName());
				}
				builder.append(")");
				value = builder.toString();
				this.toStringValue = value;
			}
			return value;
		}
	}

}

@@ -20,10 +20,10 @@
 2-20
+2
 1
-9
  the
@@ -1198,16 +1198,39 @@
 ip Webb%0A
+ * @author Sam Brannen%0A
  * @sinc
@@ -4301,11 +4301,11 @@
 end(
-%22.%22
+'.'
 );%0A%09
@@ -4430,11 +4430,11 @@
 end(
-%22(%22
+'('
 );%0A%09
@@ -4445,61 +4445,141 @@
 or (
-Type type : argumentTypes) %7B%0A%09%09%09%09%09builder.append(type
+int i = 0; i %3C argumentTypes.length; i++) %7B%0A%09%09%09%09%09if (i != 0) %7B%0A%09%09%09%09%09%09builder.append(',');%0A%09%09%09%09%09%7D%0A%09%09%09%09%09builder.append(argumentTypes%5Bi%5D
 .get
@@ -4621,11 +4621,11 @@
 end(
-%22)%22
+')'
 );%0A%09

Fix bug in SimpleMethodMetadataReadingVisitor.Source.toString()