/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

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2010, Red Hat, Inc., and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.forge.shell.project.resources;
  23. import java.lang.reflect.Constructor;
  24. import java.lang.reflect.Field;
  25. import java.lang.reflect.Member;
  26. import java.lang.reflect.Method;
  27. import java.lang.reflect.ParameterizedType;
  28. import java.lang.reflect.Type;
  29. import javax.enterprise.inject.Produces;
  30. import javax.enterprise.inject.spi.AnnotatedParameter;
  31. import javax.enterprise.inject.spi.InjectionPoint;
  32. import org.jboss.forge.resources.Resource;
  33. import org.jboss.forge.shell.Shell;
  34. import org.jboss.forge.shell.plugins.Current;
  35. /**
  36. * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  37. */
  38. public class ResourceProducer
  39. {
  40. @Produces
  41. @Current
  42. @SuppressWarnings({ "rawtypes", "unchecked" })
  43. public Resource getCurrentResource(final InjectionPoint ip, final Shell shell)
  44. {
  45. Resource<?> currentResource = shell.getCurrentResource();
  46. Type type = null;
  47. Member member = ip.getMember();
  48. if (member instanceof Field)
  49. {
  50. type = ((Field) member).getType();
  51. }
  52. else if (member instanceof Method)
  53. {
  54. AnnotatedParameter<?> annotated = (AnnotatedParameter<?>) ip.getAnnotated();
  55. type = annotated.getBaseType();
  56. }
  57. else if (member instanceof Constructor<?>)
  58. {
  59. AnnotatedParameter<?> annotated = (AnnotatedParameter<?>) ip.getAnnotated();
  60. type = annotated.getBaseType();
  61. }
  62. try
  63. {
  64. Class<? extends Resource> resourceClass = currentResource.getClass();
  65. if ((type instanceof Class) && ((Class) type).isAssignableFrom(resourceClass))
  66. {
  67. return currentResource;
  68. }
  69. else if (type instanceof ParameterizedType)
  70. {
  71. ParameterizedType t = (ParameterizedType) type;
  72. Type rawType = t.getRawType();
  73. if ((rawType instanceof Class) && ((Class) rawType).isAssignableFrom(resourceClass))
  74. {
  75. return currentResource;
  76. }
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. throw new IllegalStateException("Could not @Inject Resource type into InjectionPoint:" + ip, e);
  82. }
  83. return null;
  84. }
  85. }