PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/v2/trunk/src/main/java/net/usikkert/kouchat/reflect/GenericArgumentExtractorImpl.java

http://kouchat.googlecode.com/
Java | 71 lines | 31 code | 15 blank | 25 comment | 6 complexity | 2f5b2099910d2d11f1173335846b4f9a MD5 | raw file
 1
 2/***************************************************************************
 3 *   Copyright 2006-2010 by Christian Ihle                                 *
 4 *   kontakt@usikkert.net                                                  *
 5 *                                                                         *
 6 *   This file is part of KouChat.                                         *
 7 *                                                                         *
 8 *   KouInject is free software; you can redistribute it and/or modify     *
 9 *   it under the terms of the GNU Lesser General Public License as        *
10 *   published by the Free Software Foundation, either version 3 of        *
11 *   the License, or (at your option) any later version.                   *
12 *                                                                         *
13 *   KouInject is distributed in the hope that it will be useful,          *
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
16 *   Lesser General Public License for more details.                       *
17 *                                                                         *
18 *   You should have received a copy of the GNU Lesser General Public      *
19 *   License along with KouInject.                                         *
20 *   If not, see <http://www.gnu.org/licenses/>.                           *
21 ***************************************************************************/
22
23package net.usikkert.kouchat.reflect;
24
25import java.lang.reflect.ParameterizedType;
26import java.lang.reflect.Type;
27
28import javax.inject.Singleton;
29
30import net.usikkert.kouinject.annotation.Component;
31
32import org.apache.commons.lang.Validate;
33
34/**
35 * TODO
36 *
37 * @author Christian Ihle
38 */
39@Component
40@Singleton
41public class GenericArgumentExtractorImpl implements GenericArgumentExtractor {
42
43    public Class<?> getGenericArgument(final Object objectWithGenericArgument, final Class<?> genericType) {
44        Validate.notNull(objectWithGenericArgument, "Object can not be null");
45        Validate.notNull(genericType, "Generic Type can not be null");
46
47        final Type[] genericInterfaces = objectWithGenericArgument.getClass().getGenericInterfaces();
48
49        for (final Type type : genericInterfaces) {
50            if (type.equals(genericType)) {
51                throw new UnsupportedOperationException("A generic argument must be present on " + genericType);
52            }
53
54            if (type instanceof ParameterizedType) {
55                final ParameterizedType parameterizedType = (ParameterizedType) type;
56
57                if (parameterizedType.getRawType().equals(genericType)) {
58                    final Type[] typeArguments = parameterizedType.getActualTypeArguments();
59
60                    if (typeArguments.length != 1) {
61                        throw new UnsupportedOperationException("Only one generic argument can be present on " + genericType);
62                    }
63
64                    return (Class<?>) typeArguments[0];
65                }
66            }
67        }
68
69        throw new IllegalArgumentException("The generic type " + genericType + " was not present on " + objectWithGenericArgument.getClass());
70    }
71}