[jboss-jdeparser2] 01/01: Imported Upstream version 2.0.1
Markus Koschany
apo-guest at moszumanska.debian.org
Sun Mar 20 17:30:11 UTC 2016
This is an automated email from the git hooks/post-receive script.
apo-guest pushed a commit to annotated tag upstream/2.0.1
in repository jboss-jdeparser2.
commit 8b80cb1f1eca88df7772f6a5926e0d601f9a1941
Author: Markus Koschany <apo at debian.org>
Date: Sun Mar 20 18:27:56 2016 +0100
Imported Upstream version 2.0.1
---
README.md | 16 ++++
pom.xml | 2 +-
.../java/org/jboss/jdeparser/AbstractJCall.java | 2 +-
.../org/jboss/jdeparser/AbstractJClassDef.java | 10 +++
.../java/org/jboss/jdeparser/AbstractJType.java | 10 ++-
src/main/java/org/jboss/jdeparser/ArrayJType.java | 12 ++-
.../java/org/jboss/jdeparser/ArrayLookupJExpr.java | 2 +-
src/main/java/org/jboss/jdeparser/BasicJBlock.java | 4 +-
src/main/java/org/jboss/jdeparser/CastJExpr.java | 2 +-
src/main/java/org/jboss/jdeparser/CondJExpr.java | 6 +-
.../java/org/jboss/jdeparser/FieldRefJExpr.java | 2 +-
.../java/org/jboss/jdeparser/ForEachJBlock.java | 1 +
.../org/jboss/jdeparser/FormatPreferences.java | 2 +
.../java/org/jboss/jdeparser/ImplJSourceFile.java | 85 +++++++++++-----------
.../jboss/jdeparser/InnerJAnonymousClassDef.java | 2 +-
.../java/org/jboss/jdeparser/InnerNewJCall.java | 2 +-
.../java/org/jboss/jdeparser/InstanceJCall.java | 4 +-
src/main/java/org/jboss/jdeparser/JClassItem.java | 8 ++
src/main/java/org/jboss/jdeparser/JExprs.java | 2 +-
src/main/java/org/jboss/jdeparser/JLambdaImpl.java | 15 +++-
.../java/org/jboss/jdeparser/NarrowedJType.java | 12 +++
src/main/java/org/jboss/jdeparser/NestedJType.java | 22 +++++-
.../java/org/jboss/jdeparser/PrimitiveJType.java | 8 ++
.../java/org/jboss/jdeparser/ReferenceJType.java | 18 ++++-
.../java/org/jboss/jdeparser/SourceFileWriter.java | 16 ++--
.../java/org/jboss/jdeparser/SourceWriter.java | 29 --------
src/main/java/org/jboss/jdeparser/StaticJCall.java | 12 +--
.../java/org/jboss/jdeparser/StaticRefJExpr.java | 11 ++-
src/main/java/org/jboss/jdeparser/ThisJType.java | 8 ++
src/main/java/org/jboss/jdeparser/Tokens.java | 2 +-
src/main/java/org/jboss/jdeparser/UnaryJExpr.java | 2 +
.../java/org/jboss/jdeparser/WildcardJType.java | 27 ++++++-
.../org/jboss/jdeparser/SimpleExampleTestCase.java | 4 +
33 files changed, 242 insertions(+), 118 deletions(-)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6f82fd8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,16 @@
+# jdeparser2
+Version 2.x of JDeparser, a Java source code generating library.
+
+## Usage and documentionation
+
+The online JavaDoc is located at https://jdeparser.github.io/jdeparser2.
+
+See org.jboss.jdeparser.SimpleExampleTestCase for an example to get you off the ground quick.
+
+## Source
+
+The source code is found at https://github.com/jdeparser/jdeparser2 - feel free to clone and contribute.
+
+## Bug Tracker
+
+The bug tracker can be found at https://issues.jboss.org/browse/JDP.
diff --git a/pom.xml b/pom.xml
index 2e60967..f83ddca 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
<groupId>org.jboss.jdeparser</groupId>
<artifactId>jdeparser</artifactId>
- <version>2.0.0.Final</version>
+ <version>2.0.1.Final</version>
<parent>
<groupId>org.jboss</groupId>
diff --git a/src/main/java/org/jboss/jdeparser/AbstractJCall.java b/src/main/java/org/jboss/jdeparser/AbstractJCall.java
index 644d7c0..0467486 100644
--- a/src/main/java/org/jboss/jdeparser/AbstractJCall.java
+++ b/src/main/java/org/jboss/jdeparser/AbstractJCall.java
@@ -104,7 +104,7 @@ abstract class AbstractJCall extends AbstractJExpr implements JCall, AllowedStat
}
public void write(final SourceFileWriter writer) throws IOException {
- writer.write(FormatPreferences.Space.BEFORE_PAREN);
+ writer.write(FormatPreferences.Space.BEFORE_PAREN_METHOD_CALL);
writer.write($PUNCT.PAREN.OPEN);
writer.write(FormatPreferences.Space.WITHIN_PAREN_METHOD_DECLARATION);
if (args != null) {
diff --git a/src/main/java/org/jboss/jdeparser/AbstractJClassDef.java b/src/main/java/org/jboss/jdeparser/AbstractJClassDef.java
index a7fd728..d56a44a 100644
--- a/src/main/java/org/jboss/jdeparser/AbstractJClassDef.java
+++ b/src/main/java/org/jboss/jdeparser/AbstractJClassDef.java
@@ -89,6 +89,14 @@ abstract class AbstractJClassDef extends AbstractJGeneric implements JClassDef,
return item;
}
+ public JComment lineComment() {
+ return add(new LineJComment());
+ }
+
+ public JComment blockComment() {
+ return add(new BlockJComment());
+ }
+
public JClassDefSection section() {
return add(new JClassDefSectionImpl(this));
}
@@ -372,7 +380,9 @@ abstract class AbstractJClassDef extends AbstractJGeneric implements JClassDef,
sourceFileWriter.write(ifExt ? $KW.EXTENDS : $KW.IMPLEMENTS);
sourceFileWriter.write(iterator.next());
while (iterator.hasNext()) {
+ sourceFileWriter.write(FormatPreferences.Space.BEFORE_COMMA);
sourceFileWriter.write($PUNCT.COMMA);
+ sourceFileWriter.write(FormatPreferences.Space.AFTER_COMMA);
sourceFileWriter.write(iterator.next());
}
}
diff --git a/src/main/java/org/jboss/jdeparser/AbstractJType.java b/src/main/java/org/jboss/jdeparser/AbstractJType.java
index 463c65d..4e5f5fb 100644
--- a/src/main/java/org/jboss/jdeparser/AbstractJType.java
+++ b/src/main/java/org/jboss/jdeparser/AbstractJType.java
@@ -40,10 +40,18 @@ abstract class AbstractJType implements JType {
throw new IllegalArgumentException("Using a JType from a different implementation");
}
- String qualifiedName(SourceFileWriter writer) {
+ String qualifiedName() {
return simpleName();
}
+ public abstract int hashCode();
+
+ public final boolean equals(Object other) {
+ return other instanceof AbstractJType && equals((AbstractJType) other);
+ }
+
+ abstract boolean equals(AbstractJType other);
+
public abstract String simpleName();
public abstract String toString();
diff --git a/src/main/java/org/jboss/jdeparser/ArrayJType.java b/src/main/java/org/jboss/jdeparser/ArrayJType.java
index aa9187a..7521bc8 100644
--- a/src/main/java/org/jboss/jdeparser/ArrayJType.java
+++ b/src/main/java/org/jboss/jdeparser/ArrayJType.java
@@ -44,8 +44,16 @@ class ArrayJType extends AbstractJType {
sourceFileWriter.write($PUNCT.BRACKET.CLOSE);
}
- String qualifiedName(final SourceFileWriter writer) {
- return elementType.qualifiedName(writer);
+ String qualifiedName() {
+ return elementType.qualifiedName();
+ }
+
+ public int hashCode() {
+ return elementType.hashCode() ^ 0xee55ee55 + 7;
+ }
+
+ boolean equals(final AbstractJType other) {
+ return other instanceof ArrayJType && elementType.equals(((ArrayJType) other).elementType);
}
public JExpr _new(final JExpr dim) {
diff --git a/src/main/java/org/jboss/jdeparser/ArrayLookupJExpr.java b/src/main/java/org/jboss/jdeparser/ArrayLookupJExpr.java
index 72af2d0..8587d3e 100644
--- a/src/main/java/org/jboss/jdeparser/ArrayLookupJExpr.java
+++ b/src/main/java/org/jboss/jdeparser/ArrayLookupJExpr.java
@@ -32,7 +32,7 @@ class ArrayLookupJExpr extends AbstractJAssignableExpr {
ArrayLookupJExpr(final AbstractJExpr expr, final AbstractJExpr idx) {
super(Prec.ARRAY_ACCESS);
- this.expr = expr.prec() < Prec.ARRAY_ACCESS ? new ParenJExpr(expr) : expr;
+ this.expr = expr.prec() > Prec.ARRAY_ACCESS ? new ParenJExpr(expr) : expr;
this.idx = idx;
}
diff --git a/src/main/java/org/jboss/jdeparser/BasicJBlock.java b/src/main/java/org/jboss/jdeparser/BasicJBlock.java
index 27f7348..66ab9fe 100644
--- a/src/main/java/org/jboss/jdeparser/BasicJBlock.java
+++ b/src/main/java/org/jboss/jdeparser/BasicJBlock.java
@@ -200,7 +200,7 @@ class BasicJBlock extends BasicJCommentable implements JBlock, BlockContent {
}
public JCall call(final JExpr obj, final String name) {
- return add(new InstanceJCall(obj, name));
+ return add(new InstanceJCall(AbstractJExpr.of(obj), name));
}
public JCall callStatic(final ExecutableElement element) {
@@ -218,7 +218,7 @@ class BasicJBlock extends BasicJCommentable implements JBlock, BlockContent {
}
public JCall callStatic(final JType type, final String name) {
- return add(new StaticJCall(type, name));
+ return add(new StaticJCall(AbstractJType.of(type), name));
}
public JCall callStatic(final Class<?> type, final String name) {
diff --git a/src/main/java/org/jboss/jdeparser/CastJExpr.java b/src/main/java/org/jboss/jdeparser/CastJExpr.java
index 14ffc76..c7a82b1 100644
--- a/src/main/java/org/jboss/jdeparser/CastJExpr.java
+++ b/src/main/java/org/jboss/jdeparser/CastJExpr.java
@@ -32,7 +32,7 @@ class CastJExpr extends AbstractJExpr {
CastJExpr(final AbstractJExpr expr, final JType type) {
super(Prec.CAST);
- this.expr = expr.prec() < Prec.CAST ? new ParenJExpr(expr) : expr;
+ this.expr = expr.prec() > Prec.CAST ? new ParenJExpr(expr) : expr;
this.type = type;
}
diff --git a/src/main/java/org/jboss/jdeparser/CondJExpr.java b/src/main/java/org/jboss/jdeparser/CondJExpr.java
index b7a828e..357cdd3 100644
--- a/src/main/java/org/jboss/jdeparser/CondJExpr.java
+++ b/src/main/java/org/jboss/jdeparser/CondJExpr.java
@@ -33,9 +33,9 @@ class CondJExpr extends AbstractJExpr {
CondJExpr(final AbstractJExpr cond, final AbstractJExpr ifTrue, final AbstractJExpr ifFalse) {
super(Prec.COND);
- this.cond = cond.prec() < Prec.COND ? new ParenJExpr(cond) : cond;
- this.ifTrue = ifTrue.prec() < Prec.COND ? new ParenJExpr(ifTrue) : ifTrue;
- this.ifFalse = ifFalse.prec() < Prec.COND ? new ParenJExpr(ifFalse) : ifFalse;
+ this.cond = cond.prec() > Prec.COND ? new ParenJExpr(cond) : cond;
+ this.ifTrue = ifTrue.prec() > Prec.COND ? new ParenJExpr(ifTrue) : ifTrue;
+ this.ifFalse = ifFalse.prec() > Prec.COND ? new ParenJExpr(ifFalse) : ifFalse;
}
public void write(final SourceFileWriter writer) throws IOException {
diff --git a/src/main/java/org/jboss/jdeparser/FieldRefJExpr.java b/src/main/java/org/jboss/jdeparser/FieldRefJExpr.java
index 5a20732..d3b0102 100644
--- a/src/main/java/org/jboss/jdeparser/FieldRefJExpr.java
+++ b/src/main/java/org/jboss/jdeparser/FieldRefJExpr.java
@@ -32,7 +32,7 @@ class FieldRefJExpr extends AbstractJAssignableExpr {
FieldRefJExpr(final AbstractJExpr expr, final String refName) {
super(Prec.MEMBER_ACCESS);
- this.expr = expr;
+ this.expr = expr.prec() > prec() ? new ParenJExpr(expr) : expr;
this.refName = refName;
}
diff --git a/src/main/java/org/jboss/jdeparser/ForEachJBlock.java b/src/main/java/org/jboss/jdeparser/ForEachJBlock.java
index bf69688..1a3a022 100644
--- a/src/main/java/org/jboss/jdeparser/ForEachJBlock.java
+++ b/src/main/java/org/jboss/jdeparser/ForEachJBlock.java
@@ -48,6 +48,7 @@ class ForEachJBlock extends BasicJBlock {
writer.write(FormatPreferences.Space.WITHIN_PAREN_FOR);
JMod.write(writer, mods);
writer.write(type);
+ writer.sp();
writer.writeEscaped(name);
writer.write(FormatPreferences.Space.BEFORE_COLON);
writer.write($PUNCT.COLON);
diff --git a/src/main/java/org/jboss/jdeparser/FormatPreferences.java b/src/main/java/org/jboss/jdeparser/FormatPreferences.java
index 649a4d9..3c935af 100644
--- a/src/main/java/org/jboss/jdeparser/FormatPreferences.java
+++ b/src/main/java/org/jboss/jdeparser/FormatPreferences.java
@@ -531,6 +531,7 @@ public final class FormatPreferences {
*/
public enum Space {
// default for all parens
+ @Deprecated
BEFORE_PAREN,
// single-line statements
@@ -550,6 +551,7 @@ public final class FormatPreferences {
BEFORE_PAREN_CAST,
// default for all binary operators
+ @Deprecated
AROUND_OPERATORS,
// specific operator categories
diff --git a/src/main/java/org/jboss/jdeparser/ImplJSourceFile.java b/src/main/java/org/jboss/jdeparser/ImplJSourceFile.java
index 0d8c497..d17a214 100644
--- a/src/main/java/org/jboss/jdeparser/ImplJSourceFile.java
+++ b/src/main/java/org/jboss/jdeparser/ImplJSourceFile.java
@@ -31,7 +31,7 @@ import java.util.Map;
*/
class ImplJSourceFile extends BasicJCommentable implements JSourceFile {
private final ImplJSources sources;
- private final Map<String, ReferenceJType> imports = new HashMap<>();
+ private final Map<String, AbstractJType> imports = new HashMap<>();
private final Map<String, StaticRefJExpr> staticImports = new HashMap<>();
private final ArrayList<ClassFileContent> content = new ArrayList<>();
private final String packageName;
@@ -58,36 +58,32 @@ class ImplJSourceFile extends BasicJCommentable implements JSourceFile {
writer.writeEscaped(packageName);
writer.write($PUNCT.SEMI);
writer.nl();
- final Map<String, ReferenceJType> imports = ImplJSourceFile.this.imports;
- if (imports != null) {
- final Iterator<ReferenceJType> iterator = imports.values().iterator();
- if (iterator.hasNext()) {
+ final Map<String, AbstractJType> imports = ImplJSourceFile.this.imports;
+ final Iterator<AbstractJType> iterator = imports.values().iterator();
+ if (iterator.hasNext()) {
+ writer.nl();
+ do {
+ final AbstractJType _import = iterator.next();
+ writer.write($KW.IMPORT);
+ writer.writeClass(_import.qualifiedName());
+ writer.write($PUNCT.SEMI);
writer.nl();
- do {
- final ReferenceJType _import = iterator.next();
- writer.write($KW.IMPORT);
- writer.writeClass(_import.qualifiedName(writer));
- writer.write($PUNCT.SEMI);
- writer.nl();
- } while (iterator.hasNext());
- writer.nl();
- }
+ } while (iterator.hasNext());
+ writer.nl();
}
final Map<String, StaticRefJExpr> staticImports = ImplJSourceFile.this.staticImports;
- if (staticImports != null) {
- final Iterator<StaticRefJExpr> iterator = staticImports.values().iterator();
- if (iterator.hasNext()) {
- writer.nl();
- do {
- final StaticRefJExpr staticImport = iterator.next();
- writer.write($KW.IMPORT);
- writer.write($KW.STATIC);
- writer.write(staticImport);
- writer.write($PUNCT.SEMI);
- writer.nl();
- } while (iterator.hasNext());
+ final Iterator<StaticRefJExpr> staticIterator = staticImports.values().iterator();
+ if (staticIterator.hasNext()) {
+ writer.nl();
+ do {
+ final StaticRefJExpr staticImport = staticIterator.next();
+ writer.write($KW.IMPORT);
+ writer.write($KW.STATIC);
+ staticImport.writeForImport(writer);
+ writer.write($PUNCT.SEMI);
writer.nl();
- }
+ } while (staticIterator.hasNext());
+ writer.nl();
}
}
});
@@ -99,26 +95,17 @@ class ImplJSourceFile extends BasicJCommentable implements JSourceFile {
return imports.containsKey(name);
}
- boolean hasImport(final JType type, final SourceFileWriter writer) {
- if (type instanceof ReferenceJType) {
- ReferenceJType referenceJType = (ReferenceJType) type;
- final String name = referenceJType.simpleName();
- if (imports.containsKey(name) && imports.get(name).qualifiedName(writer).equals(referenceJType.qualifiedName(writer))) {
- return true;
- }
- }
- return false;
+ boolean hasImport(final AbstractJType type) {
+ return type.equals(imports.get(type.simpleName()));
}
boolean hasStaticImport(final String name) {
return staticImports.containsKey(name);
}
- boolean hasStaticImport(final JExpr expr, final SourceFileWriter writer) {
- if (! (expr instanceof StaticRefJExpr)) return false;
- final StaticRefJExpr staticRefJExpr = (StaticRefJExpr) expr;
- final String refName = staticRefJExpr.getRefName();
- return staticImports.containsKey(refName) && staticImports.get(refName).getType().qualifiedName(writer).equals(staticRefJExpr.getType().qualifiedName(writer));
+ boolean hasStaticImport(final String name, final AbstractJType enclosingType) {
+ final StaticRefJExpr expr = staticImports.get(name);
+ return expr != null && enclosingType.equals(expr.getType());
}
public JSourceFile _import(final String type) {
@@ -126,14 +113,16 @@ class ImplJSourceFile extends BasicJCommentable implements JSourceFile {
}
public JSourceFile _import(final JType type) {
- checkPackage();
- if (! (type instanceof ReferenceJType)) {
+ if (! (type instanceof ReferenceJType) && ! (type instanceof NestedJType)) {
+ // can't import this type
return this;
}
if (imports.containsKey(type.simpleName())) {
+ // already imported
return this;
}
- imports.put(type.simpleName(), (ReferenceJType) type);
+ checkPackage();
+ imports.put(type.simpleName(), (AbstractJType) type);
return this;
}
@@ -146,6 +135,14 @@ class ImplJSourceFile extends BasicJCommentable implements JSourceFile {
}
public JSourceFile importStatic(final JType type, final String member) {
+ if (! (type instanceof ReferenceJType) && ! (type instanceof NestedJType)) {
+ // no static members
+ return this;
+ }
+ if (staticImports.containsKey(member)) {
+ // already imported
+ return this;
+ }
checkPackage();
staticImports.put(member, new StaticRefJExpr(AbstractJType.of(type), member));
return this;
diff --git a/src/main/java/org/jboss/jdeparser/InnerJAnonymousClassDef.java b/src/main/java/org/jboss/jdeparser/InnerJAnonymousClassDef.java
index b3c3744..66c80a2 100644
--- a/src/main/java/org/jboss/jdeparser/InnerJAnonymousClassDef.java
+++ b/src/main/java/org/jboss/jdeparser/InnerJAnonymousClassDef.java
@@ -28,7 +28,7 @@ class InnerJAnonymousClassDef extends ImplJAnonymousClassDef {
InnerJAnonymousClassDef(final AbstractJExpr expr, final JType type) {
super(type);
- this.expr = expr;
+ this.expr = expr.prec() > prec() ? new ParenJExpr(expr) : expr;
}
public void write(final SourceFileWriter writer) throws IOException {
diff --git a/src/main/java/org/jboss/jdeparser/InnerNewJCall.java b/src/main/java/org/jboss/jdeparser/InnerNewJCall.java
index 6cc11b9..bd45521 100644
--- a/src/main/java/org/jboss/jdeparser/InnerNewJCall.java
+++ b/src/main/java/org/jboss/jdeparser/InnerNewJCall.java
@@ -31,7 +31,7 @@ class InnerNewJCall extends NewJCall {
InnerNewJCall(final AbstractJExpr target, final JType type) {
super(AbstractJType.of(type));
- this.target = target;
+ this.target = target.prec() > prec() ? new ParenJExpr(target) : target;
}
public void write(final SourceFileWriter writer) throws IOException {
diff --git a/src/main/java/org/jboss/jdeparser/InstanceJCall.java b/src/main/java/org/jboss/jdeparser/InstanceJCall.java
index 1b49813..69ee9a5 100644
--- a/src/main/java/org/jboss/jdeparser/InstanceJCall.java
+++ b/src/main/java/org/jboss/jdeparser/InstanceJCall.java
@@ -29,9 +29,9 @@ class InstanceJCall extends AbstractMethodJCall {
private final JExpr target;
- InstanceJCall(final JExpr target, final String name) {
+ InstanceJCall(final AbstractJExpr target, final String name) {
super(name);
- this.target = target;
+ this.target = target.prec() > prec() ? new ParenJExpr(target) : target;
}
JExpr getTarget() {
diff --git a/src/main/java/org/jboss/jdeparser/JClassItem.java b/src/main/java/org/jboss/jdeparser/JClassItem.java
index 3a63c26..3a228b2 100644
--- a/src/main/java/org/jboss/jdeparser/JClassItem.java
+++ b/src/main/java/org/jboss/jdeparser/JClassItem.java
@@ -28,6 +28,7 @@ import java.util.EnumSet;
*
* @author <a href="mailto:david.lloyd at redhat.com">David M. Lloyd</a>
*/
+ at Deprecated
public interface JClassItem {
/**
@@ -35,6 +36,7 @@ public interface JClassItem {
*
* @return the item kind
*/
+ @Deprecated
Kind getItemKind();
/**
@@ -43,6 +45,7 @@ public interface JClassItem {
* @return the modifiers
* @see JMod
*/
+ @Deprecated
int getModifiers();
/**
@@ -51,6 +54,7 @@ public interface JClassItem {
* @param mods the modifier(s)
* @return {@code true} if all of the modifiers are present, {@code false} otherwise
*/
+ @Deprecated
boolean hasAllModifiers(int mods);
/**
@@ -59,6 +63,7 @@ public interface JClassItem {
* @param mods the modifier(s)
* @return {@code true} if any if the modifiers are present, {@code false} otherwise
*/
+ @Deprecated
boolean hasAnyModifier(int mods);
/**
@@ -66,8 +71,10 @@ public interface JClassItem {
*
* @return the name, or {@code null} if there is none
*/
+ @Deprecated
String getName();
+ @Deprecated
Comparator<JClassItem> SMART_NAME_SORT = new Comparator<JClassItem>() {
private int rankOf(JClassItem item) {
return item.getItemKind().ordinal();
@@ -138,6 +145,7 @@ public interface JClassItem {
/**
* The kind of class item.
*/
+ @Deprecated
enum Kind {
/**
* A line comment. The item will implement {@link JComment}.
diff --git a/src/main/java/org/jboss/jdeparser/JExprs.java b/src/main/java/org/jboss/jdeparser/JExprs.java
index e0b8726..0a06b97 100644
--- a/src/main/java/org/jboss/jdeparser/JExprs.java
+++ b/src/main/java/org/jboss/jdeparser/JExprs.java
@@ -193,7 +193,7 @@ public final class JExprs {
* @return the method call
*/
public static JCall callStatic(final JType type, final String name) {
- return new StaticJCall(type, name);
+ return new StaticJCall(AbstractJType.of(type), name);
}
/**
diff --git a/src/main/java/org/jboss/jdeparser/JLambdaImpl.java b/src/main/java/org/jboss/jdeparser/JLambdaImpl.java
index b129f50..c756f4e 100644
--- a/src/main/java/org/jboss/jdeparser/JLambdaImpl.java
+++ b/src/main/java/org/jboss/jdeparser/JLambdaImpl.java
@@ -75,11 +75,16 @@ class JLambdaImpl extends AbstractJExpr implements JLambda {
}
public void write(final SourceFileWriter writer) throws IOException {
- writer.write(Tokens.$PUNCT.PAREN.OPEN);
if (params == null) {
+ writer.write(Tokens.$PUNCT.PAREN.OPEN);
writer.write(FormatPreferences.Space.WITHIN_PAREN_METHOD_CALL_EMPTY);
+ writer.write(Tokens.$PUNCT.PAREN.CLOSE);
} else {
- writer.write(FormatPreferences.Space.WITHIN_PAREN_METHOD_CALL);
+ final boolean singleParam = params.size() == 1 && hasInferred;
+ if (! singleParam) {
+ writer.write(Tokens.$PUNCT.PAREN.OPEN);
+ writer.write(FormatPreferences.Space.WITHIN_PAREN_METHOD_CALL);
+ }
final Iterator<Param> iterator = params.iterator();
while (iterator.hasNext()) {
final Param param = iterator.next();
@@ -93,9 +98,11 @@ class JLambdaImpl extends AbstractJExpr implements JLambda {
writer.write(FormatPreferences.Space.AFTER_COMMA);
}
}
- writer.write(FormatPreferences.Space.WITHIN_PAREN_METHOD_CALL);
+ if (! singleParam) {
+ writer.write(FormatPreferences.Space.WITHIN_PAREN_METHOD_CALL);
+ writer.write(Tokens.$PUNCT.PAREN.CLOSE);
+ }
}
- writer.write(Tokens.$PUNCT.PAREN.CLOSE);
writer.write(FormatPreferences.Space.AROUND_ARROW);
writer.write(Tokens.$PUNCT.BINOP.ARROW);
writer.write(FormatPreferences.Space.AROUND_ARROW);
diff --git a/src/main/java/org/jboss/jdeparser/NarrowedJType.java b/src/main/java/org/jboss/jdeparser/NarrowedJType.java
index 2ca578e..cddb343 100644
--- a/src/main/java/org/jboss/jdeparser/NarrowedJType.java
+++ b/src/main/java/org/jboss/jdeparser/NarrowedJType.java
@@ -36,6 +36,18 @@ class NarrowedJType extends AbstractJType {
this.args = args;
}
+ boolean equals(final AbstractJType other) {
+ return other instanceof NarrowedJType && equals((NarrowedJType) other);
+ }
+
+ private boolean equals(final NarrowedJType other) {
+ return erased.equals(other.erased) && Arrays.equals(args, other.args);
+ }
+
+ public int hashCode() {
+ return erased.hashCode() * 17 + Arrays.hashCode(args);
+ }
+
public String simpleName() {
return erased.simpleName();
}
diff --git a/src/main/java/org/jboss/jdeparser/NestedJType.java b/src/main/java/org/jboss/jdeparser/NestedJType.java
index f178a38..9078162 100644
--- a/src/main/java/org/jboss/jdeparser/NestedJType.java
+++ b/src/main/java/org/jboss/jdeparser/NestedJType.java
@@ -39,6 +39,10 @@ class NestedJType extends AbstractJType {
this.name = name;
}
+ String qualifiedName() {
+ return enclosingType.qualifiedName() + "." + name;
+ }
+
public JExpr _class() {
StaticRefJExpr expr = classExpr;
if (expr == null) {
@@ -71,6 +75,18 @@ class NestedJType extends AbstractJType {
return new ImplJAnonymousClassDef(this);
}
+ boolean equals(final AbstractJType other) {
+ return other instanceof NestedJType && equals((NestedJType) other);
+ }
+
+ private boolean equals(final NestedJType other) {
+ return enclosingType.equals(other.enclosingType) && name.equals(other.name);
+ }
+
+ public int hashCode() {
+ return enclosingType.hashCode() * 17 + name.hashCode();
+ }
+
public String simpleName() {
return name;
}
@@ -96,8 +112,10 @@ class NestedJType extends AbstractJType {
}
void writeDirect(final SourceFileWriter writer) throws IOException {
- enclosingType.writeDirect(writer);
- writer.write($PUNCT.DOT);
+ if (! writer.getClassFile().hasStaticImport(name, enclosingType) && ! writer.getClassFile().hasImport(this)) {
+ enclosingType.writeDirect(writer);
+ writer.write($PUNCT.DOT);
+ }
writer.writeClass(name);
}
}
diff --git a/src/main/java/org/jboss/jdeparser/PrimitiveJType.java b/src/main/java/org/jboss/jdeparser/PrimitiveJType.java
index 124da7a..f2edf1d 100644
--- a/src/main/java/org/jboss/jdeparser/PrimitiveJType.java
+++ b/src/main/java/org/jboss/jdeparser/PrimitiveJType.java
@@ -62,6 +62,14 @@ class PrimitiveJType extends AbstractJType {
return expr;
}
+ boolean equals(final AbstractJType other) {
+ return other instanceof PrimitiveJType && simpleName.equals(((PrimitiveJType) other).simpleName);
+ }
+
+ public int hashCode() {
+ return simpleName.hashCode();
+ }
+
public String simpleName() {
return simpleName;
}
diff --git a/src/main/java/org/jboss/jdeparser/ReferenceJType.java b/src/main/java/org/jboss/jdeparser/ReferenceJType.java
index 3d60e58..afe0d38 100644
--- a/src/main/java/org/jboss/jdeparser/ReferenceJType.java
+++ b/src/main/java/org/jboss/jdeparser/ReferenceJType.java
@@ -54,7 +54,7 @@ class ReferenceJType extends AbstractJType {
}
@Override
- String qualifiedName(final SourceFileWriter writer) {
+ String qualifiedName() {
if (packageName.isEmpty()) {
return simpleName;
}
@@ -107,7 +107,7 @@ class ReferenceJType extends AbstractJType {
final boolean packageMatches = currentPackageName.equals(packageName);
if (packageMatches && cf.hasImport(simpleName())) {
// an explicit import masks the implicit import
- sourceFileWriter.writeClass(qualifiedName(sourceFileWriter));
+ sourceFileWriter.writeClass(qualifiedName());
} else if (packageName.equals("java.lang") && ! sourceFileWriter.getClassFile().getSources().hasClass(currentPackageName + "." + simpleName()) || packageMatches) {
// implicit import
sourceFileWriter.writeClass(simpleName());
@@ -115,7 +115,7 @@ class ReferenceJType extends AbstractJType {
// explicit import
sourceFileWriter.writeClass(simpleName());
} else {
- sourceFileWriter.writeClass(qualifiedName(sourceFileWriter));
+ sourceFileWriter.writeClass(qualifiedName());
}
}
@@ -136,6 +136,18 @@ class ReferenceJType extends AbstractJType {
return nestedType;
}
+ boolean equals(final AbstractJType other) {
+ return other instanceof ReferenceJType && equals((ReferenceJType) other);
+ }
+
+ private boolean equals(final ReferenceJType other) {
+ return packageName.equals(other.packageName) && simpleName.equals(other.simpleName);
+ }
+
+ public int hashCode() {
+ return packageName.hashCode() * 17 + simpleName.hashCode();
+ }
+
public String toString() {
return "Reference of type " + simpleName();
}
diff --git a/src/main/java/org/jboss/jdeparser/SourceFileWriter.java b/src/main/java/org/jboss/jdeparser/SourceFileWriter.java
index 9b46b95..5cf9f31 100644
--- a/src/main/java/org/jboss/jdeparser/SourceFileWriter.java
+++ b/src/main/java/org/jboss/jdeparser/SourceFileWriter.java
@@ -83,7 +83,8 @@ class SourceFileWriter implements Flushable, Closeable {
private static final int SS_NONE = 0;
private static final int SS_NEEDED = 1;
private static final int SS_ADDED = 2;
- private static final int SS_NEEDS_INDENT = 3;
+ private static final int SS_NEW_LINE = 3;
+ private static final int SS_2_NEW_LINE = 4;
SourceFileWriter(final FormatPreferences format, final Writer writer) {
this.format = format;
@@ -96,7 +97,7 @@ class SourceFileWriter implements Flushable, Closeable {
countingWriter.write(lineBuffer);
countingWriter.write(lineSep);
lineBuffer.setLength(0);
- spaceState = SS_NEEDS_INDENT;
+ spaceState = spaceState == SS_NEW_LINE ? SS_2_NEW_LINE : SS_NEW_LINE;
}
/**
@@ -105,7 +106,7 @@ class SourceFileWriter implements Flushable, Closeable {
* @throws IOException etc.
*/
void sp() throws IOException {
- if (spaceState == SS_NEEDS_INDENT) {
+ if (spaceState == SS_NEW_LINE || spaceState == SS_2_NEW_LINE) {
addIndent();
} else if (spaceState != SS_ADDED) {
spaceState = SS_ADDED;
@@ -130,7 +131,8 @@ class SourceFileWriter implements Flushable, Closeable {
void processSpacing() throws IOException {
switch (spaceState) {
- case SS_NEEDS_INDENT: {
+ case SS_2_NEW_LINE:
+ case SS_NEW_LINE: {
nextIndent.addIndent(nextIndent, format, lineBuffer);
spaceState = SS_ADDED;
break;
@@ -143,7 +145,7 @@ class SourceFileWriter implements Flushable, Closeable {
}
void addIndent() throws IOException {
- assert spaceState == SS_NEEDS_INDENT; // it was a new line
+ assert spaceState == SS_NEW_LINE || spaceState == SS_2_NEW_LINE; // it was a new line
nextIndent.addIndent(nextIndent, format, lineBuffer);
spaceState = SS_ADDED;
}
@@ -185,8 +187,8 @@ class SourceFileWriter implements Flushable, Closeable {
return;
}
if (format.getSpaceType(rule) == FormatPreferences.SpaceType.NEWLINE) {
- if (spaceState != SS_NEEDS_INDENT) {
- // must not be directly after a newline
+ if (spaceState != SS_2_NEW_LINE) {
+ // must not be directly after a 2-newline
nl();
}
} else {
diff --git a/src/main/java/org/jboss/jdeparser/SourceWriter.java b/src/main/java/org/jboss/jdeparser/SourceWriter.java
deleted file mode 100644
index 97a20a8..0000000
--- a/src/main/java/org/jboss/jdeparser/SourceWriter.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2014 Red Hat, Inc., and individual contributors
- * as indicated by the @author tags.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.jboss.jdeparser;
-
-import java.io.IOException;
-
-/**
- * @author <a href="mailto:david.lloyd at redhat.com">David M. Lloyd</a>
- */
-abstract class SourceWriter {
-
- abstract void writeRaw(int ch) throws IOException;
-}
diff --git a/src/main/java/org/jboss/jdeparser/StaticJCall.java b/src/main/java/org/jboss/jdeparser/StaticJCall.java
index 65aa93a..5b269eb 100644
--- a/src/main/java/org/jboss/jdeparser/StaticJCall.java
+++ b/src/main/java/org/jboss/jdeparser/StaticJCall.java
@@ -27,20 +27,22 @@ import java.io.IOException;
*/
class StaticJCall extends AbstractMethodJCall {
- private final JType type;
+ private final AbstractJType type;
- StaticJCall(final JType type, final String name) {
+ StaticJCall(final AbstractJType type, final String name) {
super(name);
this.type = type;
}
- JType getType() {
+ AbstractJType getType() {
return type;
}
public void write(final SourceFileWriter writer) throws IOException {
- writer.write(AbstractJType.of(type));
- writer.write($PUNCT.DOT);
+ if (! writer.getClassFile().hasStaticImport(getName(), type)) {
+ type.writeDirect(writer);
+ writer.write($PUNCT.DOT);
+ }
super.write(writer);
}
}
diff --git a/src/main/java/org/jboss/jdeparser/StaticRefJExpr.java b/src/main/java/org/jboss/jdeparser/StaticRefJExpr.java
index 2c7ac06..b5b9b34 100644
--- a/src/main/java/org/jboss/jdeparser/StaticRefJExpr.java
+++ b/src/main/java/org/jboss/jdeparser/StaticRefJExpr.java
@@ -51,7 +51,16 @@ class StaticRefJExpr extends AbstractJAssignableExpr {
}
public void write(final SourceFileWriter writer) throws IOException {
- writer.write(type);
+ if (! writer.getClassFile().hasStaticImport(refName, type)) {
+ type.writeDirect(writer);
+ writer.write($PUNCT.DOT);
+ }
+ writer.writeEscaped(refName);
+ }
+
+ void writeForImport(final SourceFileWriter writer) throws IOException {
+ writer.processSpacing();
+ writer.writeClass(type.qualifiedName());
writer.write($PUNCT.DOT);
writer.writeEscaped(refName);
}
diff --git a/src/main/java/org/jboss/jdeparser/ThisJType.java b/src/main/java/org/jboss/jdeparser/ThisJType.java
index 549df0a..21c9a81 100644
--- a/src/main/java/org/jboss/jdeparser/ThisJType.java
+++ b/src/main/java/org/jboss/jdeparser/ThisJType.java
@@ -28,6 +28,14 @@ class ThisJType extends AbstractJType {
ThisJType() {
}
+ boolean equals(final AbstractJType other) {
+ return other instanceof ThisJType;
+ }
+
+ public int hashCode() {
+ return 23;
+ }
+
public String simpleName() {
return "<<THIS>>";
}
diff --git a/src/main/java/org/jboss/jdeparser/Tokens.java b/src/main/java/org/jboss/jdeparser/Tokens.java
index f27e19d..59ca7b3 100644
--- a/src/main/java/org/jboss/jdeparser/Tokens.java
+++ b/src/main/java/org/jboss/jdeparser/Tokens.java
@@ -143,7 +143,7 @@ enum Tokens implements Token {
ASSIGN_LSHR(">>>=", Space.AROUND_ASSIGN),
ASSIGN_SHL("<<=", Space.AROUND_ASSIGN),
- DBL_COLON("::", null),
+ DBL_COLON("::", Space.AROUND_METHOD_REF),
ARROW("->", Space.AROUND_ARROW),
;
diff --git a/src/main/java/org/jboss/jdeparser/UnaryJExpr.java b/src/main/java/org/jboss/jdeparser/UnaryJExpr.java
index 3754cbf..83e1e41 100644
--- a/src/main/java/org/jboss/jdeparser/UnaryJExpr.java
+++ b/src/main/java/org/jboss/jdeparser/UnaryJExpr.java
@@ -57,9 +57,11 @@ class UnaryJExpr extends AbstractJExpr {
public void write(final SourceFileWriter writer) throws IOException {
if (isPostfix()) {
writer.write(expr);
+ writer.write(FormatPreferences.Space.AT_UNARY);
writer.write(op);
} else {
writer.write(op);
+ writer.write(FormatPreferences.Space.AT_UNARY);
writer.write(expr);
}
}
diff --git a/src/main/java/org/jboss/jdeparser/WildcardJType.java b/src/main/java/org/jboss/jdeparser/WildcardJType.java
index df8656c..c521db5 100644
--- a/src/main/java/org/jboss/jdeparser/WildcardJType.java
+++ b/src/main/java/org/jboss/jdeparser/WildcardJType.java
@@ -35,6 +35,18 @@ class WildcardJType extends AbstractJType {
this.extendsNotSuper = extendsNotSuper;
}
+ boolean equals(final AbstractJType other) {
+ return other instanceof WildcardJType && equals((WildcardJType) other);
+ }
+
+ private boolean equals(final WildcardJType other) {
+ return extendsNotSuper == other.extendsNotSuper && targetType.equals(other.targetType);
+ }
+
+ public int hashCode() {
+ return targetType.hashCode() ^ (extendsNotSuper ? 0 : 1);
+ }
+
public String simpleName() {
return targetType.simpleName();
}
@@ -55,13 +67,20 @@ class WildcardJType extends AbstractJType {
void writeDirect(final SourceFileWriter sourceFileWriter) throws IOException {
sourceFileWriter.write($PUNCT.Q);
- sourceFileWriter.sp();
- sourceFileWriter.write(extendsNotSuper ? $KW.EXTENDS : $KW.SUPER);
- sourceFileWriter.write(getTargetType());
+ final AbstractJType targetType = getTargetType();
+ if (! targetType.equals(JType.OBJECT)) {
+ sourceFileWriter.sp();
+ sourceFileWriter.write(extendsNotSuper ? $KW.EXTENDS : $KW.SUPER);
+ sourceFileWriter.write(targetType);
+ }
}
public String toString() {
- return "? " + (extendsNotSuper ? "extends " : "super ") + targetType;
+ if (extendsNotSuper && targetType.equals(JType.OBJECT)) {
+ return "?";
+ } else {
+ return "? " + (extendsNotSuper ? "extends " : "super ") + targetType;
+ }
}
AbstractJType getTargetType() {
diff --git a/src/test/java/org/jboss/jdeparser/SimpleExampleTestCase.java b/src/test/java/org/jboss/jdeparser/SimpleExampleTestCase.java
index 644f5a3..06644db 100644
--- a/src/test/java/org/jboss/jdeparser/SimpleExampleTestCase.java
+++ b/src/test/java/org/jboss/jdeparser/SimpleExampleTestCase.java
@@ -63,6 +63,10 @@ public class SimpleExampleTestCase extends AbstractGeneratingTestCase {
jIf.assign(JExprs.$v(t), str("new Value"));
jIf._else().assign(JExprs.$v(t), str("other value"));
+ // static import and reference
+ body.call(JTypes.$t(Thread.State.class).$v("NEW"), "toString");
+ bazFile.importStatic(Thread.State.class, "NEW");
+
// Reference an enclosing class
body.var(0, JTypes.$t(Map.Entry.class).typeArg(String.class, Object.class), "mapEntry", JExpr.NULL);
bazFile._import(Map.class);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jboss-jdeparser2.git
More information about the pkg-java-commits
mailing list