/shell/src/main/java/org/jboss/forge/shell/project/resources/ResourceProducer.java
https://github.com/cunningt/core-1 · Java · 93 lines · 61 code · 8 blank · 24 comment · 11 complexity · df986f5df2d20efc4f7cf6c9db41b4c8 MD5 · raw file
- /*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
- package org.jboss.forge.shell.project.resources;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Member;
- import java.lang.reflect.Method;
- import java.lang.reflect.ParameterizedType;
- import java.lang.reflect.Type;
- import javax.enterprise.inject.Produces;
- import javax.enterprise.inject.spi.AnnotatedParameter;
- import javax.enterprise.inject.spi.InjectionPoint;
- import org.jboss.forge.resources.Resource;
- import org.jboss.forge.shell.Shell;
- import org.jboss.forge.shell.plugins.Current;
- /**
- * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
- */
- public class ResourceProducer
- {
- @Produces
- @Current
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public Resource getCurrentResource(final InjectionPoint ip, final Shell shell)
- {
- Resource<?> currentResource = shell.getCurrentResource();
- Type type = null;
- Member member = ip.getMember();
- if (member instanceof Field)
- {
- type = ((Field) member).getType();
- }
- else if (member instanceof Method)
- {
- AnnotatedParameter<?> annotated = (AnnotatedParameter<?>) ip.getAnnotated();
- type = annotated.getBaseType();
- }
- else if (member instanceof Constructor<?>)
- {
- AnnotatedParameter<?> annotated = (AnnotatedParameter<?>) ip.getAnnotated();
- type = annotated.getBaseType();
- }
- try
- {
- Class<? extends Resource> resourceClass = currentResource.getClass();
- if ((type instanceof Class) && ((Class) type).isAssignableFrom(resourceClass))
- {
- return currentResource;
- }
- else if (type instanceof ParameterizedType)
- {
- ParameterizedType t = (ParameterizedType) type;
- Type rawType = t.getRawType();
- if ((rawType instanceof Class) && ((Class) rawType).isAssignableFrom(resourceClass))
- {
- return currentResource;
- }
- }
- }
- catch (Exception e)
- {
- throw new IllegalStateException("Could not @Inject Resource type into InjectionPoint:" + ip, e);
- }
- return null;
- }
- }