forked from highsource/jaxb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for JAXBElement.
- Loading branch information
ja6a
authored and
ja6a
committed
Nov 28, 2014
1 parent
3d7a736
commit 6d69735
Showing
6 changed files
with
107 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
/err | ||
/std | ||
|
||
plugins/.settings/org.eclipse.core.resources.prefs | ||
|
||
plugins/.settings/org.eclipse.jdt.core.prefs | ||
|
||
tests/simple-hashCode-equals-01/.project | ||
|
||
tests/simple-hashCode-equals-01/.classpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...jvnet/jaxb2_commons/plugin/simplehashcode/generator/JAXBElementHashCodeCodeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.jvnet.jaxb2_commons.plugin.simplehashcode.generator; | ||
|
||
import javax.xml.namespace.QName; | ||
|
||
import org.apache.commons.lang3.Validate; | ||
import org.jvnet.jaxb2_commons.codemodel.generator.TypedCodeGeneratorFactory; | ||
|
||
import com.sun.codemodel.JBlock; | ||
import com.sun.codemodel.JClass; | ||
import com.sun.codemodel.JCodeModel; | ||
import com.sun.codemodel.JConditional; | ||
import com.sun.codemodel.JExpr; | ||
import com.sun.codemodel.JMod; | ||
import com.sun.codemodel.JOp; | ||
import com.sun.codemodel.JType; | ||
import com.sun.codemodel.JVar; | ||
|
||
public class JAXBElementHashCodeCodeGenerator implements HashCodeCodeGenerator { | ||
|
||
private final JCodeModel codeModel; | ||
private final TypedCodeGeneratorFactory<HashCodeCodeGenerator> codeGeneratorFactory; | ||
|
||
public JAXBElementHashCodeCodeGenerator(JCodeModel codeModel, | ||
TypedCodeGeneratorFactory<HashCodeCodeGenerator> codeGeneratorFactory) { | ||
this.codeModel = Validate.notNull(codeModel); | ||
this.codeGeneratorFactory = Validate.notNull(codeGeneratorFactory); | ||
} | ||
|
||
@Override | ||
public void generate(JBlock block, JType type, JVar left, JVar right) { | ||
|
||
JBlock leftNeRight = block._if(left.ne(right))._then(); | ||
|
||
JConditional ifLeftOrRightIsNull = leftNeRight._if(JOp.cor( | ||
left.eq(JExpr._null()), right.eq(JExpr._null()))); | ||
|
||
final JBlock leftOrRightAreNull = ifLeftOrRightIsNull._then(); | ||
final JBlock leftAndRightAreNotNull = ifLeftOrRightIsNull._else(); | ||
|
||
leftOrRightAreNull._return(JExpr.FALSE); | ||
|
||
generateNonNull(leftAndRightAreNotNull, type, left, right); | ||
|
||
} | ||
|
||
public void generateNonNull(JBlock block, JType type, JVar left, JVar right) { | ||
|
||
// TODO extract type wildcard | ||
generate(block, type, left, right, "Name", "getName", QName.class); | ||
generate(block, type, left, right, "Value", "getValue", Object.class); | ||
final JClass classWildcard = codeModel.ref(Class.class).narrow( | ||
codeModel.ref(Object.class).wildcard()); | ||
generate(block, type, left, right, "DeclaredType", "getDeclaredType", | ||
classWildcard); | ||
generate(block, type, left, right, "Scope", "getScope", classWildcard); | ||
generate(block, type, left, right, "Nil", "isNil", codeModel.BOOLEAN); | ||
|
||
} | ||
|
||
private void generate(JBlock block, JType type, JVar left, JVar right, | ||
String propertyName, String method, Class<?> propertyType) { | ||
generate(block, type, left, right, propertyName, method, | ||
codeModel.ref(propertyType)); | ||
} | ||
|
||
private void generate(JBlock block, JType type, JVar left, JVar right, | ||
String propertyName, String method, JType propertyType) { | ||
|
||
final JBlock propertyBlock = block.block(); | ||
|
||
JVar leftPropertyValue = propertyBlock.decl(JMod.FINAL, propertyType, | ||
left.name() + propertyName, left.invoke(method)); | ||
JVar rightPropertyValue = propertyBlock.decl(JMod.FINAL, propertyType, | ||
right.name() + propertyName, right.invoke(method)); | ||
|
||
final HashCodeCodeGenerator codeGenerator = codeGeneratorFactory | ||
.getCodeGenerator(propertyType); | ||
codeGenerator.generate(propertyBlock, propertyType, leftPropertyValue, | ||
rightPropertyValue); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters