diff options
author | kmoore | 2010-12-30 19:02:15 +0000 |
---|---|---|
committer | kmoore | 2010-12-30 19:02:15 +0000 |
commit | f653106a89d49bf52323fc29394a933592287914 (patch) | |
tree | 128fa02876227c11745ba6841141d2e7608ffbcb | |
parent | 198c026101a49092ebbf5cbe0a676e95113b5b8a (diff) | |
download | webtools.dali-f653106a89d49bf52323fc29394a933592287914.tar.gz webtools.dali-f653106a89d49bf52323fc29394a933592287914.tar.xz webtools.dali-f653106a89d49bf52323fc29394a933592287914.zip |
added attribute(fields/properties) support to the JAXB context model
12 files changed, 2060 insertions, 19 deletions
diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/JaxbFactory.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/JaxbFactory.java index c602b3ca13..983247662d 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/JaxbFactory.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/JaxbFactory.java @@ -23,6 +23,8 @@ import org.eclipse.jpt.jaxb.core.context.JaxbPackage; import org.eclipse.jpt.jaxb.core.context.JaxbPackageInfo; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentEnum; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentField; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentProperty; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentType; import org.eclipse.jpt.jaxb.core.context.JaxbRegistry; import org.eclipse.jpt.jaxb.core.context.XmlJavaTypeAdapter; @@ -32,6 +34,7 @@ import org.eclipse.jpt.jaxb.core.context.XmlSchema; import org.eclipse.jpt.jaxb.core.context.XmlSchemaType; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceEnum; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceEnumConstant; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourcePackage; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType; @@ -125,4 +128,7 @@ public interface JaxbFactory { JaxbElementFactoryMethod buildJavaElementFactoryMethod(JaxbRegistry parent, JavaResourceMethod resourceMethod); + JaxbPersistentField buildJavaPersistentField(JaxbPersistentClass parent, JavaResourceField resourceField); + + JaxbPersistentProperty buildJavaPersistentProperty(JaxbPersistentClass parent, JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter); } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentAttribute.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentAttribute.java new file mode 100644 index 0000000000..6a347252f3 --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentAttribute.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.context; + +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; + +/** + * Represents a JAXB attribute (field/property). + * <p> + * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * @version 3.0 + * @since 3.0 + */ +public interface JaxbPersistentAttribute + extends JaxbContextNode { + + /** + * Return the name of the attribute. This will not change, a + * new JaxbPersistentAttribute will be built if the name changes. + */ + String getName(); + + boolean isFor(JavaResourceField resourceField); + + boolean isFor(JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter); + +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentClass.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentClass.java index ed6ce35956..b3989eab95 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentClass.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentClass.java @@ -51,4 +51,11 @@ public interface JaxbPersistentClass */ Iterable<JaxbPersistentClass> getAncestors(); + + /********** attributes **********/ + + Iterable<JaxbPersistentAttribute> getAttributes(); + int getAttributesSize(); + String ATTRIBUTES_COLLECTION = "attributes"; //$NON-NLS-1$ + } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentField.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentField.java new file mode 100644 index 0000000000..e87e68feae --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentField.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.context; + +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; + +/** + * Represents a JAXB field. + * <p> + * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * @version 3.0 + * @since 3.0 + */ +public interface JaxbPersistentField + extends JaxbPersistentAttribute { + + JavaResourceField getResourceField(); + +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentProperty.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentProperty.java new file mode 100644 index 0000000000..329d44807e --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/context/JaxbPersistentProperty.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle - initial API and implementation + *******************************************************************************/ +package org.eclipse.jpt.jaxb.core.context; + +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; + +/** + * Represents a JAXB property (getter/setter pair) + * <p> + * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * @version 3.0 + * @since 3.0 + */ +public interface JaxbPersistentProperty + extends JaxbPersistentAttribute { + + JavaResourceMethod getResourceGetterMethod(); + + JavaResourceMethod getResourceSetterMethod(); + +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbFactory.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbFactory.java index 467bd95c69..2b768ce04e 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbFactory.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbFactory.java @@ -24,6 +24,8 @@ import org.eclipse.jpt.jaxb.core.context.JaxbPackage; import org.eclipse.jpt.jaxb.core.context.JaxbPackageInfo; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentEnum; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentField; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentProperty; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentType; import org.eclipse.jpt.jaxb.core.context.JaxbRegistry; import org.eclipse.jpt.jaxb.core.context.XmlJavaTypeAdapter; @@ -38,6 +40,8 @@ import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaEnumConstant; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaPackageInfo; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaPersistentClass; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaPersistentEnum; +import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaPersistentField; +import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaPersistentProperty; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaRegistry; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaXmlJavaTypeAdapter; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaXmlNs; @@ -46,6 +50,7 @@ import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaXmlSchema; import org.eclipse.jpt.jaxb.core.internal.context.java.GenericJavaXmlSchemaType; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceEnum; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceEnumConstant; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourcePackage; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType; @@ -133,4 +138,12 @@ public abstract class AbstractJaxbFactory public JaxbElementFactoryMethod buildJavaElementFactoryMethod(JaxbRegistry parent, JavaResourceMethod resourceMethod) { return new GenericJavaElementFactoryMethod(parent, resourceMethod); } + + public JaxbPersistentField buildJavaPersistentField(JaxbPersistentClass parent, JavaResourceField resourceField) { + return new GenericJavaPersistentField(parent, resourceField); + } + + public JaxbPersistentProperty buildJavaPersistentProperty(JaxbPersistentClass parent, JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter) { + return new GenericJavaPersistentProperty(parent, resourceGetter, resourceSetter); + } } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbProject.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbProject.java index 0f4cd44756..99ccc1fd9f 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbProject.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/AbstractJaxbProject.java @@ -592,13 +592,13 @@ public abstract class AbstractJaxbProject /** * Return all {@link JavaResourceType}s that are represented by java source within this project, - * that are also annotated (and persistable) with a recognized annotation + * that are also annotated with a recognized annotation */ public Iterable<JavaResourceType> getAnnotatedJavaSourceResourceTypes() { return new FilteringIterable<JavaResourceType>(getJavaSourceResourceTypes()) { @Override protected boolean accept(JavaResourceType type) { - return type.isPersistable() && type.isAnnotated(); // i.e. the type is valid and has a valid type annotation + return type.isAnnotated(); } }; } @@ -713,7 +713,7 @@ public abstract class AbstractJaxbProject public JavaResourceType getJavaResourceType(String typeName) { - for (JavaResourceType type : this.getPersistableJavaResourceTypes()) { + for (JavaResourceType type : this.getJavaResourceTypes()) { if (type.getQualifiedName().equals(typeName)) { return type; } @@ -735,20 +735,6 @@ public abstract class AbstractJaxbProject } /** - * return *all* the "persistable" Java resource persistent types, including those in JARs referenced in - * persistence.xml - * @see org.eclipse.jpt.core.internal.utility.jdt.JPTTools#typeIsPersistable(org.eclipse.jpt.core.internal.utility.jdt.JPTTools.TypeAdapter) - */ - protected Iterable<JavaResourceType> getPersistableJavaResourceTypes() { - return new FilteringIterable<JavaResourceType>(this.getJavaResourceTypes()) { - @Override - protected boolean accept(JavaResourceType type) { - return type.isPersistable(); - } - }; - } - - /** * return *all* the Java resource persistent types, including those in JARs referenced in * persistence.xml */ diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentAttribute.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentAttribute.java new file mode 100644 index 0000000000..b9b5e3755e --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentAttribute.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jaxb.core.internal.context.java; + +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentAttribute; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; +import org.eclipse.jpt.jaxb.core.internal.context.AbstractJaxbContextNode; + +public abstract class GenericJavaPersistentAttribute + extends AbstractJaxbContextNode + implements JaxbPersistentAttribute { + + + + protected GenericJavaPersistentAttribute(JaxbPersistentClass parent) { + super(parent); + } + + public void synchronizeWithResourceModel() { + + } + + public void update() { + + } +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentClass.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentClass.java index c1e46a5373..18300e39ef 100644 --- a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentClass.java +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentClass.java @@ -9,18 +9,32 @@ ******************************************************************************/ package org.eclipse.jpt.jaxb.core.internal.context.java; +import java.lang.reflect.Modifier; import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; +import java.util.Vector; import org.eclipse.jpt.jaxb.core.context.JaxbContextRoot; import org.eclipse.jpt.jaxb.core.context.JaxbPackageInfo; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentAttribute; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentField; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentProperty; import org.eclipse.jpt.jaxb.core.context.XmlAccessOrder; import org.eclipse.jpt.jaxb.core.context.XmlAccessType; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMember; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType; import org.eclipse.jpt.jaxb.core.resource.java.XmlAccessorOrderAnnotation; import org.eclipse.jpt.jaxb.core.resource.java.XmlAccessorTypeAnnotation; +import org.eclipse.jpt.utility.Filter; import org.eclipse.jpt.utility.internal.CollectionTools; +import org.eclipse.jpt.utility.internal.StringTools; import org.eclipse.jpt.utility.internal.iterables.ChainIterable; +import org.eclipse.jpt.utility.internal.iterables.FilteringIterable; +import org.eclipse.jpt.utility.internal.iterables.ListIterable; +import org.eclipse.jpt.utility.internal.iterables.LiveCloneIterable; public class GenericJavaPersistentClass extends AbstractJavaPersistentType @@ -34,6 +48,8 @@ public class GenericJavaPersistentClass protected XmlAccessOrder defaultAccessOrder; protected XmlAccessOrder specifiedAccessOrder; + protected final Vector<JaxbPersistentAttribute> attributes = new Vector<JaxbPersistentAttribute>(); + public GenericJavaPersistentClass(JaxbContextRoot parent, JavaResourceType resourceType) { super(parent, resourceType); this.superPersistentClass = this.buildSuperPersistentClass(); @@ -41,6 +57,7 @@ public class GenericJavaPersistentClass this.specifiedAccessOrder = this.getResourceAccessOrder(); this.defaultAccessType = this.buildDefaultAccessType(); this.defaultAccessOrder = this.buildDefaultAccessOrder(); + this.initializeAttributes(); } @Override @@ -55,6 +72,7 @@ public class GenericJavaPersistentClass super.synchronizeWithResourceModel(); this.setSpecifiedAccessType_(this.getResourceAccessType()); this.setSpecifiedAccessOrder_(this.getResourceAccessOrder()); + this.syncAttributes(); } @Override @@ -62,6 +80,7 @@ public class GenericJavaPersistentClass this.setSuperPersistentClass(this.buildSuperPersistentClass()); this.setDefaultAccessType(this.buildDefaultAccessType()); this.setDefaultAccessOrder(this.buildDefaultAccessOrder()); + this.updateAttributes(); } @@ -279,4 +298,554 @@ public class GenericJavaPersistentClass return packageInfo == null ? null : packageInfo.getAccessOrder(); } + + // ********** attributes ********** + + public Iterable<JaxbPersistentAttribute> getAttributes() { + return new LiveCloneIterable<JaxbPersistentAttribute>(this.attributes); + } + + public int getAttributesSize() { + return this.attributes.size(); + } + + protected void addAttribute(JaxbPersistentAttribute attribute) { + addItemToCollection(attribute, this.attributes, ATTRIBUTES_COLLECTION); + } + + protected void removeAttribute(JaxbPersistentAttribute attribute) { + removeItemFromCollection(attribute, this.attributes, ATTRIBUTES_COLLECTION); + } + + protected JaxbPersistentField buildField(JavaResourceField resourceField) { + return getFactory().buildJavaPersistentField(this, resourceField); + } + + protected JaxbPersistentProperty buildProperty(JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter) { + return getFactory().buildJavaPersistentProperty(this, resourceGetter, resourceSetter); + } + + protected void initializeAttributes() { + if (getAccessType() == XmlAccessType.PUBLIC_MEMBER) { + this.initializePublicMemberAccessAttributes(); + } + else if (getAccessType() == XmlAccessType.FIELD) { + this.intializeFieldAccessAttributes(); + } + else if (getAccessType() == XmlAccessType.PROPERTY) { + this.intializePropertyAccessAttributes(); + } + else if (getAccessType() == XmlAccessType.NONE) { + this.intializeNoneAccessAttributes(); + } + } + + /** + * Initialize the attributes for XmlAccessType.PUBLIC_MEMBER + * 1. all public, non-static, non-transient fields (transient modifier, @XmlTransient is brought to the context model) + * 2. all annotated fields that aren't public + * 3. all public getter/setter javabeans pairs + * 4. all annotated methods (some will have a matching getter/setter, some will be standalone) + */ + private void initializePublicMemberAccessAttributes() { + this.initializeFieldAttributes(this.buildPublicMemberAccessTypeResourceFieldsFilter()); + Collection<JavaResourceMethod> resourceMethods = CollectionTools.collection(this.getResourceMethods()); + //iterate through all persistable resource method getters + for (JavaResourceMethod getterMethod : this.getResourceMethods(this.buildPersistablePropertyGetterMethodsFilter())) { + JavaResourceMethod setterMethod = getValidSiblingSetMethod(getterMethod, resourceMethods); + if (methodsArePersistablePublicMemberAccess(getterMethod, setterMethod)) { + this.attributes.add(this.buildProperty(getterMethod, setterMethod)); + } + resourceMethods.remove(getterMethod); + resourceMethods.remove(setterMethod); + } + this.initializeRemainingResourceMethodAttributes(resourceMethods); + } + + /** + * Initialize the attributes for XmlAccessType.FIELD + * 1. all non-transient fields + * 2. all annotated methods getters/setters + */ + private void intializeFieldAccessAttributes() { + this.initializeFieldAttributes(this.buildNonTransientNonStaticResourceFieldsFilter()); + this.initializeAnnotatedPropertyAttributes(); + } + + /** + * Initialize the attributes for XmlAccessType.PROPERTY + * 1. all getter/setter javabeans pairs + * 2. all annotated fields + * 3. all annotated methods getters/setters that don't have a matching pair + */ + private void intializePropertyAccessAttributes() { + this.initializeFieldAttributes(this.buildAnnotatedResourceFieldsFilter()); + + Collection<JavaResourceMethod> resourceMethods = CollectionTools.collection(this.getResourceMethods()); + //iterate through all resource methods searching for persistable getters + for (JavaResourceMethod getterMethod : this.getResourceMethods(this.buildPersistablePropertyGetterMethodsFilter())) { + JavaResourceMethod setterMethod = getValidSiblingSetMethod(getterMethod, resourceMethods); + if (methodsArePersistableProperties(getterMethod, setterMethod)) { + this.attributes.add(this.buildProperty(getterMethod, setterMethod)); + } + resourceMethods.remove(getterMethod); + resourceMethods.remove(setterMethod); + } + this.initializeRemainingResourceMethodAttributes(resourceMethods); + } + + /** + * Initialize the attributes for XmlAccessType.NONE + * 1. all annotated fields + * 2. all annotated methods getters/setters (some will have a matching getter/setter, some will be standalone) + */ + private void intializeNoneAccessAttributes() { + this.initializeFieldAttributes(this.buildAnnotatedResourceFieldsFilter()); + this.initializeAnnotatedPropertyAttributes(); + } + + private void initializeFieldAttributes(Filter<JavaResourceField> filter) { + for (JavaResourceField resourceField : this.getResourceFields(filter)) { + this.attributes.add(this.buildField(resourceField)); + } + } + + private void initializeRemainingResourceMethodAttributes(Collection<JavaResourceMethod> resourceMethods) { + //iterate through remaining resource methods and search for those that are annotated. + //all getter methods will already be used. + for (JavaResourceMethod resourceMethod : resourceMethods) { + if (resourceMethod.isAnnotated()) { + //annotated setter(or other random method) with no corresponding getter, bring into context model for validation purposes + this.attributes.add(this.buildProperty(null, resourceMethod)); + } + } + } + + private static boolean methodsArePersistableProperties(JavaResourceMethod getterMethod, JavaResourceMethod setterMethod) { + if (setterMethod != null) { + return true; + } + //Lists do not have to have a corresponding setter method + else if (getterMethod.getReturnTypeName().equals("java.util.List")) { //$NON-NLS-1$ + return true; + } + else if (getterMethod.isAnnotated()) { + //annotated getter with no corresponding setter, bring into context model for validation purposes + return true; + } + return false; + } + + private static boolean methodsArePersistablePublicMemberAccess(JavaResourceMethod getterMethod, JavaResourceMethod setterMethod) { + if (getterMethod.isPublic()) { + if (setterMethod != null) { + if (setterMethod.isPublic()) { + return true; + } + } + //Lists do not have to have a corresponding setter method + else if (getterMethod.getReturnTypeName().equals("java.util.List")) { //$NON-NLS-1$ + return true; + } + else if (getterMethod.isAnnotated()) { + //annotated getter with no corresponding setter, bring into context model for validation purposes + return true; + } + } + else if (getterMethod.isAnnotated() || (setterMethod != null && setterMethod.isAnnotated())) { + return true; + } + return false; + } + + private void initializeAnnotatedPropertyAttributes() { + Collection<JavaResourceMethod> resourceMethods = CollectionTools.collection(this.getResourceMethods()); + //iterate through all resource methods searching for persistable getters + for (JavaResourceMethod getterMethod : this.getResourceMethods(buildPersistablePropertyGetterMethodsFilter())) { + JavaResourceMethod setterMethod = getValidSiblingSetMethod(getterMethod, resourceMethods); + if (getterMethod.isAnnotated() || (setterMethod != null && setterMethod.isAnnotated())) { + this.attributes.add(this.buildProperty(getterMethod, setterMethod)); + } + resourceMethods.remove(getterMethod); + resourceMethods.remove(setterMethod); + } + this.initializeRemainingResourceMethodAttributes(resourceMethods); + } + + protected Iterable<JavaResourceField> getResourceFields() { + return getJavaResourceType().getFields(); + } + + protected Iterable<JavaResourceMethod> getResourceMethods() { + return getJavaResourceType().getMethods(); + } + + protected Iterable<JavaResourceField> getResourceFields(Filter<JavaResourceField> filter) { + return new FilteringIterable<JavaResourceField>(getResourceFields(), filter); + } + + protected Iterable<JavaResourceMethod> getResourceMethods(Filter<JavaResourceMethod> filter) { + return new FilteringIterable<JavaResourceMethod>(getResourceMethods(), filter); + } + + protected Filter<JavaResourceField> buildNonTransientNonStaticResourceFieldsFilter() { + return new Filter<JavaResourceField>() { + public boolean accept(JavaResourceField resourceField) { + return memberIsNonTransientNonStatic(resourceField) || resourceField.isAnnotated(); + } + }; + } + + protected Filter<JavaResourceField> buildPublicMemberAccessTypeResourceFieldsFilter() { + return new Filter<JavaResourceField>() { + public boolean accept(JavaResourceField resourceField) { + return memberIsPublicNonTransientNonStatic(resourceField) || resourceField.isAnnotated(); + } + }; + } + + protected Filter<JavaResourceMethod> buildPersistablePropertyGetterMethodsFilter() { + return new Filter<JavaResourceMethod>() { + public boolean accept(JavaResourceMethod resourceMethod) { + return methodIsPersistablePropertyGetter(resourceMethod, getResourceMethods()); + } + }; + } + + protected static boolean memberIsPublicNonTransientNonStatic(JavaResourceMember resourceMember) { + return resourceMember.isPublic() && memberIsNonTransientNonStatic(resourceMember); + } + + protected static boolean memberIsNonTransientNonStatic(JavaResourceMember resourceMember) { + return !resourceMember.isTransient() && !resourceMember.isStatic(); + } + + protected Filter<JavaResourceField> buildAnnotatedResourceFieldsFilter() { + return new Filter<JavaResourceField>() { + public boolean accept(JavaResourceField resourceField) { + return resourceField.isAnnotated(); + } + }; + } + + protected void syncAttributes() { + if (getAccessType() == XmlAccessType.PUBLIC_MEMBER) { + this.syncPublicMemberAccessAttributes(); + } + else if (getAccessType() == XmlAccessType.FIELD) { + this.syncFieldAccessAttributes(); + } + else if (getAccessType() == XmlAccessType.PROPERTY) { + this.syncPropertyAccessAttributes(); + } + else if (getAccessType() == XmlAccessType.NONE) { + this.syncNoneAccessAttributes(); + } + } + + /** + * Sync the attributes for XmlAccessType.PUBLIC_MEMBER + * 1. all public, non-static, non-transient fields (transient modifier, @XmlTransient is brought to the context model) + * 2. all annotated fields that aren't public + * 3. all public getter/setter javabeans pairs + * 4. all annotated methods (some will have a matching getter/setter, some will be standalone) + */ + private void syncPublicMemberAccessAttributes() { + HashSet<JaxbPersistentAttribute> contextAttributes = CollectionTools.set(this.getAttributes()); + + this.syncFieldAttributes(contextAttributes, this.buildPublicMemberAccessTypeResourceFieldsFilter()); + + Collection<JavaResourceMethod> resourceMethods = CollectionTools.collection(this.getResourceMethods()); + //iterate through all persistable resource method getters + for (JavaResourceMethod getterMethod : this.getResourceMethods(this.buildPersistablePropertyGetterMethodsFilter())) { + JavaResourceMethod setterMethod = getValidSiblingSetMethod(getterMethod, resourceMethods); + if (methodsArePersistablePublicMemberAccess(getterMethod, setterMethod)) { + boolean match = false; + for (Iterator<JaxbPersistentAttribute> stream = contextAttributes.iterator(); stream.hasNext();) { + JaxbPersistentAttribute contextAttribute = stream.next(); + if (contextAttribute.isFor(getterMethod, setterMethod)) { + match = true; + contextAttribute.synchronizeWithResourceModel(); + stream.remove(); + break; + } + } + if (!match) { + this.addAttribute(this.buildProperty(getterMethod, setterMethod)); + } + resourceMethods.remove(getterMethod); + resourceMethods.remove(setterMethod); + } + } + this.syncRemainingResourceMethods(contextAttributes, resourceMethods); + } + + /** + * Initialize the attributes for XmlAccessType.FIELD + * 1. all non-transient fields + * 2. all annotated methods getters/setters + */ + private void syncFieldAccessAttributes() { + HashSet<JaxbPersistentAttribute> contextAttributes = CollectionTools.set(this.getAttributes()); + + this.syncFieldAttributes(contextAttributes, this.buildNonTransientNonStaticResourceFieldsFilter()); + this.syncAnnotatedPropertyAttributes(contextAttributes); + } + + /** + * Initialize the attributes for XmlAccessType.PROPERTY + * 1. all getter/setter javabeans pairs + * 2. all annotated fields + * 3. all annotated methods getters/setters that don't have a matching pair + */ + private void syncPropertyAccessAttributes() { + HashSet<JaxbPersistentAttribute> contextAttributes = CollectionTools.set(this.getAttributes()); + + this.syncFieldAttributes(contextAttributes, this.buildAnnotatedResourceFieldsFilter()); + + Collection<JavaResourceMethod> resourceMethods = CollectionTools.collection(this.getResourceMethods()); + //iterate through all resource methods searching for persistable getters + for (JavaResourceMethod getterMethod : this.getResourceMethods(this.buildPersistablePropertyGetterMethodsFilter())) { + JavaResourceMethod setterMethod = getValidSiblingSetMethod(getterMethod, resourceMethods); + if (methodsArePersistableProperties(getterMethod, setterMethod)) { + boolean match = false; + for (Iterator<JaxbPersistentAttribute> stream = contextAttributes.iterator(); stream.hasNext();) { + JaxbPersistentAttribute contextAttribute = stream.next(); + if (contextAttribute.isFor(getterMethod, setterMethod)) { + match = true; + contextAttribute.synchronizeWithResourceModel(); + stream.remove(); + break; + } + } + if (!match) { + this.addAttribute(this.buildProperty(getterMethod, setterMethod)); + } + } + resourceMethods.remove(getterMethod); + resourceMethods.remove(setterMethod); + } + this.syncRemainingResourceMethods(contextAttributes, resourceMethods); + } + + /** + * Initialize the attributes for XmlAccessType.NONE + * 1. all annotated fields + * 2. all annotated methods getters/setters (some will have a matching getter/setter, some will be standalone) + */ + private void syncNoneAccessAttributes() { + HashSet<JaxbPersistentAttribute> contextAttributes = CollectionTools.set(this.getAttributes()); + + this.syncFieldAttributes(contextAttributes, this.buildAnnotatedResourceFieldsFilter()); + this.syncAnnotatedPropertyAttributes(contextAttributes); + } + + private void syncAnnotatedPropertyAttributes(HashSet<JaxbPersistentAttribute> contextAttributes) { + Collection<JavaResourceMethod> resourceMethods = CollectionTools.collection(this.getResourceMethods()); + //iterate through all resource methods searching for persistable getters + for (JavaResourceMethod getterMethod : this.getResourceMethods(buildPersistablePropertyGetterMethodsFilter())) { + JavaResourceMethod setterMethod = getValidSiblingSetMethod(getterMethod, resourceMethods); + if (getterMethod.isAnnotated() || (setterMethod != null && setterMethod.isAnnotated())) { + boolean match = false; + for (Iterator<JaxbPersistentAttribute> stream = contextAttributes.iterator(); stream.hasNext();) { + JaxbPersistentAttribute contextAttribute = stream.next(); + if (contextAttribute.isFor(getterMethod, setterMethod)) { + match = true; + contextAttribute.synchronizeWithResourceModel(); + stream.remove(); + break; + } + } + if (!match) { + this.addAttribute(this.buildProperty(getterMethod, setterMethod)); + } + } + resourceMethods.remove(getterMethod); + resourceMethods.remove(setterMethod); + } + this.syncRemainingResourceMethods(contextAttributes, resourceMethods); + } + + private void syncFieldAttributes(HashSet<JaxbPersistentAttribute> contextAttributes, Filter<JavaResourceField> filter) { + for (JavaResourceField resourceField : this.getResourceFields(filter)) { + boolean match = false; + for (Iterator<JaxbPersistentAttribute> stream = contextAttributes.iterator(); stream.hasNext(); ) { + JaxbPersistentAttribute contextAttribute = stream.next(); + if (contextAttribute.isFor(resourceField)) { + match = true; + contextAttribute.synchronizeWithResourceModel(); + stream.remove(); + break; + } + } + if (!match) { + // added elements are sync'ed during construction or will be + // updated during the next "update" (which is triggered by + // their addition to the model) + this.addAttribute(this.buildField(resourceField)); + } + } + } + + private void syncRemainingResourceMethods(HashSet<JaxbPersistentAttribute> contextAttributes, Collection<JavaResourceMethod> resourceMethods) { + //iterate through remaining resource methods and search for those that are annotated. + //all getter methods will already be used. + for (JavaResourceMethod resourceMethod : resourceMethods) { + if (resourceMethod.isAnnotated()) { + boolean match = false; + //annotated setter(or other random method) with no corresponding getter, bring into context model for validation purposes + for (Iterator<JaxbPersistentAttribute> stream = contextAttributes.iterator(); stream.hasNext();) { + JaxbPersistentAttribute contextAttribute = stream.next(); + if (contextAttribute.isFor(null, resourceMethod)) { + match = true; + contextAttribute.synchronizeWithResourceModel(); + stream.remove(); + break; + } + } + if (!match) { + this.addAttribute(this.buildProperty(null, resourceMethod)); + } + } + } + + // remove any leftover context attributes + for (JaxbPersistentAttribute contextAttribute : contextAttributes) { + this.removeAttribute(contextAttribute); + } + } + + protected void updateAttributes() { + this.updateNodes(this.attributes); + } + + /** + * Return whether the specified method is a "getter" method that + * represents a property that may be "persisted". + */ + protected static boolean methodIsPersistablePropertyGetter(JavaResourceMethod resourceMethod, Iterable<JavaResourceMethod> allMethods) { + if (methodHasInvalidModifiers(resourceMethod)) { + return false; + } + if (resourceMethod.isConstructor()) { + return false; + } + + String returnTypeName = resourceMethod.getReturnTypeName(); + if (returnTypeName == null) { + return false; // DOM method bindings can have a null name + } + if (returnTypeName.equals("void")) { //$NON-NLS-1$ + return false; + } + if (methodHasParameters(resourceMethod)) { + return false; + } + + boolean booleanGetter = methodIsBooleanGetter(resourceMethod); + + // if the type has both methods: + // boolean isProperty() + // boolean getProperty() + // then #isProperty() takes precedence and we ignore #getProperty(); + // but only having #getProperty() is OK too + // (see the JavaBeans spec 1.01) + if (booleanGetter && methodHasValidSiblingIsMethod(resourceMethod, allMethods)) { + return false; // since the type also defines #isProperty(), ignore #getProperty() + } + return true; + } + + private static boolean methodIsBooleanGetter(JavaResourceMethod resourceMethod) { + String returnTypeName = resourceMethod.getReturnTypeName(); + String name = resourceMethod.getMethodName(); + boolean booleanGetter = false; + if (name.startsWith("is")) { //$NON-NLS-1$ + if (returnTypeName.equals("boolean")) { //$NON-NLS-1$ + } else { + return false; + } + } else if (name.startsWith("get")) { //$NON-NLS-1$ + if (returnTypeName.equals("boolean")) { //$NON-NLS-1$ + booleanGetter = true; + } + } else { + return false; + } + return booleanGetter; + } + + /** + * Return whether the method's modifiers prevent it + * from being a getter or setter for a "persistent" property. + */ + private static boolean methodHasInvalidModifiers(JavaResourceMethod resourceMethod) { + int modifiers = resourceMethod.getReturnTypeModifiers(); + if (Modifier.isStatic(modifiers)) { + return true; + } + return false; + } + + private static boolean methodHasParameters(JavaResourceMethod resourceMethod) { + return resourceMethod.getParametersSize() != 0; + } + + /** + * Return whether the method has a sibling "is" method for the specified + * property and that method is valid for a "persistable" property. + * Pre-condition: the method is a "boolean getter" (e.g. 'public boolean getProperty()'); + * this prevents us from returning true when the method itself is an + * "is" method. + */ + private static boolean methodHasValidSiblingIsMethod(JavaResourceMethod getMethod, Iterable<JavaResourceMethod> resourceMethods) { + String capitalizedAttributeName = StringTools.capitalize(getMethod.getName()); + for (JavaResourceMethod sibling : resourceMethods) { + if ((sibling.getParametersSize() == 0) + && sibling.getMethodName().equals("is" + capitalizedAttributeName)) { //$NON-NLS-1$ + return methodIsValidSibling(sibling, "boolean"); //$NON-NLS-1$ + } + } + return false; + } + + /** + * Return whether the method has a sibling "set" method + * and that method is valid for a "persistable" property. + */ + private static JavaResourceMethod getValidSiblingSetMethod(JavaResourceMethod getMethod, Collection<JavaResourceMethod> resourceMethods) { + String capitalizedAttributeName = StringTools.capitalize(getMethod.getName()); + String parameterTypeErasureName = getMethod.getReturnTypeName(); + for (JavaResourceMethod sibling : resourceMethods) { + ListIterable<String> siblingParmTypeNames = sibling.getParameterTypeNames(); + if ((sibling.getParametersSize() == 1) + && sibling.getMethodName().equals("set" + capitalizedAttributeName) //$NON-NLS-1$ + && siblingParmTypeNames.iterator().next().equals(parameterTypeErasureName)) { + return methodIsValidSibling(sibling, "void") ? sibling : null; //$NON-NLS-1$ + } + } + return null; + } + + /** + * Return whether the specified method is a valid sibling with the + * specified return type. + */ + private static boolean methodIsValidSibling(JavaResourceMethod resourceMethod, String returnTypeName) { + if (resourceMethod == null) { + return false; + } + if (methodHasInvalidModifiers(resourceMethod)) { + return false; + } + if (resourceMethod.isConstructor()) { + return false; + } + String rtName = resourceMethod.getReturnTypeName(); + if (rtName == null) { + return false; // DOM method bindings can have a null name + } + return rtName.equals(returnTypeName); + } + } diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentField.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentField.java new file mode 100644 index 0000000000..94ba768e06 --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentField.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jaxb.core.internal.context.java; + +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentField; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; + +public class GenericJavaPersistentField + extends GenericJavaPersistentAttribute + implements JaxbPersistentField { + + + protected final JavaResourceField resourceField; + + public GenericJavaPersistentField(JaxbPersistentClass parent, JavaResourceField resourceField) { + super(parent); + this.resourceField = resourceField; + } + + public JavaResourceField getResourceField() { + return this.resourceField; + } + + public String getName() { + return this.resourceField.getName(); + } + + public boolean isFor(JavaResourceField resourceField) { + return this.resourceField == resourceField; + } + + public boolean isFor(JavaResourceMethod getterMethod, JavaResourceMethod setterMethod) { + return false; + } + +} diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentProperty.java b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentProperty.java new file mode 100644 index 0000000000..3184c96b7f --- /dev/null +++ b/jaxb/plugins/org.eclipse.jpt.jaxb.core/src/org/eclipse/jpt/jaxb/core/internal/context/java/GenericJavaPersistentProperty.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.jaxb.core.internal.context.java; + +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentProperty; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; + +public class GenericJavaPersistentProperty + extends GenericJavaPersistentAttribute + implements JaxbPersistentProperty { + + + protected final JavaResourceMethod resourceGetter; + + protected final JavaResourceMethod resourceSetter; + + public GenericJavaPersistentProperty(JaxbPersistentClass parent, JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter) { + super(parent); + this.resourceGetter = resourceGetter; + this.resourceSetter = resourceSetter; + } + + public JavaResourceMethod getResourceGetterMethod() { + return this.resourceGetter; + } + + public JavaResourceMethod getResourceSetterMethod() { + return this.resourceSetter; + } + + public String getName() { + if (this.resourceGetter != null) { + return this.resourceGetter.getName(); + } + return this.resourceSetter.getName(); + } + + + public boolean isFor(JavaResourceField resourceField) { + return false; + } + + public boolean isFor(JavaResourceMethod getterMethod, JavaResourceMethod setterMethod) { + return (this.resourceGetter == getterMethod) && (this.resourceSetter == setterMethod); + } + +} diff --git a/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaPersistentClassTests.java b/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaPersistentClassTests.java index 1d37830624..2d59b52e59 100644 --- a/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaPersistentClassTests.java +++ b/jaxb/tests/org.eclipse.jpt.jaxb.core.tests/src/org/eclipse/jpt/jaxb/core/tests/internal/context/java/GenericJavaPersistentClassTests.java @@ -10,25 +10,39 @@ package org.eclipse.jpt.jaxb.core.tests.internal.context.java; import java.util.Iterator; +import java.util.List; import java.util.ListIterator; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.BodyDeclaration; +import org.eclipse.jdt.core.dom.IExtendedModifier; import org.eclipse.jdt.core.dom.MarkerAnnotation; +import org.eclipse.jdt.core.dom.Modifier; +import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword; import org.eclipse.jdt.core.dom.NormalAnnotation; import org.eclipse.jpt.core.tests.internal.projects.TestJavaProject.SourceWriter; import org.eclipse.jpt.core.utility.jdt.AnnotatedElement; import org.eclipse.jpt.core.utility.jdt.Member; import org.eclipse.jpt.core.utility.jdt.ModifiedDeclaration; import org.eclipse.jpt.jaxb.core.context.JaxbPackageInfo; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentAttribute; import org.eclipse.jpt.jaxb.core.context.JaxbPersistentClass; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentField; +import org.eclipse.jpt.jaxb.core.context.JaxbPersistentProperty; import org.eclipse.jpt.jaxb.core.context.XmlAccessOrder; import org.eclipse.jpt.jaxb.core.context.XmlAccessType; -import org.eclipse.jpt.jaxb.core.resource.java.JAXB; import org.eclipse.jpt.jaxb.core.resource.java.AbstractJavaResourceType; +import org.eclipse.jpt.jaxb.core.resource.java.JAXB; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceField; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMember; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceMethod; +import org.eclipse.jpt.jaxb.core.resource.java.JavaResourceType; import org.eclipse.jpt.jaxb.core.resource.java.XmlAccessorOrderAnnotation; import org.eclipse.jpt.jaxb.core.resource.java.XmlAccessorTypeAnnotation; +import org.eclipse.jpt.jaxb.core.resource.java.XmlAttributeAnnotation; import org.eclipse.jpt.jaxb.core.resource.java.XmlRootElementAnnotation; +import org.eclipse.jpt.jaxb.core.resource.java.XmlTransientAnnotation; import org.eclipse.jpt.jaxb.core.resource.java.XmlTypeAnnotation; import org.eclipse.jpt.jaxb.core.tests.internal.context.JaxbContextModelTestCase; import org.eclipse.jpt.utility.internal.CollectionTools; @@ -108,7 +122,156 @@ public class GenericJavaPersistentClassTests extends JaxbContextModelTestCase JAXB.XML_ACCESS_TYPE, JAXB.XML_ACCESSOR_TYPE); } - public void testModifyFactoryClass() throws Exception { + private void createTestXmlTypeWithFieldAndPublicMemberAccess() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TYPE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_ATTRIBUTE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TRANSIENT); + sb.append(";"); + sb.append(CR); + sb.append("import java.util.List;"); + sb.append(CR); + sb.append("@XmlType"); + sb.append(CR); + sb.append("public class ").append(TYPE_NAME).append(" "); + sb.append("{").append(CR); + sb.append(" public int foo;").append(CR); + sb.append(CR).append("}").append(CR); + } + }; + this.javaProject.createCompilationUnit(PACKAGE_NAME, "AnnotationTestType.java", sourceWriter); + } + + private void createTestXmlTypeWithPropertyAndPublicMemberAccess() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TYPE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_ATTRIBUTE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TRANSIENT); + sb.append(";"); + sb.append(CR); + sb.append("import java.util.List;"); + sb.append(CR); + sb.append("@XmlType"); + sb.append(CR); + sb.append("public class ").append(TYPE_NAME).append(" "); + sb.append("{").append(CR); + sb.append(" public int getFoo() {").append(CR); + sb.append(" return 1;").append(CR).append(" }").append(CR); + sb.append(" public void setFoo(int foo) {}").append(CR).append(CR); + sb.append(CR).append("}").append(CR); + } + }; + this.javaProject.createCompilationUnit(PACKAGE_NAME, "AnnotationTestType.java", sourceWriter); + } + + private void createTestXmlTypeWithPropertyGetterAndPublicMemberAccess() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TYPE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_ATTRIBUTE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TRANSIENT); + sb.append(";"); + sb.append(CR); + sb.append("import java.util.List;"); + sb.append(CR); + sb.append("@XmlType"); + sb.append(CR); + sb.append("public class ").append(TYPE_NAME).append(" "); + sb.append("{").append(CR); + sb.append(" public int getFoo() {").append(CR); + sb.append(" return 1;").append(CR).append(" }").append(CR); + sb.append(CR).append("}").append(CR); + } + }; + this.javaProject.createCompilationUnit(PACKAGE_NAME, "AnnotationTestType.java", sourceWriter); + } + + private void createTestXmlTypeWithPropertyGetterListAndPublicMemberAccess() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TYPE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_ATTRIBUTE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TRANSIENT); + sb.append(";"); + sb.append(CR); + sb.append("import java.util.List;"); + sb.append(CR); + sb.append("@XmlType"); + sb.append(CR); + sb.append("public class ").append(TYPE_NAME).append(" "); + sb.append("{").append(CR); + sb.append(" public List<?> getFoo() {").append(CR); + sb.append(" return 1;").append(CR).append(" }").append(CR); + sb.append(CR).append("}").append(CR); + } + }; + this.javaProject.createCompilationUnit(PACKAGE_NAME, "AnnotationTestType.java", sourceWriter); + } + + private void createTestXmlTypeWithPropertySetterAndPublicMemberAccess() throws Exception { + SourceWriter sourceWriter = new SourceWriter() { + public void appendSourceTo(StringBuilder sb) { + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TYPE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_ATTRIBUTE); + sb.append(";"); + sb.append(CR); + sb.append("import "); + sb.append(JAXB.XML_TRANSIENT); + sb.append(";"); + sb.append(CR); + sb.append("import java.util.List;"); + sb.append(CR); + sb.append("@XmlType"); + sb.append(CR); + sb.append("public class ").append(TYPE_NAME).append(" "); + sb.append("{").append(CR); + sb.append(" public void setFoo(int foo) {}").append(CR).append(CR); + sb.append(CR).append("}").append(CR); + } + }; + this.javaProject.createCompilationUnit(PACKAGE_NAME, "AnnotationTestType.java", sourceWriter); + } + + public void testModifyFactoryClass() throws Exception { createTypeWithXmlType(); JaxbPersistentClass persistentClass = CollectionTools.get(getContextRoot().getPersistentClasses(), 0); @@ -824,4 +987,1063 @@ public class GenericJavaPersistentClassTests extends JaxbContextModelTestCase assertNull(persistentClass.getRootElement()); assertFalse(persistentClass.isRootElement()); } + + + public void testUpdateFieldAttributes() throws Exception { + this.createTestXmlTypeWithFieldAndPublicMemberAccess(); + + JaxbPersistentClass contextClass = CollectionTools.get(getContextRoot().getPersistentClasses(), 0); + + //public int foo; + this.publicFieldTest(contextClass); + + //public transient int foo; + this.publicTransientFieldTest(contextClass); + + //public static int foo; + this.publicStaticFieldTest(contextClass); + + //public final int foo; + this.publicFinalFieldTest(contextClass); + + //public static final int foo; + this.publicStaticFinalFieldTest(contextClass); + + //private int foo; + this.privateFieldTest(contextClass); + + // private transient int foo; + this.privateTransientFieldTest(contextClass); + + // @XmlAttribute + // private static int foo; //persistent + this.privateStaticFieldTest(contextClass); + + // @XmlAttribute + // private static final int foo; //persistent + this.privateStaticFinalFieldTest(contextClass); + } + + protected void publicFieldTest(JaxbPersistentClass contextClass) { + //public int foo; PUBLIC_MEMBER access - persistent + assertEquals(1, contextClass.getAttributesSize()); + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + + //add @XmlTransient annotation and test each access type + JavaResourceField resourceField = ((JaxbPersistentField) contextClass.getAttributes().iterator().next()).getResourceField(); + AnnotatedElement annotatedElement = this.annotatedElement(resourceField); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlTransientAnnotation.ANNOTATION_NAME); + } + }); + //@XmlTransient + //public int foo; //PROPERTY access - not "persistent", but comes in to our context model because it is "mapped" + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlTransient + //public int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlTransient + //public int foo; //NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlTransient + //public int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlTransientAnnotation.ANNOTATION_NAME); + } + }); + } + + protected void publicTransientFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + + //public transient int foo; PUBLIC_MEMBER access - not persistent + this.addModifiers(resourceField, ModifierKeyword.TRANSIENT_KEYWORD); + assertEquals(0, contextClass.getAttributesSize()); + + //public transient int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public transient int foo; //FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public transient int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public transient int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + this.removeModifiers(resourceField, ModifierKeyword.TRANSIENT_KEYWORD); + } + + protected void publicStaticFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + + //public static int foo; PUBLIC_MEMBER access - not persistent + this.addModifiers(resourceField, ModifierKeyword.STATIC_KEYWORD); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int foo; //FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + this.removeModifiers(resourceField, ModifierKeyword.STATIC_KEYWORD); + } + + protected void publicFinalFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + + //public final int foo; PUBLIC_MEMBER access - persistent + this.addModifiers(resourceField, ModifierKeyword.FINAL_KEYWORD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public final int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public final int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public final int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public final int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + this.removeModifiers(resourceField, ModifierKeyword.FINAL_KEYWORD); + } + + protected void publicStaticFinalFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + + //public static final int foo; PUBLIC_MEMBER access - not persistent + this.addModifiers(resourceField, ModifierKeyword.STATIC_KEYWORD, ModifierKeyword.FINAL_KEYWORD); + assertEquals(0, contextClass.getAttributesSize()); + + //public static final int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public static final int foo; //FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public static final int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public static final int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + this.removeModifiers(resourceField, ModifierKeyword.STATIC_KEYWORD, ModifierKeyword.FINAL_KEYWORD); + } + + protected void privateFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + removeModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD); + + //private int foo; PUBLIC_MEMBER access - not persistent + assertEquals(0, contextClass.getAttributesSize()); + + //private int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //private int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //private int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //private int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + AnnotatedElement annotatedElement = this.annotatedElement(resourceField); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //private int foo; //PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private int foo; //NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + removeModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + } + + protected void privateTransientFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + removeModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD, ModifierKeyword.TRANSIENT_KEYWORD); + + //private transient int foo; PUBLIC_MEMBER access - not persistent + assertEquals(0, contextClass.getAttributesSize()); + + //private transient int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //private transient int foo; //FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //private transient int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //private transient int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + AnnotatedElement annotatedElement = this.annotatedElement(resourceField); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //private transient int foo; //PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private transient int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private transient int foo; //NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private transient int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + removeModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD, ModifierKeyword.TRANSIENT_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + } + + protected void privateStaticFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + removeModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD, ModifierKeyword.STATIC_KEYWORD); + + //private static int foo; PUBLIC_MEMBER access - not persistent + assertEquals(0, contextClass.getAttributesSize()); + + //private static int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //private static int foo; //FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //private static int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //private static int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + AnnotatedElement annotatedElement = this.annotatedElement(resourceField); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //private static int foo; //PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private static int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private static int foo; //NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private static int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + removeModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD, ModifierKeyword.STATIC_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + } + + protected void privateStaticFinalFieldTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JavaResourceField resourceField = ((JaxbPersistentField) attributes.next()).getResourceField(); + removeModifiers(resourceField, ModifierKeyword.PUBLIC_KEYWORD); + addModifiers(resourceField, ModifierKeyword.PRIVATE_KEYWORD, ModifierKeyword.STATIC_KEYWORD, ModifierKeyword.FINAL_KEYWORD); + + //private static final int foo; PUBLIC_MEMBER access - not persistent + assertEquals(0, contextClass.getAttributesSize()); + + //private static final int foo; //PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //private static final int foo; //FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //private static final int foo; //NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //private static final int foo; PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + AnnotatedElement annotatedElement = this.annotatedElement(resourceField); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //private static final int foo; //PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private static final int foo; //FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private static final int foo; //NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private static final int foo; PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + } + + public void testUpdatePropertyAttributes() throws Exception { + this.createTestXmlTypeWithPropertyAndPublicMemberAccess(); + + JaxbPersistentClass contextClass = CollectionTools.get(getContextRoot().getPersistentClasses(), 0); + + //public int getFoo();, public void setFoo(int); + this.publicGetterSetterTest(contextClass); + + //public static int getFoo();, public static void setFoo(int); + this.publicStaticGetterSetterTest(contextClass); + + //private int getFoo();, private void setFoo(int); + this.privateGetterSetterTest(contextClass); + } + + public void testUpdateGetterPropertyAttributes() throws Exception { + this.createTestXmlTypeWithPropertyGetterAndPublicMemberAccess(); + + JaxbPersistentClass contextClass = CollectionTools.get(getContextRoot().getPersistentClasses(), 0); + + //public int getFoo(); PUBLIC_MEMBER access - not persistent + assertEquals(0, contextClass.getAttributesSize()); + + //public int getFoo(); PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public int getFoo(); FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public int getFoo(); NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public int getFoo(); PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + JavaResourceMethod resourceMethod = contextClass.getJavaResourceType().getMethods().iterator().next(); + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //public int getFoo(); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + assertEquals(1, contextClass.getAttributesSize()); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public int getFoo(); FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public int getFoo(); NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public int getFoo(); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + } + + public void testUpdateSetterPropertyAttributes() throws Exception { + this.createTestXmlTypeWithPropertySetterAndPublicMemberAccess(); + + JaxbPersistentClass contextClass = CollectionTools.get(getContextRoot().getPersistentClasses(), 0); + + //public void setFoo(int foo); PUBLIC_MEMBER access - not persistent + assertEquals(0, contextClass.getAttributesSize()); + + //public void setFoo(int foo); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public void setFoo(int foo); FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public void setFoo(int foo); NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public void setFoo(int foo); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + JavaResourceMethod resourceMethod = contextClass.getJavaResourceType().getMethods().iterator().next(); + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //public void setFoo(int foo);; PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + assertEquals(1, contextClass.getAttributesSize()); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public void setFoo(int foo); FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public void setFoo(int foo); NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public void setFoo(int foo); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + } + + public void testUpdateGetterListPropertyAttributes() throws Exception { + this.createTestXmlTypeWithPropertyGetterListAndPublicMemberAccess(); + + JaxbPersistentClass contextClass = CollectionTools.get(getContextRoot().getPersistentClasses(), 0); + + //public List<?> getFoo(); PUBLIC_MEMBER access - persistent + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + assertEquals(1, contextClass.getAttributesSize()); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public List<?> getFoo(); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + attributes = contextClass.getAttributes().iterator(); + assertEquals(1, contextClass.getAttributesSize()); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public List<?> getFoo(); FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public List<?> getFoo(); NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public List<?> getFoo(); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + attributes = contextClass.getAttributes().iterator(); + assertEquals(1, contextClass.getAttributesSize()); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + + //add @XmlAttribute annotation and test each access type + JavaResourceMethod resourceMethod = contextClass.getJavaResourceType().getMethods().iterator().next(); + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //public List<?> getFoo(); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + attributes = contextClass.getAttributes().iterator(); + assertEquals(1, contextClass.getAttributesSize()); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public List<?> getFoo(); FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public List<?> getFoo(); NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public List<?> getFoo(); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + } + + protected void publicGetterSetterTest(JaxbPersistentClass contextClass) { + //public int getFoo();, public void setFoo(int); PUBLIC_MEMBER access - persistent + assertEquals(1, contextClass.getAttributesSize()); + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public int getFoo();, public void setFoo(int); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //public int getFoo();, public void setFoo(int); FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public int getFoo();, public void setFoo(int); NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public int getFoo();, public void setFoo(int); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + + //add @XmlAttribute annotation and test each access type + JavaResourceMethod resourceMethod = ((JaxbPersistentProperty) contextClass.getAttributes().iterator().next()).getResourceGetterMethod(); + AnnotatedElement annotatedElement = this.annotatedElement(resourceMethod); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //public int getFoo();, public void setFoo(int); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public int getFoo();, public void setFoo(int); FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public int getFoo();, public void setFoo(int); NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public int getFoo();, public void setFoo(int); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + } + + protected void publicStaticGetterSetterTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JaxbPersistentProperty contextProperty = (JaxbPersistentProperty) attributes.next(); + JavaResourceMethod resourceGetter = contextProperty.getResourceGetterMethod(); + JavaResourceMethod resourceSetter = contextProperty.getResourceSetterMethod(); + + //public static int getFoo();, public static void setFoo(int); PUBLIC_MEMBER access - not persistent + this.addModifiers(resourceGetter, ModifierKeyword.STATIC_KEYWORD); + this.addModifiers(resourceSetter, ModifierKeyword.STATIC_KEYWORD); + assertEquals(0, contextClass.getAttributesSize()); + + + //public static int getFoo();, public static void setFoo(int); PROPERTY access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int getFoo();, public static void setFoo(int); FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int getFoo();, public static void setFoo(int); NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //public static int getFoo();, public static void setFoo(int); PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + AnnotatedElement annotatedElement = this.annotatedElement(resourceGetter); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //public static int getFoo();, public static void setFoo(int); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public static int getFoo();, public static void setFoo(int); FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public static int getFoo();, public static void setFoo(int); NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //public static int getFoo();, public static void setFoo(int); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + this.removeModifiers(resourceGetter, ModifierKeyword.STATIC_KEYWORD); + this.removeModifiers(resourceSetter, ModifierKeyword.STATIC_KEYWORD); + } + + protected void privateGetterSetterTest(JaxbPersistentClass contextClass) { + Iterator<JaxbPersistentAttribute> attributes = contextClass.getAttributes().iterator(); + JaxbPersistentProperty contextProperty = (JaxbPersistentProperty) attributes.next(); + JavaResourceMethod resourceGetter = contextProperty.getResourceGetterMethod(); + JavaResourceMethod resourceSetter = contextProperty.getResourceSetterMethod(); + + //private int getFoo();, private void setFoo(int); PUBLIC_MEMBER access - not persistent + this.removeModifiers(resourceGetter, ModifierKeyword.PUBLIC_KEYWORD); + this.removeModifiers(resourceSetter, ModifierKeyword.PUBLIC_KEYWORD); + this.addModifiers(resourceGetter, ModifierKeyword.PRIVATE_KEYWORD); + this.addModifiers(resourceSetter, ModifierKeyword.PRIVATE_KEYWORD); + assertEquals(0, contextClass.getAttributesSize()); + + + //private int getFoo();, private void setFoo(int); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //private int getFoo();, private void setFoo(int); FIELD access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(0, contextClass.getAttributesSize()); + + //private int getFoo();, private void setFoo(int); NONE access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(0, contextClass.getAttributesSize()); + + //private int getFoo();, private void setFoo(int); PUBLIC_MEMBER access - not persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(0, contextClass.getAttributesSize()); + + + //add @XmlAttribute annotation and test each access type + AnnotatedElement annotatedElement = this.annotatedElement(resourceGetter); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addMarkerAnnotation(declaration.getDeclaration(), XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + //@XmlAttribute + //private int getFoo();, private void setFoo(int); PROPERTY access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PROPERTY); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private int getFoo();, private void setFoo(int); FIELD access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__FIELD); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private int getFoo();, private void setFoo(int); NONE access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__NONE); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + //@XmlAttribute + //private int getFoo();, private void setFoo(int); PUBLIC_MEMBER access - persistent + this.setAccessTypeInJavaSource(contextClass, JAXB.XML_ACCESS_TYPE__PUBLIC_MEMBER); + assertEquals(1, contextClass.getAttributesSize()); + attributes = contextClass.getAttributes().iterator(); + assertEquals("foo", attributes.next().getName()); + assertFalse(attributes.hasNext()); + + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeAnnotation(declaration, XmlAttributeAnnotation.ANNOTATION_NAME); + } + }); + this.removeModifiers(resourceGetter, ModifierKeyword.PRIVATE_KEYWORD); + this.removeModifiers(resourceSetter, ModifierKeyword.PRIVATE_KEYWORD); + this.addModifiers(resourceGetter, ModifierKeyword.PUBLIC_KEYWORD); + this.addModifiers(resourceSetter, ModifierKeyword.PUBLIC_KEYWORD); + } + + protected void addModifiers(JavaResourceMember resourceMember, final ModifierKeyword... modifiers) { + AnnotatedElement annotatedElement = this.annotatedElement(resourceMember); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.addModifiers((BodyDeclaration) declaration.getDeclaration(), modifiers); + } + }); + } + + protected void removeModifiers(JavaResourceMember resourceMember, final ModifierKeyword... modifiers) { + AnnotatedElement annotatedElement = this.annotatedElement(resourceMember); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.removeModifiers((BodyDeclaration) declaration.getDeclaration(), modifiers); + } + }); + } + + protected void addModifiers(BodyDeclaration bodyDeclaration, ModifierKeyword... modifiers) { + for (ModifierKeyword modifier : modifiers) { + this.modifiers(bodyDeclaration).add(bodyDeclaration.getAST().newModifier(modifier)); + } + } + + protected void removeModifiers(BodyDeclaration bodyDeclaration, ModifierKeyword... modifierKeywords) { + for (ModifierKeyword modifierKeyWord : modifierKeywords) { + for (Iterator<IExtendedModifier> stream = this.modifiers(bodyDeclaration).iterator(); stream.hasNext();) { + IExtendedModifier modifier = stream.next(); + if (modifier.isModifier()) { + if (((Modifier) modifier).getKeyword() == modifierKeyWord) { + stream.remove(); + break; + } + } + } + } + } + + @SuppressWarnings("unchecked") + protected List<IExtendedModifier> modifiers(BodyDeclaration bodyDeclaration) { + return bodyDeclaration.modifiers(); + } + + protected void setAccessTypeInJavaSource(JaxbPersistentClass contextClass, final String accessType) { + JavaResourceType resourceClass = contextClass.getJavaResourceType(); + AnnotatedElement annotatedElement = this.annotatedElement(resourceClass); + annotatedElement.edit(new Member.Editor() { + public void edit(ModifiedDeclaration declaration) { + GenericJavaPersistentClassTests.this.setEnumMemberValuePair(declaration, XmlAccessorTypeAnnotation.ANNOTATION_NAME, accessType); + } + }); + } + }
\ No newline at end of file |