1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with this 4 * work for additional information regarding copyright ownership. The ASF 5 * licenses this file to You under the Apache License, Version 2.0 (the 6 * "License"); you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law 9 * or agreed to in writing, software distributed under the License is 10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language 12 * governing permissions and limitations under the License. 13 */ 14 package org.apache.webbeans.portable; 15 16 import java.lang.reflect.Constructor; 17 import java.lang.reflect.Field; 18 import java.lang.reflect.Method; 19 20 import javax.enterprise.inject.spi.Annotated; 21 import javax.enterprise.inject.spi.AnnotatedConstructor; 22 import javax.enterprise.inject.spi.AnnotatedField; 23 import javax.enterprise.inject.spi.AnnotatedMethod; 24 import javax.enterprise.inject.spi.AnnotatedType; 25 26 /** 27 * Factory for {@link Annotated} elements. 28 * 29 * @version $Rev: 788970 $ $Date: 2009-06-27 16:26:23 +0300 (Sat, 27 Jun 2009) $ 30 */ 31 public final class AnnotatedElementFactory 32 { 33 private AnnotatedElementFactory() 34 { 35 // Emtpty 36 } 37 38 /** 39 * Creates and configures new annotated type. 40 * 41 * @param <X> class info 42 * @param annotatedClass annotated class 43 * @return new annotated type 44 */ 45 @SuppressWarnings("unchecked") 46 public static <X> AnnotatedType<X> newAnnotatedType(Class<X> annotatedClass) 47 { 48 AnnotatedTypeImpl<X> annotatedType = new AnnotatedTypeImpl<X>(annotatedClass); 49 50 Field[] fields = annotatedClass.getDeclaredFields(); 51 Method[] methods = annotatedClass.getDeclaredMethods(); 52 Constructor<X>[] ctxs = (Constructor<X>[])annotatedClass.getDeclaredConstructors(); 53 54 for(Field f : fields) 55 { 56 AnnotatedField<X> af = new AnnotatedFieldImpl<X>(f, annotatedType); 57 annotatedType.addAnnotatedField(af); 58 } 59 60 for(Method m : methods) 61 { 62 AnnotatedMethod<X> am = new AnnotatedMethodImpl<X>(m,annotatedType); 63 annotatedType.addAnnotatedMethod(am); 64 } 65 66 for(Constructor<X> ct : ctxs) 67 { 68 AnnotatedConstructor<X> ac = new AnnotatedConstructorImpl<X>(ct,annotatedType); 69 annotatedType.addAnnotatedConstructor(ac); 70 } 71 72 return annotatedType; 73 } 74 75 /** 76 * Creates and configures new annotated constructor. 77 * 78 * @param <X> declaring class 79 * @param constructor constructor 80 * @return new annotated constructor 81 */ 82 public static <X> AnnotatedConstructor<X> newAnnotatedConstructor(Constructor<X> constructor) 83 { 84 return new AnnotatedConstructorImpl<X>(constructor); 85 } 86 87 /** 88 * Creates and configures new annotated field. 89 * 90 * @param <X> declaring class 91 * @param field field instance 92 * @param declaringClass declaring class 93 * @return new annotated field 94 */ 95 public static <X> AnnotatedField<X> newAnnotatedField(Field field, Class<X> declaringClass) 96 { 97 return new AnnotatedFieldImpl<X>(field); 98 } 99 100 /** 101 * Creates and configures new annotated method. 102 * 103 * @param <X> declaring class 104 * @param method annotated method 105 * @param declaringClass declaring class info 106 * @return new annotated method 107 */ 108 public static <X> AnnotatedMethod<X> newAnnotatedMethod(Method method, Class<X> declaringClass) 109 { 110 return new AnnotatedMethodImpl<X>(method); 111 } 112 113 }