求助帥哥程序員,不帥的不用進來

spring cloud K8s源碼閱讀:

package org.springframework.cloud.kubernetes.ribbon;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;

import com.netflix.client.config.IClientConfigKey;

import org.springframework.util.Assert;

/**
 * Kubernetes implementation of a Ribbon {@link IClientConfigKey}.
 *
 * @param <T> type of key
 * @author Ioannis Canellos
 */
public abstract class KubernetesConfigKey<T> implements IClientConfigKey<T> {

	/**
	 * Namespace configuration key.
	 */
	public static final IClientConfigKey<String> Namespace = new KubernetesConfigKey<String>(
			"KubernetesNamespace") {
	};

	/**
	 * Port name configuration key.
	 */
	public static final IClientConfigKey<String> PortName = new KubernetesConfigKey<String>(
			"PortName") {
	};

	private static final Set<IClientConfigKey> keys = new HashSet<IClientConfigKey>();

	/*請問這段是什麼意思? 該方法沒有方法名,沒有參數?*/
	static {
		for (Field f : KubernetesConfigKey.class.getDeclaredFields()) {
			if (Modifier.isStatic(f.getModifiers()) // &&
													// Modifier.isPublic(f.getModifiers())
					&& IClientConfigKey.class.isAssignableFrom(f.getType())) {
				try {
					keys.add((IClientConfigKey) f.get(null));
				}
				catch (IllegalAccessException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}

	private final String configKey;

	private final Class<T> type;

	@SuppressWarnings("unchecked")
	protected KubernetesConfigKey(String configKey) {
		this.configKey = configKey;
		Type superclass = getClass().getGenericSuperclass();
		Assert.isTrue(superclass instanceof ParameterizedType,
				superclass + " isn't parameterized");
		Type runtimeType = ((ParameterizedType) superclass).getActualTypeArguments()[0];
		this.type = (Class<T>) Types.rawType(runtimeType);
	}

	/**
	 * @deprecated see {@link #keys()}
	 * @return array of {@link IClientConfigKey}
	 */
	@Deprecated
	public static IClientConfigKey[] values() {
		return keys().toArray(new IClientConfigKey[0]);
	}

	/**
	 * @return all the public static keys defined in this class
	 */
	public static Set<IClientConfigKey> keys() {
		return keys;
	}

	public static IClientConfigKey valueOf(final String name) {
		for (IClientConfigKey key : keys()) {
			if (key.key().equals(name)) {
				return key;
			}
		}
		return new IClientConfigKey() {
			@Override
			public String key() {
				return name;
			}

			@Override
			public Class type() {
				return String.class;
			}
		};
	}

	@Override
	public Class<T> type() {
		return this.type;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.netflix.niws.client.ClientConfig#key()
	 */
	@Override
	public String key() {
		return this.configKey;
	}

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

來吧,給大家講講這段代碼!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章