Skip to content

Commit

Permalink
Merge pull request #78 from guiguilechat/API_guiguigui
Browse files Browse the repository at this point in the history
made tests use a unified test JCM - bad indentations
  • Loading branch information
phax authored May 25, 2020
2 parents 30087e4 + 857c053 commit ec12c7f
Show file tree
Hide file tree
Showing 36 changed files with 522 additions and 270 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/helger/jcodemodel/AbstractJType.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,16 @@ public boolean isAssignableFrom (@Nonnull final AbstractJType aThat)
return isAssignableFrom (aThat, true);
}

protected boolean isAssignableFrom (@Nonnull final AbstractJType aThat,
final boolean bAllowsRawTypeUnchekedConversion)
protected boolean isAssignableFrom (
@Nonnull final AbstractJType aThat,
final boolean bAllowsRawTypeUnchekedConversion)
{
if (isError () || aThat.isError ())
return false;
if (this.equals (aThat))
if (equals (aThat))
return true;

if (this.isReference () && aThat.isReference ())
if (isReference () && aThat.isReference ())
{
final AbstractJClass thisClass = (AbstractJClass) this;
final AbstractJClass thatClass = (AbstractJClass) aThat;
Expand All @@ -311,8 +312,8 @@ protected boolean isAssignableFrom (@Nonnull final AbstractJType aThat,
return true;

// Array covariance: i. e. Object[] array1 = (Integer[])array2
if (this.isArray () && aThat.isArray ())
return this.elementType ().isAssignableFrom (aThat.elementType (), false);
if (isArray () && aThat.isArray ())
return elementType ().isAssignableFrom (aThat.elementType (), false);

if (thisClass.erasure ().equals (thatClass.erasure ()))
{
Expand Down
132 changes: 92 additions & 40 deletions src/main/java/com/helger/jcodemodel/JCodeModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

import com.helger.jcodemodel.exceptions.JCaseSensitivityChangeException;
import com.helger.jcodemodel.exceptions.JInvalidFileNameException;
import com.helger.jcodemodel.meta.CodeModelBuildingException;
import com.helger.jcodemodel.meta.ErrorTypeFound;
import com.helger.jcodemodel.meta.JCodeModelJavaxLangModelAdapter;
Expand Down Expand Up @@ -238,28 +240,77 @@ public final IFileSystemConvention getFileSystemConvention ()
}

/**
* Set the target file system convention to be used. This method MUST be
* Set the target file system convention to be used. This method is better
* called BEFORE the first package or resource directory is created. Later
* calls result in an exception.
* calls result in an exception, if the new convention tries to change case
* sensitivy or prevent the creation of resources names that are already used.
*
* @param aFSConvention
* The file system convention to be used. May not be <code>null</code>.
* The file system convention to be used. May not be
* <code>null</code>.
* @return this for chaining
* @throws JCodeModelException
* if a package or a resource directory is already present.
* @throws JCaseSensitivityChangeException
* if the new convention has different case sensitivity
* @throws JInvalidFileNameException
* if the new convention does not allow some file previously
* created.
* @see IFileSystemConvention
* @since 3.4.0
*/
@Nonnull
public final JCodeModel setFileSystemConvention (@Nonnull final IFileSystemConvention aFSConvention) throws JCodeModelException
public final IFileSystemConvention setFileSystemConvention (@Nonnull final IFileSystemConvention aFSConvention)
throws JCaseSensitivityChangeException, JInvalidFileNameException
{
JCValueEnforcer.notNull (aFSConvention, "FSConvention");
if (!m_aPackages.isEmpty () || !m_aResourceDirs.isEmpty ())
throw new JCodeModelException ("The FileSystem convention cannot be changed if a package or a resource directory already exists.");
if (aFSConvention == m_aFSConvention)
return m_aFSConvention;
IFileSystemConvention old = m_aFSConvention;
if (!m_aResourceDirs.isEmpty ())
{
// test null in case we set the platform from the constructor
if (m_aFSConvention != null && m_aFSConvention.isCaseSensistive () != aFSConvention.isCaseSensistive ())
throw new JCaseSensitivityChangeException ();
for (FSName name : m_aResourceDirs.keySet ())
{
String sName = name.getName ();

// copy from JresourceDir. should be mutualized ?

// An empty directory name is okay
if (sName.length () > 0)
for (final String sPart : JCStringHelper.getExplodedArray (JResourceDir.SEPARATOR, sName))
if (!aFSConvention.isValidDirectoryName (sPart))
throw new JInvalidFileNameException (sName, sPart);
}

// nothing to do with packages, file names convention is not relevant to
// them.
}
m_aFSConvention = aFSConvention;
return old;
}

@Nonnull
public final JCodeModel withFileSystemConvention (@Nonnull final IFileSystemConvention aFSConvention)
throws JCodeModelException
{
setFileSystemConvention (aFSConvention);
return this;
}

public static JCodeModel createUnified ()
{
try
{
return new JCodeModel ().withFileSystemConvention (EFileSystemConvention.LINUX);
}
catch (JCodeModelException e)
{
// should not happen
throw new UnsupportedOperationException ("catch this", e);
}
}

/**
* Add a package to the list of packages to be generated
*
Expand Down Expand Up @@ -331,13 +382,17 @@ private FSName _createFSName (@Nonnull final String sName)
* Name of the resource directory. Use "" to indicate the root
* directory.
* @return Newly generated resource directory. Never <code>null</code>.
* @throws JCodeModelException
* @throws JInvalidFileNameException
* if the name is invalid for current platform.
* @throws JResourceAlreadyExistsException
* If the resource directory could not be created because another
* file or class already has this name.
* @see #rootResourceDir()
* @since v3.4.0
*/
@Nonnull
public JResourceDir resourceDir (@Nonnull final String sName) throws JCodeModelException
public JResourceDir resourceDir (@Nonnull final String sName)
throws JResourceAlreadyExistsException, JInvalidFileNameException
{
JCValueEnforcer.notNull (sName, "Name");

Expand Down Expand Up @@ -367,8 +422,14 @@ public JResourceDir resourceDir (@Nonnull final String sName) throws JCodeModelE

// Get main subdir
final JResourceDir aFinalParentDir = aParentDir;
aCur = m_aResourceDirs.computeIfAbsent (_createFSName (sDirName),
k -> new JResourceDir (this, aFinalParentDir, k.getName ()));
FSName curName = _createFSName (sDirName);
// cannot use computeifAbsent because exception thrown.
aCur = m_aResourceDirs.get (curName);
if (aCur == null)
{
aCur = new JResourceDir (this, aFinalParentDir, curName.getName ());
m_aResourceDirs.put (curName, aCur);
}
aParentDir = aCur;
}

Expand Down Expand Up @@ -446,9 +507,7 @@ public JDefinedClass _class (final int nMods,
if (nIdx < 0)
return rootPackage ()._class (nMods, sFullyQualifiedClassName, eClassType);
return _package (sFullyQualifiedClassName.substring (0, nIdx))._class (nMods,
sFullyQualifiedClassName.substring (nIdx +
1),
eClassType);
sFullyQualifiedClassName.substring (nIdx + 1), eClassType);
}

/**
Expand Down Expand Up @@ -478,8 +537,8 @@ public JDefinedClass _class (@Nonnull final String sFullyQualifiedClassName) thr
* When the specified class/interface was already created.
*/
@Nonnull
public JDefinedClass _class (final int nMods,
@Nonnull final String sFullyQualifiedClassName) throws JCodeModelException
public JDefinedClass _class (final int nMods, @Nonnull final String sFullyQualifiedClassName)
throws JCodeModelException
{
return _class (nMods, sFullyQualifiedClassName, EClassType.CLASS);
}
Expand All @@ -496,8 +555,8 @@ public JDefinedClass _class (final int nMods,
* When the specified class/interface was already created.
*/
@Nonnull
public JDefinedClass _class (@Nonnull final String sFullyQualifiedClassName,
@Nonnull final EClassType eClassType) throws JCodeModelException
public JDefinedClass _class (@Nonnull final String sFullyQualifiedClassName, @Nonnull final EClassType eClassType)
throws JCodeModelException
{
return _class (JMod.PUBLIC, sFullyQualifiedClassName, eClassType);
}
Expand Down Expand Up @@ -637,8 +696,8 @@ public JDefinedClass _getClass (@Nonnull final String sFullyQualifiedClassName)
final int nIndex = sFullyQualifiedClassName.lastIndexOf (JPackage.SEPARATOR);
if (nIndex < 0)
return rootPackage ()._getClass (sFullyQualifiedClassName);
return _package (sFullyQualifiedClassName.substring (0,
nIndex))._getClass (sFullyQualifiedClassName.substring (nIndex + 1));
return _package (sFullyQualifiedClassName.substring (0, nIndex))
._getClass (sFullyQualifiedClassName.substring (nIndex + 1));
}

/**
Expand Down Expand Up @@ -771,9 +830,10 @@ public void build (@Nonnull final File aDestDir, @Nullable final PrintStream aSt
*/
@Deprecated
@ChangeInV4
public void build (@Nonnull final File aSrcDir,
@Nonnull final File aResourceDir,
@Nullable final PrintStream aStatusPS) throws IOException
public void build (
@Nonnull final File aSrcDir,
@Nonnull final File aResourceDir,
@Nullable final PrintStream aStatusPS) throws IOException
{
AbstractCodeWriter res = new FileCodeWriter (aResourceDir, m_aBuildingCharset, m_sBuildingNewLine);
AbstractCodeWriter src = new FileCodeWriter (aSrcDir, m_aBuildingCharset, m_sBuildingNewLine);
Expand Down Expand Up @@ -852,8 +912,8 @@ public void build (@Nonnull final AbstractCodeWriter aWriter) throws IOException
*/
@Deprecated
@ChangeInV4
public void build (@Nonnull final AbstractCodeWriter aSource,
@Nonnull final AbstractCodeWriter aResource) throws IOException
public void build (@Nonnull final AbstractCodeWriter aSource, @Nonnull final AbstractCodeWriter aResource)
throws IOException
{
new JCMWriter (this).setCharset (m_aBuildingCharset).setNewLine (m_sBuildingNewLine).build (aSource, aResource);
}
Expand Down Expand Up @@ -892,10 +952,8 @@ public AbstractJClass ref (@Nonnull final Class <?> aClazz)
if (aRefClass == null)
{
if (aClazz.isPrimitive ())
{
// Cannot return BYTE etc. because the return type does not match
throw new IllegalArgumentException (aClazz + " is a primitive");
}

if (aClazz.isArray ())
{
Expand Down Expand Up @@ -940,8 +998,8 @@ public AbstractJClass ref (@Nonnull final Class <?> aClazz)
* @see #refWithErrorTypes(TypeElement,Elements)
*/
@Nonnull
public JDefinedClass ref (@Nonnull final TypeElement aElement,
@Nonnull final Elements aElementUtils) throws ErrorTypeFound, CodeModelBuildingException
public JDefinedClass ref (@Nonnull final TypeElement aElement, @Nonnull final Elements aElementUtils)
throws ErrorTypeFound, CodeModelBuildingException
{
final JCodeModelJavaxLangModelAdapter adapter = new JCodeModelJavaxLangModelAdapter (this, aElementUtils);
return adapter.getClass (aElement);
Expand Down Expand Up @@ -977,8 +1035,8 @@ public JDefinedClass ref (@Nonnull final TypeElement aElement,
* @see #buildsErrorTypeRefs()
*/
@Nonnull
public JDefinedClass refWithErrorTypes (@Nonnull final TypeElement aElement,
@Nonnull final Elements aElementUtils) throws CodeModelBuildingException
public JDefinedClass refWithErrorTypes (@Nonnull final TypeElement aElement, @Nonnull final Elements aElementUtils)
throws CodeModelBuildingException
{
final JCodeModelJavaxLangModelAdapter adapter = new JCodeModelJavaxLangModelAdapter (this, aElementUtils);
return adapter.getClassWithErrorTypes (aElement);
Expand Down Expand Up @@ -1049,10 +1107,8 @@ public AbstractJClass ref (@Nonnull final String sFullyQualifiedClassName)
public AbstractJClass wildcard ()
{
if (m_aWildcard == null)
{
// "Hack" if base class is "Object" it is omitted
m_aWildcard = ref (Object.class).wildcardExtends ();
}
return m_aWildcard;
}

Expand All @@ -1072,10 +1128,8 @@ public AbstractJType parseType (@Nonnull final String sName)
{
// array
if (sName.endsWith ("[]"))
{
// Simply remove trailing "[]"
return parseType (sName.substring (0, sName.length () - 2)).array ();
}

// try primitive type
try
Expand Down Expand Up @@ -1137,8 +1191,8 @@ AbstractJClass parseTypeName ()
}

// not supported
throw new IllegalArgumentException ("only extends/super can follow ?, but found " +
m_sTypeName.substring (m_nIdx));
throw new IllegalArgumentException (
"only extends/super can follow ?, but found " + m_sTypeName.substring (m_nIdx));
}

while (m_nIdx < m_sTypeName.length ())
Expand All @@ -1162,10 +1216,8 @@ AbstractJClass parseTypeName ()
private AbstractJClass _parseSuffix (@Nonnull final AbstractJClass aClazz)
{
if (m_nIdx == m_sTypeName.length ())
{
// hit EOL
return aClazz;
}

final char ch = m_sTypeName.charAt (m_nIdx);

Expand Down
32 changes: 18 additions & 14 deletions src/main/java/com/helger/jcodemodel/JResourceDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.jcodemodel.exceptions.JInvalidFileNameException;
import com.helger.jcodemodel.fmt.AbstractJResourceFile;
import com.helger.jcodemodel.util.FSName;
import com.helger.jcodemodel.util.JCFilenameHelper;
Expand Down Expand Up @@ -97,12 +98,11 @@ public class JResourceDir implements IJOwned
* Name of directory. May not be <code>null</code> but empty. No
* absolute paths are allowed and only Linux forward slashes may be
* used as path separators.
* @throws IllegalArgumentException
* If each part of the package name is not a valid filename part.
* @throws JInvalidFileNameException
* If a part of the package name is not a valid filename part.
*/
protected JResourceDir (@Nonnull final JCodeModel aOwner,
@Nullable final JResourceDir aParentDir,
@Nonnull final String sName)
protected JResourceDir (@Nonnull final JCodeModel aOwner, @Nullable final JResourceDir aParentDir,
@Nonnull final String sName) throws JInvalidFileNameException
{
JCValueEnforcer.notNull (sName, "Name");
JCValueEnforcer.notNull (aOwner, "CodeModel");
Expand All @@ -119,17 +119,14 @@ protected JResourceDir (@Nonnull final JCodeModel aOwner,
if (sName.length () > 0)
for (final String sPart : JCStringHelper.getExplodedArray (JResourceDir.SEPARATOR, sName))
if (!aOwner.getFileSystemConvention ().isValidDirectoryName (sPart))
throw new IllegalArgumentException ("Resource directory name '" +
sName +
"' contains the the invalid part '" +
sPart +
"' according to the current file system conventions");
throw new JInvalidFileNameException (sName, sPart);
}

/**
* @return the code model root object being used to create this resource
* directory.
*/
@Override
@Nonnull
public final JCodeModel owner ()
{
Expand Down Expand Up @@ -194,9 +191,8 @@ public <T extends AbstractJResourceFile> T addResourceFile (@Nonnull final T aRe
final String sName = aResFile.name ();

if (!m_aOwner.getFileSystemConvention ().isValidFilename (sName))
throw new IllegalArgumentException ("Resource filename '" +
sName +
"' is invalid according to the current file system conventions");
throw new IllegalArgumentException (
"Resource filename '" + sName + "' is invalid according to the current file system conventions");

// Check if a sub directory already exists with the same name
if (m_aOwner.containsResourceDir (fullChildName (sName)))
Expand Down Expand Up @@ -318,6 +314,14 @@ String fullChildName (@Nonnull final String sChildName)
@Nonnull
static JResourceDir root (@Nonnull final JCodeModel aOwner)
{
return new JResourceDir (aOwner, null, "");
try
{
return new JResourceDir (aOwner, null, "");
}
catch (JInvalidFileNameException e)
{
// should not happen
throw new UnsupportedOperationException ("catch this", e);
}
}
}
Loading

0 comments on commit ec12c7f

Please sign in to comment.