Skip to content

Commit

Permalink
Added possibility to define classes not to be imported; #51
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed May 17, 2017
1 parent cd65670 commit ea76217
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 62 deletions.
107 changes: 76 additions & 31 deletions src/main/java/com/helger/jcodemodel/JCodeModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -197,11 +200,13 @@ public static boolean isFileSystemCaseSensitive ()
private AbstractJClass m_aWildcard;

/** The charset used for building the output - null means system default */
private Charset m_aBuildingCharset = null;
private Charset m_aBuildingCharset;

/** The newline string to be used. Defaults to system default */
private String m_sBuildingNewLine = AbstractCodeWriter.getDefaultNewLine ();

private final Set <AbstractJClass> m_aDontImportClasses = new HashSet <> ();

public JCodeModel ()
{}

Expand Down Expand Up @@ -555,95 +560,95 @@ public void build (@Nonnull final File destDir, @Nullable final PrintStream stat
* Generates Java source code. A convenience method that calls
* {@link #build(AbstractCodeWriter,AbstractCodeWriter)}.
*
* @param srcDir
* @param aSrcDir
* Java source files are generated into this directory.
* @param resourceDir
* @param aResourceDir
* Other resource files are generated into this directory.
* @param status
* @param aStatus
* Progress stream. May be <code>null</code>.
* @throws IOException
* on IO error if non-null, progress indication will be sent to this
* stream.
*/
public void build (@Nonnull final File srcDir,
@Nonnull final File resourceDir,
@Nullable final PrintStream status) throws IOException
public void build (@Nonnull final File aSrcDir,
@Nonnull final File aResourceDir,
@Nullable final PrintStream aStatus) throws IOException
{
AbstractCodeWriter res = new FileCodeWriter (resourceDir, m_aBuildingCharset, m_sBuildingNewLine);
AbstractCodeWriter src = new FileCodeWriter (srcDir, m_aBuildingCharset, m_sBuildingNewLine);
if (status != null)
AbstractCodeWriter res = new FileCodeWriter (aResourceDir, m_aBuildingCharset, m_sBuildingNewLine);
AbstractCodeWriter src = new FileCodeWriter (aSrcDir, m_aBuildingCharset, m_sBuildingNewLine);
if (aStatus != null)
{
src = new ProgressCodeWriter (src, status);
res = new ProgressCodeWriter (res, status);
src = new ProgressCodeWriter (src, aStatus);
res = new ProgressCodeWriter (res, aStatus);
}
build (src, res);
}

/**
* A convenience method for <code>build(destDir,System.out)</code>.
*
* @param destDir
* @param aDestDir
* source files and resources are generated into this directory.
* @throws IOException
* on IO error
*/
public void build (@Nonnull final File destDir) throws IOException
public void build (@Nonnull final File aDestDir) throws IOException
{
build (destDir, System.out);
build (aDestDir, System.out);
}

/**
* A convenience method for <code>build(srcDir,resourceDir,System.out)</code>.
*
* @param srcDir
* @param aSrcDir
* Java source files are generated into this directory.
* @param resourceDir
* @param aResourceDir
* Other resource files are generated into this directory.
* @throws IOException
* on IO error
*/
public void build (@Nonnull final File srcDir, @Nonnull final File resourceDir) throws IOException
public void build (@Nonnull final File aSrcDir, @Nonnull final File aResourceDir) throws IOException
{
build (srcDir, resourceDir, System.out);
build (aSrcDir, aResourceDir, System.out);
}

/**
* A convenience method for <code>build(out,out)</code>.
*
* @param out
* @param aWriter
* Source code and resource writer
* @throws IOException
* on IO error
*/
public void build (@Nonnull final AbstractCodeWriter out) throws IOException
public void build (@Nonnull final AbstractCodeWriter aWriter) throws IOException
{
build (out, out);
build (aWriter, aWriter);
}

/**
* Generates Java source code.
*
* @param source
* @param aSource
* Source code writer
* @param resource
* @param aResource
* Resource writer
* @throws IOException
* on IO error
*/
public void build (@Nonnull final AbstractCodeWriter source,
@Nonnull final AbstractCodeWriter resource) throws IOException
public void build (@Nonnull final AbstractCodeWriter aSource,
@Nonnull final AbstractCodeWriter aResource) throws IOException
{
try
{
final JPackage [] pkgs = m_aPackages.values ().toArray (new JPackage [m_aPackages.size ()]);
// avoid concurrent modification exception
// Copy to avoid concurrent modification exception
final List <JPackage> pkgs = new ArrayList <> (m_aPackages.values ());
for (final JPackage pkg : pkgs)
pkg.build (source, resource);
pkg.build (aSource, aResource);
}
finally
{
source.close ();
resource.close ();
aSource.close ();
aResource.close ();
}
}

Expand Down Expand Up @@ -1009,4 +1014,44 @@ private AbstractJClass _parseArguments (@Nonnull final AbstractJClass rawType)
}
}
}

/**
* Add a class that should <strong>NOT</strong> be imported.
*
* @param aClass
* The class to use. May not be <code>null</code>.
* @return <code>true</code> if it was added, <code>false</code> if it was
* already contained.
* @since 3.0.0
*/
public boolean addDontImportClass (@Nonnull final Class <?> aClass)
{
return addDontImportClass (ref (aClass));
}

/**
* Add a class that should <strong>NOT</strong> be imported.
*
* @param aClass
* The class to use. May not be <code>null</code>.
* @return <code>true</code> if it was added, <code>false</code> if it was
* already contained.
* @since 3.0.0
*/
public boolean addDontImportClass (@Nonnull final AbstractJClass aClass)
{
JCValueEnforcer.notNull (aClass, "Class");
return m_aDontImportClasses.add (aClass);
}

/**
* @return A copy all classes that should not be imported. Never
* <code>null</code> but maybe empty.
* @since 3.0.0
*/
@Nonnull
public Collection <AbstractJClass> getAllDontImportClasses ()
{
return new HashSet <> (m_aDontImportClasses);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/helger/jcodemodel/JFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ private static enum EMode

private final class ImportedClasses
{
private final Set <AbstractJClass> m_aDontImportClasses = new HashSet <> ();
private final Set <AbstractJClass> m_aClasses = new HashSet <> ();
private final Set <String> m_aNames = new HashSet <> ();

Expand All @@ -248,10 +249,23 @@ private AbstractJClass _getClassForImport (@Nullable final AbstractJClass aClass
return aRealClass;
}

public void addDontImportClass (@Nonnull final AbstractJClass aClass)
{
final AbstractJClass aRealClass = _getClassForImport (aClass);
m_aDontImportClasses.add (aRealClass);
}

public boolean add (@Nonnull final AbstractJClass aClass)
{
final AbstractJClass aRealClass = _getClassForImport (aClass);

if (m_aDontImportClasses.contains (aRealClass))
{
if (m_bImportDebug)
System.out.println ("The class '" + aRealClass.fullName () + "' should not be imported!");
return false;
}

// Avoid importing 2 classes with the same class name
if (!m_aNames.add (aRealClass.name ()))
{
Expand Down Expand Up @@ -1067,6 +1081,20 @@ void write (@Nonnull final JDefinedClass aClassToBeWritten)
declaration (aClassToBeWritten);
}

/**
* Add classes that should not be imported.
*
* @param aClasses
* The classes to not be used in "import" statements. May be
* <code>null</code>.
*/
public void addDontImportClasses (@Nullable final Iterable <? extends AbstractJClass> aClasses)
{
if (aClasses != null)
for (final AbstractJClass aClass : aClasses)
m_aImportedClasses.addDontImportClass (aClass);
}

public static boolean containsErrorTypes (@Nonnull final JDefinedClass c)
{
final JFormatter aFormatter = new JFormatter (NullWriter.getInstance ());
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/helger/jcodemodel/JPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.WillNotClose;

import com.helger.jcodemodel.util.JCValueEnforcer;

Expand Down Expand Up @@ -548,7 +549,19 @@ public void generate (@Nonnull final JFormatter f)
f.print (m_sName);
}

void build (final AbstractCodeWriter src, final AbstractCodeWriter res) throws IOException
@Nonnull
private JFormatter _createJavaSourceFileWriter (@Nonnull final AbstractCodeWriter src,
@Nonnull final String className) throws IOException
{
final SourcePrintWriter aWriter = src.openSource (this, className + ".java");
final JFormatter ret = new JFormatter (aWriter);
// Add all classes to not be imported (may be empty)
ret.addDontImportClasses (m_aOwner.getAllDontImportClasses ());
return ret;
}

void build (@Nonnull @WillNotClose final AbstractCodeWriter aSrcWriter,
@Nonnull @WillNotClose final AbstractCodeWriter aResWriter) throws IOException
{
// write classes
for (final JDefinedClass c : m_aClasses.values ())
Expand All @@ -559,7 +572,7 @@ void build (final AbstractCodeWriter src, final AbstractCodeWriter res) throws I
continue;
}

try (final JFormatter f = _createJavaSourceFileWriter (src, c.name ()))
try (final JFormatter f = _createJavaSourceFileWriter (aSrcWriter, c.name ()))
{
f.write (c);
}
Expand All @@ -568,7 +581,7 @@ void build (final AbstractCodeWriter src, final AbstractCodeWriter res) throws I
// write package annotations
if (m_aAnnotations != null || m_aJavaDoc != null)
{
try (final JFormatter f = _createJavaSourceFileWriter (src, "package-info"))
try (final JFormatter f = _createJavaSourceFileWriter (aSrcWriter, "package-info"))
{
if (m_aJavaDoc != null)
f.generable (m_aJavaDoc);
Expand All @@ -586,7 +599,7 @@ void build (final AbstractCodeWriter src, final AbstractCodeWriter res) throws I
// write resources
for (final AbstractJResourceFile rsrc : m_aResources)
{
final AbstractCodeWriter cw = rsrc.isResource () ? res : src;
final AbstractCodeWriter cw = rsrc.isResource () ? aResWriter : aSrcWriter;
try (final OutputStream os = new BufferedOutputStream (cw.openBinary (this, rsrc.name ())))
{
rsrc.build (os);
Expand Down Expand Up @@ -634,12 +647,4 @@ boolean buildsErrorTypeRefs ()

return ret;
}

@Nonnull
private JFormatter _createJavaSourceFileWriter (@Nonnull final AbstractCodeWriter src,
@Nonnull final String className) throws IOException
{
final SourcePrintWriter aWriter = src.openSource (this, className + ".java");
return new JFormatter (aWriter);
}
}

0 comments on commit ea76217

Please sign in to comment.