Skip to content

Commit

Permalink
Added getInner as requested by #60
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Apr 23, 2018
1 parent cf0c847 commit 9dcb8e9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
24 changes: 24 additions & 0 deletions src/main/java/com/helger/jcodemodel/AbstractJClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -425,6 +426,29 @@ public final JTypeWildcard wildcard (@Nonnull final EWildcardBoundMode eMode)
protected abstract AbstractJClass substituteParams (@Nonnull JTypeVar [] aVariables,
@Nonnull List <? extends AbstractJClass> aBindings);

/**
* Check if this class is a class container and if so try to find the inner
* class with the provided name.
*
* @param sName
* The name to check. May be <code>null</code>.
* @return The inner class with the provided name.
* @since 3.0.3
*/
@Nullable
public AbstractJClassContainer <?> getInnerClass (@Nullable final String sName)
{
if (sName != null && this instanceof AbstractJClassContainer <?>)
{
@SuppressWarnings ("rawtypes")
final Collection <AbstractJClassContainer <?>> aInnerClasses = ((AbstractJClassContainer) this).classes ();
for (final AbstractJClassContainer <?> aInnerClass : aInnerClasses)
if (aInnerClass.name ().equals (sName))
return aInnerClass;
}
return null;
}

/**
* @return name<code>.class</code>
*/
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/com/helger/jcodemodel/AbstractJClassContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public abstract class AbstractJClassContainer <CLASSTYPE extends AbstractJClassC
* capitalized in a case sensitive file system (
* {@link JCodeModel#isFileSystemCaseSensitive()}) to avoid conflicts. Lazily
* created to save footprint.
*
* @see #_getClasses()
*/
protected Map <String, CLASSTYPE> m_aClasses;

Expand Down Expand Up @@ -178,7 +176,8 @@ public String fullName ()
}

/**
* @return <code>true</code> if this is an anonymous class.
* @return <code>true</code> if this is an anonymous class. Note: this applies
* only to classes.
*/
public final boolean isAnonymous ()
{
Expand Down Expand Up @@ -213,14 +212,6 @@ public final JPackage getPackage ()
return parentContainer ().getPackage ();
}

@Nonnull
private Map <String, CLASSTYPE> _getClasses ()
{
if (m_aClasses == null)
m_aClasses = new TreeMap <> ();
return m_aClasses;
}

@Nonnull
protected abstract CLASSTYPE createInnerClass (final int nMods,
@Nonnull final EClassType eClassType,
Expand All @@ -237,12 +228,19 @@ public final CLASSTYPE _class (final int nMods,
else
sRealName = sName;

final CLASSTYPE aExistingClass = _getClasses ().get (sRealName);
if (aExistingClass != null)
throw new JClassAlreadyExistsException (aExistingClass);
// Existing class?
if (m_aClasses != null)
{
final CLASSTYPE aExistingClass = m_aClasses.get (sRealName);
if (aExistingClass != null)
throw new JClassAlreadyExistsException (aExistingClass);
}
else
m_aClasses = new TreeMap <> ();

// Create and add inner class
final CLASSTYPE c = createInnerClass (nMods, eClassType, sName);
_getClasses ().put (sRealName, c);
m_aClasses.put (sRealName, c);
return c;
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/helger/jcodemodel/InnerClassFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
package com.helger.jcodemodel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import org.junit.Test;

Expand All @@ -57,6 +59,8 @@ public void innerClassesAreImported () throws Exception
final JDefinedClass daInnerInner = daInner1._class ("InnerInner");
final JDefinedClass daInner2 = aClass._class ("DaTestClassInner");
final JDefinedClass daInner2Inner = daInner2._class ("Inner2");
assertSame (daInner2Inner, daInner2.getInnerClass ("Inner2"));
assertNull (daInner2.getInnerClass ("Inner3"));

assertEquals ("Inner", daInner1.name ());
assertEquals ("org.test.DaTestClass.Inner", daInner1.fullName ());
Expand Down

0 comments on commit 9dcb8e9

Please sign in to comment.