Package com.google.gerrit.util.cli

Source Code of com.google.gerrit.util.cli.OptionHandlerUtil

// Copyright (C) 2009 The Android Open Source Project
//
// 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
//
// http://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 com.google.gerrit.util.cli;

import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.util.Types;

import org.kohsuke.args4j.spi.OptionHandler;

import java.lang.reflect.Type;

/** Utilities to support creating OptionHandler instances. */
public class OptionHandlerUtil {
  /** Generate a key for an {@link OptionHandlerFactory} in Guice. */
  @SuppressWarnings("unchecked")
  public static <T> Key<OptionHandlerFactory<T>> keyFor(final Class<T> valueType) {
    final Type factoryType = Types.newParameterizedType(OptionHandlerFactory.class, valueType);
    return (Key<OptionHandlerFactory<T>>) Key.get(factoryType);
  }

  @SuppressWarnings("unchecked")
  private static <T> Key<OptionHandler<T>> handlerOf(Class<T> type) {
    final Type handlerType = Types.newParameterizedTypeWithOwner(null, OptionHandler.class, type);
    return (Key<OptionHandler<T>>) Key.get(handlerType);
  }

  public static <T> Module moduleFor(final Class<T> type, Class<? extends OptionHandler<T>> impl) {
    return new FactoryModuleBuilder()
        .implement(handlerOf(type), impl)
        .build(keyFor(type));
  }

  private OptionHandlerUtil() {
  }
}
TOP

Related Classes of com.google.gerrit.util.cli.OptionHandlerUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.