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-2017 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;
42  
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  import javax.annotation.Nonnull;
48  import javax.annotation.Nullable;
49  
50  /**
51   * A special {@link AbstractJClass} that represents an unknown class (except its
52   * name.)
53   *
54   * @author Kohsuke Kawaguchi
55   * @see JCodeModel#directClass(String)
56   */
57  public class JDirectClass extends AbstractJClassContainer <JDirectClass>
58  {
59    private final String m_sFullName;
60  
61    @Deprecated
62    protected JDirectClass (@Nonnull final JCodeModel aOwner, @Nonnull final String sFullName)
63    {
64      this (aOwner, null, EClassType.CLASS, sFullName);
65    }
66  
67    @Nonnull
68    private static String _getName (@Nonnull final String sFullName)
69    {
70      final int nLast = sFullName.lastIndexOf ('.');
71      if (nLast < 0)
72        return sFullName;
73      return sFullName.substring (nLast + 1);
74    }
75  
76    protected JDirectClass (@Nonnull final JCodeModel aOwner,
77                            @Nullable final IJClassContainer <?> aOuter,
78                            @Nonnull final EClassType eClassType,
79                            @Nonnull final String sFullName)
80    {
81      super (aOwner, aOuter, eClassType, _getName (sFullName));
82      m_sFullName = sFullName;
83    }
84  
85    @Override
86    @Nonnull
87    public String name ()
88    {
89      return super.name ();
90    }
91  
92    /**
93     * Gets the fully qualified name of this class.
94     */
95    @Override
96    @Nonnull
97    public String fullName ()
98    {
99      if (getOuter () instanceof AbstractJClassContainer <?>)
100       return ((AbstractJClassContainer <?>) getOuter ()).fullName () + '.' + m_sFullName;
101 
102     // The fully qualified name was already provided in the ctor
103     return m_sFullName;
104   }
105 
106   @Override
107   @Nonnull
108   public JPackage _package ()
109   {
110     final IJClassContainer <?> aOuter = getOuter ();
111     if (aOuter instanceof AbstractJClassContainer <?>)
112       return ((AbstractJClassContainer <?>) aOuter)._package ();
113     if (aOuter instanceof JPackage)
114       return (JPackage) aOuter;
115 
116     // No package present - use name based analysis
117     final String sFullName = fullName ();
118     final int i = sFullName.lastIndexOf ('.');
119     if (i >= 0)
120       return owner ()._package (sFullName.substring (0, i));
121     return owner ().rootPackage ();
122   }
123 
124   @Override
125   @Nonnull
126   public AbstractJClass _extends ()
127   {
128     return owner ().ref (Object.class);
129   }
130 
131   @Override
132   @Nonnull
133   public Iterator <AbstractJClass> _implements ()
134   {
135     return Collections.<AbstractJClass> emptyList ().iterator ();
136   }
137 
138   @Override
139   public boolean isAbstract ()
140   {
141     return false;
142   }
143 
144   @Override
145   @Nonnull
146   protected AbstractJClass substituteParams (final JTypeVar [] aVariables,
147                                              final List <? extends AbstractJClass> aBindings)
148   {
149     return this;
150   }
151 
152   @Override
153   @Nonnull
154   protected JDirectClass createInnerClass (final int nMods, final EClassType eClassType, final String sName)
155   {
156     return new JDirectClass (owner (), this, eClassType, sName);
157   }
158 }