Skip to content

DATACMNS-853 - Support interface entity types using generated property accessors. #161

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.13.0.BUILD-SNAPSHOT</version>
<version>1.13.0.DATACMNS-853-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,17 @@ private static void visitGetProperty0(PersistentEntity<?, ?> entity, PersistentP
} else {
// bean.get...
mv.visitVarInsn(ALOAD, 2);
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(getter.getDeclaringClass()), getter.getName(),
String.format("()%s", signatureTypeName(getter.getReturnType())), false);

int invokeOpCode = INVOKEVIRTUAL;
Class<?> declaringClass = getter.getDeclaringClass();
boolean interfaceDefinition = declaringClass.isInterface();

if(interfaceDefinition){
invokeOpCode = INVOKEINTERFACE;
}

mv.visitMethodInsn(invokeOpCode, Type.getInternalName(declaringClass), getter.getName(),
String.format("()%s", signatureTypeName(getter.getReturnType())), interfaceDefinition);
autoboxIfNeeded(getter.getReturnType(), autoboxType(getter.getReturnType()), mv);
}
} else {
Expand Down Expand Up @@ -1051,8 +1060,17 @@ private static void visitSetProperty0(PersistentEntity<?, ?> entity, PersistentP
Class<?> parameterType = setter.getParameterTypes()[0];
mv.visitTypeInsn(CHECKCAST, Type.getInternalName(autoboxType(parameterType)));
autoboxIfNeeded(autoboxType(parameterType), parameterType, mv);
mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(setter.getDeclaringClass()), setter.getName(),
String.format("(%s)V", signatureTypeName(parameterType)), false);

int invokeOpCode = INVOKEVIRTUAL;
Class<?> declaringClass = setter.getDeclaringClass();
boolean interfaceDefinition = declaringClass.isInterface();

if(interfaceDefinition){
invokeOpCode = INVOKEINTERFACE;
}

mv.visitMethodInsn(invokeOpCode, Type.getInternalName(setter.getDeclaringClass()), setter.getName(),
String.format("(%s)V", signatureTypeName(parameterType)), interfaceDefinition);
}
} else {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.data.mapping.model;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.io.Serializable;

import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.context.SampleMappingContext;
import org.springframework.data.mapping.context.SamplePersistentProperty;
import org.springframework.data.repository.core.support.PersistentEntityInformation;

/**
* Unit tests for {@link ClassGeneratingPropertyAccessorFactory} covering interface
* and concrete class entity types.
*
* @author John Blum
* @see DATACMNS-809
*/
public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {

private final SampleMappingContext mappingContext = new SampleMappingContext();

protected PersistentEntity<Object, SamplePersistentProperty> getPersistentEntity(Object entity) {
return getPersistentEntity(entity.getClass());
}

protected PersistentEntity<Object, SamplePersistentProperty> getPersistentEntity(Class<?> entityType) {
return mappingContext.getPersistentEntity(entityType);
}

protected PersistentEntityInformation<Object, ?> getPersistentEntityInformation(Object entity) {
return new PersistentEntityInformation<Object, Serializable>(getPersistentEntity(entity));
}

protected PersistentEntityInformation<Object, ?> getPersistentEntityInformation(Class<?> entityType) {
return new PersistentEntityInformation<Object, Serializable>(getPersistentEntity(entityType));
}

@Test
public void getIdentifierOfInterfaceBasedEntity() {
PersistentEntityInformation<Object, ?> quickSortEntityInfo =
getPersistentEntityInformation(Algorithm.class);

Algorithm quickSort = new QuickSort();

assertThat(String.valueOf(quickSortEntityInfo.getId(quickSort)), is(equalTo(quickSort.getName())));
}

@Test
public void getIdentifierOfClassBasedEntity() {
Person jonDoe = Person.newPerson("JonDoe");
PersistentEntityInformation<Object, ?> jonDoeEntityInfo = getPersistentEntityInformation(jonDoe);

assertThat(String.valueOf(jonDoeEntityInfo.getId(jonDoe)), is(equalTo(jonDoe.getName())));
}

interface Algorithm {
@Id String getName();
}

class QuickSort implements Algorithm {
@Override
public String getName() {
return getClass().toString();
}
}

static class Person {

@Id
private final String name;

static Person newPerson(String name) {
return new Person(name);
}

Person(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public String toString() {
return getName();
}
}
}