View Javadoc
1   /**
2    *  Copyright (C) 2016 Gary Gregory. All rights reserved.
3    *
4    *  See the NOTICE.txt file distributed with this work for additional
5    *  information regarding copyright ownership.
6    *  
7    *  Licensed under the Apache License, Version 2.0 (the "License");
8    *  you may not use this file except in compliance with the License.
9    *  You may obtain a copy of the License at
10   *  
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *  
13   *  Unless required by applicable law or agreed to in writing, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   */
19  
20  package com.garygregory.jcommander.converters.lang;
21  
22  import com.garygregory.jcommander.converters.AbstractBaseConverter;
23  
24  /**
25   * Converts a {@link String} into a {@link Class}.
26   * <p>
27   * For a description of the format, see {@link Class#forName(String)}.
28   * </p>
29   * 
30   * <p>
31   * Example:
32   * </p>
33   * 
34   * <pre class="prettyprint">
35   * <code class="language-java">&#64;Parameter(names = { "--class" }, converter = ClassConverter.class)
36   * private Class class;</code>
37   * </pre>
38   * <p>
39   * 
40   * @see Class
41   * @see Class#forName(String)
42   * 
43   * @since 1.0.0
44   * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
45   */
46  @SuppressWarnings("rawtypes")
47  public class ClassConverter extends AbstractBaseConverter<Class> {
48  
49      /**
50       * Constructs a converter.
51       * 
52       * @param optionName
53       *            The option name, may be null.
54       */
55      public ClassConverter(final String optionName) {
56          super(optionName, Class.class);
57      }
58  
59      @Override
60      protected Class<?> convertImpl(final String value) throws ClassNotFoundException {
61          return Class.forName(value);
62      }
63  
64  }