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