Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
App Blocks
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Genesys PGR
App Blocks
Commits
8603605c
Commit
8603605c
authored
May 25, 2020
by
Maxym Borodenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extended Copyable interface, introduced @NotCopyable annotation
parent
88a25a5b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
1 deletion
+85
-1
core/src/main/java/org/genesys/blocks/annotations/NotCopyable.java
...main/java/org/genesys/blocks/annotations/NotCopyable.java
+33
-0
core/src/main/java/org/genesys/blocks/model/Copyable.java
core/src/main/java/org/genesys/blocks/model/Copyable.java
+20
-1
core/src/test/java/org/genesys/blocks/tests/model/CopyableTest.java
...est/java/org/genesys/blocks/tests/model/CopyableTest.java
+32
-0
No files found.
core/src/main/java/org/genesys/blocks/annotations/NotCopyable.java
0 → 100644
View file @
8603605c
/*
* 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
{
}
core/src/main/java/org/genesys/blocks/model/Copyable.java
View file @
8603605c
...
...
@@ -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.
...
...
core/src/test/java/org/genesys/blocks/tests/model/CopyableTest.java
View file @
8603605c
...
...
@@ -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
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment