View Javadoc
1   /**
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3    *
4    * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
5    * Portions Copyright 2013-2018 Philip Helger + contributors
6    *
7    * The contents of this file are subject to the terms of either the GNU
8    * General Public License Version 2 only ("GPL") or the Common Development
9    * and Distribution License("CDDL") (collectively, the "License").  You
10   * may not use this file except in compliance with the License.  You can
11   * obtain a copy of the License at
12   * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13   * or packager/legal/LICENSE.txt.  See the License for the specific
14   * language governing permissions and limitations under the License.
15   *
16   * When distributing the software, include this License Header Notice in each
17   * file and include the License file at packager/legal/LICENSE.txt.
18   *
19   * GPL Classpath Exception:
20   * Oracle designates this particular file as subject to the "Classpath"
21   * exception as provided by Oracle in the GPL Version 2 section of the License
22   * file that accompanied this code.
23   *
24   * Modifications:
25   * If applicable, add the following below the License Header, with the fields
26   * enclosed by brackets [] replaced by your own identifying information:
27   * "Portions Copyright [year] [name of copyright owner]"
28   *
29   * Contributor(s):
30   * If you wish your version of this file to be governed by only the CDDL or
31   * only the GPL Version 2, indicate your decision by adding "[Contributor]
32   * elects to include this software in this distribution under the [CDDL or GPL
33   * Version 2] license."  If you don't indicate a single choice of license, a
34   * recipient has the option to distribute your version of this file under
35   * either the CDDL, the GPL Version 2 or to extend the choice of license to
36   * its licensees as provided above.  However, if you add GPL Version 2 code
37   * and therefore, elected the GPL Version 2 license, then the option applies
38   * only if the new code is made subject to such option by the copyright
39   * holder.
40   */
41  package com.helger.jcodemodel.meta;
42  
43  import java.util.List;
44  
45  import javax.annotation.Nonnull;
46  import javax.lang.model.element.Element;
47  import javax.lang.model.element.ElementKind;
48  import javax.lang.model.element.ExecutableElement;
49  import javax.lang.model.element.TypeElement;
50  import javax.lang.model.element.TypeParameterElement;
51  import javax.lang.model.element.VariableElement;
52  import javax.lang.model.type.TypeKind;
53  import javax.lang.model.type.TypeMirror;
54  
55  import com.helger.jcodemodel.AbstractJClass;
56  import com.helger.jcodemodel.AbstractJType;
57  import com.helger.jcodemodel.JCodeModel;
58  import com.helger.jcodemodel.JDefinedClass;
59  import com.helger.jcodemodel.JMethod;
60  import com.helger.jcodemodel.JTypeVar;
61  import com.helger.jcodemodel.JVar;
62  
63  /**
64   * @author Victor Nazarov <asviraspossible@gmail.com>
65   */
66  class ClassFiller
67  {
68    private final JDefinedClass m_aNewClass;
69    private final JCodeModel m_aCodeModel;
70    private final DecidedErrorTypesModelsAdapter m_aModelsAdapter;
71  
72    ClassFiller (final JCodeModel codeModel,
73                 final DecidedErrorTypesModelsAdapter modelsAdapter,
74                 final JDefinedClass newClass)
75    {
76      m_aCodeModel = codeModel;
77      m_aModelsAdapter = modelsAdapter;
78      m_aNewClass = newClass;
79    }
80  
81    void fillClass (@Nonnull final TypeElement element,
82                    @Nonnull final TypeEnvironment environment) throws CodeModelBuildingException, ErrorTypeFound
83    {
84      m_aNewClass.hide ();
85      final Annotatorml#Annotator">Annotator classAnnotator = new Annotator (m_aModelsAdapter, m_aNewClass, environment);
86      classAnnotator.annotate (element.getAnnotationMirrors ());
87      for (final TypeParameterElement parameter : element.getTypeParameters ())
88      {
89        final JTypeVar typeVariable = m_aNewClass.generify (parameter.getSimpleName ().toString ());
90        environment.put (typeVariable.name (), typeVariable);
91        for (final TypeMirror type : parameter.getBounds ())
92        {
93          typeVariable.bound ((AbstractJClass) m_aModelsAdapter.toJType (type, environment));
94        }
95      }
96      final TypeMirror superclass = element.getSuperclass ();
97      if (superclass != null && superclass.getKind () != TypeKind.NONE)
98      {
99        m_aNewClass._extends ((AbstractJClass) m_aModelsAdapter.toJType (superclass, environment));
100     }
101     for (final TypeMirror iface : element.getInterfaces ())
102     {
103       m_aNewClass._implements ((AbstractJClass) m_aModelsAdapter.toJType (iface, environment));
104     }
105     for (final Element enclosedElement : element.getEnclosedElements ())
106     {
107       if (enclosedElement.getKind ().equals (ElementKind.INTERFACE) ||
108           enclosedElement.getKind ().equals (ElementKind.CLASS))
109       {
110         final TypeElement innerClassElement = (TypeElement) enclosedElement;
111         m_aModelsAdapter.defineInnerClass (m_aNewClass, innerClassElement, environment.enclosed ());
112       }
113       else
114         if (enclosedElement.getKind ().equals (ElementKind.METHOD))
115         {
116           final ExecutableElement executable = (ExecutableElement) enclosedElement;
117           final JMethod method = m_aNewClass.method (DecidedErrorTypesModelsAdapter.toJMod (executable.getModifiers ()),
118                                                      m_aCodeModel.VOID,
119                                                      executable.getSimpleName ().toString ());
120           final TypeEnvironment methodEnvironment = environment.enclosed ();
121           final Annotatorl#Annotator">Annotator methodAnnotator = new Annotator (m_aModelsAdapter, method, environment);
122           methodAnnotator.annotate (executable.getAnnotationMirrors ());
123           for (final TypeParameterElement parameter : executable.getTypeParameters ())
124           {
125             final JTypeVar typeVariable = method.generify (parameter.getSimpleName ().toString ());
126             methodEnvironment.put (typeVariable.name (), typeVariable);
127             for (final TypeMirror type : parameter.getBounds ())
128             {
129               typeVariable.bound ((AbstractJClass) m_aModelsAdapter.toJType (type, methodEnvironment));
130             }
131           }
132           method.type (m_aModelsAdapter.toJType (executable.getReturnType (), methodEnvironment));
133           for (final TypeMirror type : executable.getThrownTypes ())
134           {
135             final AbstractJClass/com/helger/jcodemodel/AbstractJClass.html#AbstractJClass">AbstractJClass throwable = (AbstractJClass) m_aModelsAdapter.toJType (type, methodEnvironment);
136             method._throws (throwable);
137           }
138           final List <? extends VariableElement> parameters = executable.getParameters ();
139           int n = 0;
140           for (final VariableElement variable : parameters)
141           {
142             final String parameterName = variable.getSimpleName ().toString ();
143             final TypeMirror parameterTypeMirror = variable.asType ();
144             final AbstractJType parameterType = m_aModelsAdapter.toJType (parameterTypeMirror, methodEnvironment);
145             JVar param;
146             if (executable.isVarArgs () && n == parameters.size () - 1)
147             {
148               param = method.varParam (DecidedErrorTypesModelsAdapter.toJMod (variable.getModifiers ()),
149                                        parameterType.elementType (),
150                                        parameterName);
151             }
152             else
153             {
154               param = method.param (DecidedErrorTypesModelsAdapter.toJMod (variable.getModifiers ()),
155                                     parameterType,
156                                     parameterName);
157             }
158             final Annotatornnotator">Annotator parametorAnnotator = new Annotator (m_aModelsAdapter, param, methodEnvironment);
159             parametorAnnotator.annotate (variable.getAnnotationMirrors ());
160             n++;
161           }
162         }
163     }
164   }
165 
166 }