Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less warnings, less castings, no unnecessary blocks #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.xml.sax.ErrorHandler;

import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
//import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JConditional;
import com.sun.codemodel.JDefinedClass;
Expand Down Expand Up @@ -154,11 +154,9 @@ protected void processClassOutline(ClassOutline classOutline) {

final JMethod newMethod = theClass.method(JMod.PUBLIC, theClass
.owner().ref(Object.class), "createNewInstance");
newMethod.annotate(Override.class);
{
final JBlock body = newMethod.body();
body._return(JExpr._new(theClass));
}
newMethod.annotate(Override.class); // DEMATIC
final JBlock body = newMethod.body();
body._return(JExpr._new(theClass));
return newMethod;
} else {
return existingMethod;
Expand All @@ -167,15 +165,12 @@ protected void processClassOutline(ClassOutline classOutline) {

protected JMethod generateObject$clone(final ClassOutline classOutline,
final JDefinedClass theClass) {

final JMethod clone = theClass.method(JMod.PUBLIC, theClass.owner()
.ref(Object.class), "clone");
clone.annotate(Override.class);
{
final JBlock body = clone.body();
body._return(JExpr.invoke("copyTo").arg(
JExpr.invoke("createNewInstance")));
}
clone.annotate(Override.class); // DEMATIC
final JBlock body = clone.body();
body._return(JExpr.invoke("copyTo").arg(
JExpr.invoke("createNewInstance")));
return clone;
}

Expand All @@ -185,18 +180,17 @@ protected void processClassOutline(ClassOutline classOutline) {
final JCodeModel codeModel = theClass.owner();
final JMethod copyTo$copyTo = theClass.method(JMod.PUBLIC,
codeModel.ref(Object.class), "copyTo");
copyTo$copyTo.annotate(Override.class);
{
final JVar target = copyTo$copyTo.param(Object.class, "target");
copyTo$copyTo.annotate(Override.class); // DEMATIC
final JVar target = copyTo$copyTo.param(Object.class, "target");

final JBlock body = copyTo$copyTo.body();
final JVar copyStrategy = body.decl(JMod.FINAL,
codeModel.ref(CopyStrategy2.class), "strategy",
createCopyStrategy(codeModel));
final JBlock body = copyTo$copyTo.body();
final JVar copyStrategy = body.decl(JMod.FINAL,
codeModel.ref(CopyStrategy2.class), "strategy2",
createCopyStrategy(codeModel));

body._return(JExpr.invoke("copyTo").arg(JExpr._null()).arg(target)
.arg(copyStrategy));
}
body._return(JExpr.invoke("copyTo").arg(JExpr._null()).arg(target)
.arg(copyStrategy));

return copyTo$copyTo;
}

Expand All @@ -207,137 +201,143 @@ protected void processClassOutline(ClassOutline classOutline) {

final JMethod copyTo = theClass.method(JMod.PUBLIC,
codeModel.ref(Object.class), "copyTo");
copyTo.annotate(Override.class);
{
final JVar locator = copyTo.param(ObjectLocator.class, "locator");
final JVar target = copyTo.param(Object.class, "target");
final JVar copyStrategy = copyTo.param(CopyStrategy2.class,
"strategy");

final JBlock body = copyTo.body();

final JVar draftCopy;
if (!classOutline.target.isAbstract()) {
draftCopy = body.decl(
JMod.FINAL,
codeModel.ref(Object.class),
"draftCopy",

JOp.cond(JOp.eq(target, JExpr._null()),
JExpr.invoke("createNewInstance"), target));
} else {
body._if(JExpr._null().eq(target))
._then()
._throw(JExpr
._new(codeModel
.ref(IllegalArgumentException.class))
.arg("Target argument must not be null for abstract copyable classes."));
draftCopy = target;
}
copyTo.annotate(Override.class); // DEMATIC
final JVar locator = copyTo.param(ObjectLocator.class, "locator");
final JVar target = copyTo.param(Object.class, "target");
final JVar copyStrategy = copyTo.param(CopyStrategy2.class,
"strategy2");

final JBlock body = copyTo.body();

final JVar draftCopy;
if (!classOutline.target.isAbstract()) {
draftCopy = body.decl(
JMod.FINAL,
codeModel.ref(Object.class),
"draftCopy",

Boolean superClassImplementsCopyTo = StrategyClassUtils
.superClassImplements(classOutline, getIgnoring(),
CopyTo2.class);
JOp.cond(JOp.eq(target, JExpr._null()),
JExpr.invoke("createNewInstance"), target));
} else {
body._if(JExpr._null().eq(target))
._then()
._throw(JExpr
._new(codeModel
.ref(IllegalArgumentException.class))
.arg("Target argument must not be null for abstract copyable classes."));
draftCopy = target;
}

if (superClassImplementsCopyTo == null) {
Boolean superClassImplementsCopyTo = StrategyClassUtils
.superClassImplements(classOutline, getIgnoring(),
CopyTo2.class);

} else if (superClassImplementsCopyTo.booleanValue()) {
body.invoke(JExpr._super(), "copyTo").arg(locator)
.arg(draftCopy).arg(copyStrategy);
if (superClassImplementsCopyTo == null) {

} else {
} else if (superClassImplementsCopyTo.booleanValue()) {
body.invoke(JExpr._super(), "copyTo").arg(locator)
.arg(draftCopy).arg(copyStrategy);

}
} else {

final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
classOutline.getDeclaredFields(), getIgnoring());
}

if (declaredFields.length > 0) {
final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
classOutline.getDeclaredFields(), getIgnoring());

final JBlock bl = body._if(draftCopy._instanceof(theClass))
._then();
if (declaredFields.length > 0) {

final JBlock bl = body._if(draftCopy._instanceof(theClass))
._then();

final JVar copy = bl.decl(JMod.FINAL, theClass, "copy",
JExpr.cast(theClass, draftCopy));

final JVar copy = bl.decl(JMod.FINAL, theClass, "copy",
JExpr.cast(theClass, draftCopy));

for (final FieldOutline fieldOutline : declaredFields) {

final FieldAccessorEx sourceFieldAccessor = getFieldAccessorFactory()
.createFieldAccessor(fieldOutline, JExpr._this());
final FieldAccessorEx copyFieldAccessor = getFieldAccessorFactory()
.createFieldAccessor(fieldOutline, copy);

if (sourceFieldAccessor.isConstant()) {
continue;
}

final JBlock block = bl.block();

final JExpression valueIsSet = (sourceFieldAccessor
.isAlwaysSet() || sourceFieldAccessor.hasSetValue() == null) ? JExpr.TRUE
: sourceFieldAccessor.hasSetValue();

final JVar shouldBeCopied = block.decl(codeModel
.ref(Boolean.class), fieldOutline.getPropertyInfo()
.getName(false) + "ShouldBeCopiedAndSet",
copyStrategy.invoke("shouldBeCopiedAndSet").arg(locator)
.arg(valueIsSet));

final JConditional ifShouldBeSetConditional = block._if(JOp
.eq(shouldBeCopied, codeModel.ref(Boolean.class)
.staticRef("TRUE")));

final JBlock ifShouldBeSetBlock = ifShouldBeSetConditional
._then();
final JConditional ifShouldNotBeSetConditional = ifShouldBeSetConditional
._elseif(JOp.eq(
shouldBeCopied,
codeModel.ref(Boolean.class).staticRef(
"FALSE")));
final JBlock ifShouldBeUnsetBlock = ifShouldNotBeSetConditional
._then();

final JType copyFieldType = sourceFieldAccessor.getType();
final JVar sourceField = ifShouldBeSetBlock.decl(
copyFieldType,
"source"
+ fieldOutline.getPropertyInfo().getName(
true));
sourceFieldAccessor.toRawValue(ifShouldBeSetBlock,
sourceField);
final JExpression builtCopy = JExpr
.invoke(copyStrategy, "copy")
.arg(codeModel
.ref(LocatorUtils.class)
.staticInvoke("property")
.arg(locator)
.arg(fieldOutline.getPropertyInfo()
.getName(false)).arg(sourceField))
.arg(sourceField).arg(valueIsSet);
final JVar copyField = ifShouldBeSetBlock.decl(
copyFieldType,
"copy"
+ fieldOutline.getPropertyInfo().getName(
true),
copyFieldType.isPrimitive() ? builtCopy :

JExpr.cast(copyFieldType, builtCopy));
if (copyFieldType instanceof JClass
&& ((JClass) copyFieldType).isParameterized()) {
copyField.annotate(SuppressWarnings.class).param(
"value", "unchecked");
}
copyFieldAccessor.fromRawValue(ifShouldBeSetBlock, "unique"
+ fieldOutline.getPropertyInfo().getName(true),
copyField);

copyFieldAccessor.unsetValues(ifShouldBeUnsetBlock);
for (final FieldOutline fieldOutline : declaredFields) {

final FieldAccessorEx sourceFieldAccessor = getFieldAccessorFactory()
.createFieldAccessor(fieldOutline, JExpr._this());
final FieldAccessorEx copyFieldAccessor = getFieldAccessorFactory()
.createFieldAccessor(fieldOutline, copy);

if (sourceFieldAccessor.isConstant()) {
continue;
}
}

body._return(draftCopy);
final JBlock block = bl; // DEMATIC

final JExpression valueIsSet = (sourceFieldAccessor
.isAlwaysSet() || sourceFieldAccessor.hasSetValue() == null) ? JExpr.TRUE
: sourceFieldAccessor.hasSetValue();

final JVar shouldBeCopied = block.decl(codeModel
.ref(Boolean.class), fieldOutline.getPropertyInfo()
.getName(false) + "ShouldBeCopiedAndSet",
copyStrategy.invoke("shouldBeCopiedAndSet").arg(locator)
.arg(valueIsSet));

final JConditional ifShouldBeSetConditional = block._if(JOp
.eq(shouldBeCopied, codeModel.ref(Boolean.class)
.staticRef("TRUE")));

final JBlock ifShouldBeSetBlock = ifShouldBeSetConditional
._then();
final JConditional ifShouldNotBeSetConditional = ifShouldBeSetConditional
._elseif(JOp.eq(
shouldBeCopied,
codeModel.ref(Boolean.class).staticRef(
"FALSE")));
final JBlock ifShouldBeUnsetBlock = ifShouldNotBeSetConditional
._then();

final JType copyFieldType = sourceFieldAccessor.getType();

//final JVar sourceField = ifShouldBeSetBlock.decl( copyFieldType, "source" + fieldOutline.getPropertyInfo().getName(true));
//sourceFieldAccessor.toRawValue(ifShouldBeSetBlock, sourceField);

String propertyName = fieldOutline.getPropertyInfo().getName(true);// DEMATIC
final JVar sourceField = ifShouldBeSetBlock.decl(copyFieldType, "source" + propertyName, JExpr._this().invoke(createMethodName(fieldOutline))); // DEMATIC

final JExpression builtCopy = JExpr
.invoke(copyStrategy, "copy")
.arg(codeModel
.ref(LocatorUtils.class)
.staticInvoke("property")
.arg(locator)
.arg(fieldOutline.getPropertyInfo()
.getName(false)).arg(sourceField))
.arg(sourceField).arg(valueIsSet);
final JVar copyField = ifShouldBeSetBlock.decl(
copyFieldType,
"copy"
+ fieldOutline.getPropertyInfo().getName(
true),
copyFieldType.isPrimitive() ? builtCopy :

JExpr.cast(copyFieldType, builtCopy));
// if (copyFieldType instanceof JClass
// && ((JClass) copyFieldType).isParameterized()) {
// copyField.annotate(SuppressWarnings.class).param(
// "value", "unchecked");
// }
copyFieldAccessor.fromRawValue(ifShouldBeSetBlock, "unique"
+ fieldOutline.getPropertyInfo().getName(true),
copyField);

copyFieldAccessor.unsetValues(ifShouldBeUnsetBlock);
}
}

body._return(draftCopy);
return copyTo;
}

}

private String createMethodName(FieldOutline fieldOutline) {
String name = fieldOutline.getPropertyInfo().getName(true);
String type = fieldOutline.getRawType().name();
if (type.toLowerCase().equals("boolean")) {
return "is" + name;
}
return "get" + name;
}
}
Loading