Skip to content

Commit

Permalink
Renaming of variables and test
Browse files Browse the repository at this point in the history
for #315
  • Loading branch information
pauxus committed Mar 2, 2024
1 parent 560869d commit 75dc252
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@
/** If set, the owner is determined by walking the owner hierarchy up until the given type is found. */
Class<?> providerType() default Object.class;

/**
* If set, determines the strategy to determine which field of the provider is to be used as the link source.
* FIELD_NAME: use the field with the same name as the annotated field, i.e. if the annotated field is called
* 'admin', the field 'admin' of the provider is used.
* INSTANCE_NAME: use the field with the same name as the instance name of the annotated field's owner, i.e. the
* name of the field of the annotated field's classes owner pointing to the instance of the annotated field's container.
* Can only be set together with one of provider or providerType.
*/
@AlsoNeeds({"provider", "providerType"})
Strategy strategy() default Strategy.AUTO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,61 +56,61 @@ static void autoLink(KlumInstanceProxy proxy, Field field, LinkTo linkTo) {
}

static Object determineLinkTarget(KlumInstanceProxy proxy, Field field, LinkTo linkTo) {
Object ownerObject = determineOwnerObject(proxy, linkTo);
if (ownerObject == null) return null;
Object providerObject = determineProviderObject(proxy, linkTo);
if (providerObject == null) return null;

if (!linkTo.field().equals(""))
return InvokerHelper.getProperty(ownerObject, linkTo.field());
if (!linkTo.field().isEmpty())
return InvokerHelper.getProperty(providerObject, linkTo.field());

if (!linkTo.fieldId().equals(""))
return ClusterModel.getSingleValueOrFail(ownerObject, field.getType(), it -> isLinkSourceWithId(it, linkTo.fieldId()));
if (!linkTo.fieldId().isEmpty())
return ClusterModel.getSingleValueOrFail(providerObject, field.getType(), it -> isLinkSourceWithId(it, linkTo.fieldId()));

MetaProperty metaPropertyForFieldName = getFieldNameProperty(field, ownerObject, linkTo);
MetaProperty metaPropertyForInstanceName = getInstanceNameProperty(proxy, ownerObject, linkTo);
MetaProperty metaPropertyForFieldName = getFieldNameProperty(field, providerObject, linkTo);
MetaProperty metaPropertyForInstanceName = getInstanceNameProperty(proxy, providerObject, linkTo);

if (metaPropertyForInstanceName != null && metaPropertyForFieldName != null && !metaPropertyForInstanceName.getName().equals(metaPropertyForFieldName.getName())) {
switch (linkTo.strategy()) {
case INSTANCE_NAME:
return metaPropertyForInstanceName.getProperty(ownerObject);
return metaPropertyForInstanceName.getProperty(providerObject);
case FIELD_NAME:
return metaPropertyForFieldName.getProperty(ownerObject);
return metaPropertyForFieldName.getProperty(providerObject);
default:
throw new IllegalStateException(format("LinkTo annotation on %s#%s targeting %s would match both instance name (%s) and field name (%s). You need to explicitly set a strategy.",
field.getDeclaringClass().getName(), field.getName(), ownerObject.getClass().getName(), metaPropertyForInstanceName.getName(), metaPropertyForFieldName.getName()));
field.getDeclaringClass().getName(), field.getName(), providerObject.getClass().getName(), metaPropertyForInstanceName.getName(), metaPropertyForFieldName.getName()));
}
}

if (metaPropertyForInstanceName != null)
return metaPropertyForInstanceName.getProperty(ownerObject);
return metaPropertyForInstanceName.getProperty(providerObject);
else if (metaPropertyForFieldName != null)
return metaPropertyForFieldName.getProperty(ownerObject);
return metaPropertyForFieldName.getProperty(providerObject);

return ClusterModel.getSingleValueOrFail(ownerObject, field.getType(), it -> !it.isAnnotationPresent(LinkSource.class));
return ClusterModel.getSingleValueOrFail(providerObject, field.getType(), it -> !it.isAnnotationPresent(LinkSource.class));
}

private static boolean isLinkSourceWithId(AnnotatedElement field, String id) {
return field.isAnnotationPresent(LinkSource.class) && field.getAnnotation(LinkSource.class).value().equals(id);
}

static MetaProperty getFieldNameProperty(Field field, Object ownerObject, LinkTo linkTo) {
return InvokerHelper.getMetaClass(ownerObject).getMetaProperty(field.getName() + linkTo.nameSuffix());
static MetaProperty getFieldNameProperty(Field field, Object providerObject, LinkTo linkTo) {
return InvokerHelper.getMetaClass(providerObject).getMetaProperty(field.getName() + linkTo.nameSuffix());
}

static MetaProperty getInstanceNameProperty(KlumInstanceProxy proxy, Object ownerObject, LinkTo linkTo) {
static MetaProperty getInstanceNameProperty(KlumInstanceProxy proxy, Object providerObject, LinkTo linkTo) {
Set<Object> owners = proxy.getOwners();
if (owners.size() != 1) return null;

Object owner = owners.stream().findFirst().orElseThrow(AssertionError::new);

if (owner == ownerObject) return null;
if (owner == providerObject) return null;

return StructureUtil.getPathOfSingleField(owner, proxy.getDSLInstance())
.map(it -> it + linkTo.nameSuffix())
.map(it -> InvokerHelper.getMetaClass(ownerObject).getMetaProperty(it))
.map(it -> InvokerHelper.getMetaClass(providerObject).getMetaProperty(it))
.orElse(null);
}

static Object determineOwnerObject(KlumInstanceProxy proxy, LinkTo linkTo) {
static Object determineProviderObject(KlumInstanceProxy proxy, LinkTo linkTo) {
if (linkTo.provider() != LinkTo.None.class)
return ClosureHelper.invokeClosureWithDelegateAsArgument(linkTo.provider(), proxy.getDSLInstance());
if (linkTo.providerType() != Object.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
@SuppressWarnings('GrPackage')
class AutoLinkDSLTest extends AbstractDSLSpec {

def "auto link default name and owner"() {
def "auto link default name and provider"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -76,7 +76,7 @@ class AutoLinkDSLTest extends AbstractDSLSpec {
instance.services.s3.user.name == 'serviceUser'
}

def "auto link with explicit field name and default owner"() {
def "auto link with explicit field name and default provider"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -120,7 +120,7 @@ class AutoLinkDSLTest extends AbstractDSLSpec {
instance.services.s3.aUser.name == 'serviceUser'
}

def "auto link with fieldId and default owner"() {
def "auto link with fieldId and default provider"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -165,7 +165,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.services.s3.aUser.name == 'serviceUser'
}

def "auto link with no default name, single field and default owner"() {
def "auto link with no default name, single field and default provider"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -208,7 +208,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.services.s3.access.name == 'serviceUser'
}

def "auto link default name and owner closure"() {
def "auto link default name and provider closure"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -256,7 +256,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.consumer.user.is(instance.producer.user)

}
def "auto link implicit instance name strategy and owner closure"() {
def "auto link implicit instance name strategy and provider closure"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -304,7 +304,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.consumer.user.is(instance.producer.consumer)
}

def "auto link explicit instance name strategy and owner closure"() {
def "auto link explicit instance name strategy and provider closure"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -353,7 +353,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.consumer.user.is(instance.producer.consumer)
}

def "auto link explicit instance name strategy and owner closure on class"() {
def "auto link explicit instance name strategy and provider closure on class"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -401,7 +401,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.consumer.user.is(instance.producer.consumer)
}

def "auto link implicit instance name strategy, nameSuffix and owner closure"() {
def "auto link implicit instance name strategy, nameSuffix and provider closure"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -450,7 +450,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.consumer.user.is(instance.producer.consumerUser)
}

def "auto link with owner type"() {
def "auto link with provider type"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -496,7 +496,7 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
instance.container.service.user.is(instance.user)
}

def "determine owner scenarios"() {
def "determine provider scenarios"() {
given:
createClass('''
package tmp
Expand Down Expand Up @@ -545,15 +545,15 @@ import com.blackbuild.klum.ast.util.layer3.annotations.LinkTo
})

then:
LinkHelper.determineOwnerObject(KlumInstanceProxy.getProxyFor(instance), linkTo) == instance
LinkHelper.determineProviderObject(KlumInstanceProxy.getProxyFor(instance), linkTo) == instance

when:
linkTo = withDefaults(GroovyStub(LinkTo) {
provider() >> { container.service2 }.getClass()
})

then:
LinkHelper.determineOwnerObject(KlumInstanceProxy.getProxyFor(instance), linkTo) == instance.container.service2
LinkHelper.determineProviderObject(KlumInstanceProxy.getProxyFor(instance), linkTo) == instance.container.service2
}

LinkTo withDefaults(LinkTo stub) {
Expand Down

0 comments on commit 75dc252

Please sign in to comment.