Commit 8603605c authored by Maxym Borodenko's avatar Maxym Borodenko

Extended Copyable interface, introduced @NotCopyable annotation

parent 88a25a5b
/*
* Copyright 2018 Global Crop Diversity Trust
*
* 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.genesys.blocks.annotations;
import org.genesys.blocks.model.Copyable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Add this annotation to exclude a field from a list of copyable fields in {@link Copyable}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.TYPE })
public @interface NotCopyable {
}
......@@ -23,9 +23,16 @@ import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.genesys.blocks.annotations.NotCopyable;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldFilter;
import javax.persistence.Id;
/**
* The Interface Copyable.
*
......@@ -101,7 +108,19 @@ public interface Copyable<T> {
// or map
|| Map.class.isAssignableFrom(field.getType())
// or array
|| field.getClass().isArray());
|| field.getClass().isArray()
// or has @Id
|| field.isAnnotationPresent(Id.class)
// or has @CreatedBy
|| field.isAnnotationPresent(CreatedBy.class)
// or has @LastModifiedBy
|| field.isAnnotationPresent(LastModifiedBy.class)
// or has @CreatedDate
|| field.isAnnotationPresent(CreatedDate.class)
// or has @LastModifiedDate
|| field.isAnnotationPresent(LastModifiedDate.class)
// or has @NotCopyable
|| field.isAnnotationPresent(NotCopyable.class));
/**
* Utility method that makes a deep copy of {@link Copyable} elements.
......
......@@ -23,14 +23,22 @@ import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.model.Copyable;
import org.junit.Test;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.Id;
/**
* The Class CopyableTest.
......@@ -69,6 +77,18 @@ public class CopyableTest {
}
private static class Child extends Parent implements Copyable<Child> {
@Id
private Long myId;
@CreatedBy
private Long myCreatedBy;
@LastModifiedBy
private Long myLastModifiedBy;
@CreatedDate
private Date myCreatedDate;
@LastModifiedDate
private Date myLastModifiedDate;
@NotCopyable
private String myNotCopyable;
private int myInt;
int myDefaultInt;
final int myFinalInt;
......@@ -106,6 +126,12 @@ public class CopyableTest {
source.protectedInt = 3;
source.publicInt = 4;
source.myInt = 5;
source.myId = 1L;
source.myCreatedBy = 2L;
source.myLastModifiedBy = 3L;
source.myCreatedDate = new Date();
source.myLastModifiedDate = new Date();
source.myNotCopyable = "test string";
source.setPrivateInt(6);
source.myDefaultInt = 7;
source.put("test1");
......@@ -117,6 +143,12 @@ public class CopyableTest {
final Child copy = source.copy();
assertThat(Parent.STATIC_INT, is(33));
assertThat(copy.myId, not(source.myId));
assertThat(copy.myCreatedBy, not(source.myCreatedBy));
assertThat(copy.myLastModifiedBy, not(source.myLastModifiedBy));
assertThat(copy.myCreatedDate, not(source.myCreatedDate));
assertThat(copy.myLastModifiedDate, not(source.myLastModifiedDate));
assertThat(copy.myNotCopyable, not(source.myNotCopyable));
assertThat(copy.finalInt, is(42));
assertThat(copy.defaultInt, is(source.defaultInt));
assertThat(copy.protectedInt, is(source.protectedInt));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment