Skip to content

Commit 19c06ff

Browse files
authored
Merge pull request #17 from KoenDG/infrecur
Infinite recursion fix + static code analysis
2 parents 2b4b5e5 + 46d5caa commit 19c06ff

File tree

10 files changed

+35
-29
lines changed

10 files changed

+35
-29
lines changed

src/main/java/io/ebean/BeanCapture.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@ public class BeanCapture {
1717
/**
1818
* The captured beans sent to the save() methods.
1919
*/
20-
public List<Object> save = new ArrayList<Object>();
20+
public List<Object> save = new ArrayList<>();
2121

2222
/**
2323
* The captured beans sent to the insert() methods.
2424
*/
25-
public List<Object> insert = new ArrayList<Object>();
25+
public List<Object> insert = new ArrayList<>();
2626

2727
/**
2828
* The captured beans sent to the update() methods.
2929
*/
30-
public List<Object> update = new ArrayList<Object>();
30+
public List<Object> update = new ArrayList<>();
3131

3232
/**
3333
* The captured beans sent to the delete() methods.
3434
* <p/>
3535
* Note that these can include MethodCall objects for the cases when
3636
* delete by id is called.
3737
*/
38-
public List<Object> delete = new ArrayList<Object>();
38+
public List<Object> delete = new ArrayList<>();
3939

4040
/**
4141
* Captured beans sent to deletePermanent() methods.
4242
*/
43-
public List<Object> deletePermanent = new ArrayList<Object>();
43+
public List<Object> deletePermanent = new ArrayList<>();
4444

4545

4646
protected void addSaved(Object bean) {

src/main/java/io/ebean/MethodCall.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public class MethodCall {
1212

1313
String name;
1414

15-
Map<String,Object> args = new LinkedHashMap<String, Object>();
15+
Map<String, Object> args = new LinkedHashMap<>();
1616

1717
public MethodCall(String name) {
1818
this.name = name;
1919
}
2020

2121
public String toString() {
22-
return name+":"+args;
22+
return name + ':' + args;
2323
}
2424

2525

src/main/java/io/ebean/MethodCalls.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
*/
99
public class MethodCalls {
1010

11-
12-
13-
List<MethodCall> list = new ArrayList<MethodCall>();
11+
List<MethodCall> list = new ArrayList<>();
1412

1513
public void add(MethodCall call) {
1614
list.add(call);
@@ -42,7 +40,7 @@ public List<MethodCall> delete() {
4240

4341
protected List<MethodCall> matches(String methodName) {
4442

45-
List<MethodCall> matches = new ArrayList<MethodCall>();
43+
List<MethodCall> matches = new ArrayList<>();
4644
for (MethodCall call : list) {
4745
if (isMatch(call, methodName)) {
4846
matches.add(call);
@@ -51,7 +49,7 @@ protected List<MethodCall> matches(String methodName) {
5149
return matches;
5250
}
5351

54-
protected boolean isMatch(MethodCall call, String methodName) {
52+
protected static boolean isMatch(MethodCall call, String methodName) {
5553
return call.name.equals(methodName);
5654
}
5755
}

src/main/java/io/ebean/WhenFind.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111
public class WhenFind {
1212

13-
List<WhenBeanReturn<?>> byId = new ArrayList();
13+
List<WhenBeanReturn<?>> byId = new ArrayList<>();
1414

15-
Map<Class<?>,WhenBeanReturn<?>> byUnique = new HashMap<Class<?>,WhenBeanReturn<?>>();
15+
Map<Class<?>, WhenBeanReturn<?>> byUnique = new HashMap<>();
1616

1717
public <T> WhenBeanReturn<T> byId(Class<T> beanType) {
1818

@@ -21,14 +21,14 @@ public <T> WhenBeanReturn<T> byId(Class<T> beanType) {
2121

2222
public <T> WhenBeanReturn<T> byId(Class<T> beanType, Object id) {
2323

24-
WhenBeanReturn<T> ret = new WhenBeanReturn<T>(beanType, id);
24+
WhenBeanReturn<T> ret = new WhenBeanReturn<>(beanType, id);
2525
byId.add(ret);
2626
return ret;
2727
}
2828

2929
public <T> WhenBeanReturn<T> byUnique(Class<T> beanType) {
3030

31-
WhenBeanReturn<T> ret = new WhenBeanReturn<T>(beanType);
31+
WhenBeanReturn<T> ret = new WhenBeanReturn<>(beanType);
3232
byUnique.put(beanType, ret);
3333
return ret;
3434
}

src/main/java/io/ebean/WithStaticFinder.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.lang.reflect.Field;
44
import java.lang.reflect.Modifier;
5+
import java.security.AccessController;
6+
import java.security.PrivilegedAction;
57

68
/**
79
* Used to replace a "Finder" that is located as a static field (typically on an Model entity bean).
@@ -11,7 +13,7 @@
1113
*/
1214
public class WithStaticFinder<T> {
1315

14-
Class<T> beanType;
16+
final Class<T> beanType;
1517

1618
String fieldName;
1719

@@ -41,7 +43,7 @@ public WithStaticFinder(Class<T> beanType, String fieldName) {
4143
* <p/>
4244
* Note that the test double instance is not set until <code>useTestDouble()</code> is called.
4345
*/
44-
public WithStaticFinder as(Object testDouble) throws FinderFieldNotFoundException {
46+
public WithStaticFinder<T> as(Object testDouble) throws FinderFieldNotFoundException {
4547

4648
try {
4749
this.testDouble = testDouble;
@@ -50,7 +52,16 @@ public WithStaticFinder as(Object testDouble) throws FinderFieldNotFoundExceptio
5052
this.field.setAccessible(true);
5153
try {
5254
Field modifiersField = Field.class.getDeclaredField("modifiers");
53-
modifiersField.setAccessible(true);
55+
56+
/**
57+
* If the project using this library has a SecurityManager set up, permission may be denied.
58+
* Therefor, running this as a privileged action.
59+
*/
60+
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
61+
modifiersField.setAccessible(true);
62+
return null;
63+
});
64+
5465
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
5566
} catch (NoSuchFieldException e) {
5667
throw new RuntimeException(e);

src/main/java/io/ebean/WithStaticFinders.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
*/
1010
public class WithStaticFinders {
1111

12-
protected List<WithStaticFinder<?>> staticFinderReplacement = new ArrayList<WithStaticFinder<?>>();
12+
protected List<WithStaticFinder<?>> staticFinderReplacement = new ArrayList<>();
1313

1414
/**
1515
* Create and return a WithStaticFinder. Expects a static 'finder' field on the beanType class.
1616
*/
1717
public <T> WithStaticFinder<T> withFinder(Class<T> beanType) {
18-
WithStaticFinder withFinder = new WithStaticFinder<T>(beanType);
18+
WithStaticFinder<T> withFinder = new WithStaticFinder<>(beanType);
1919
staticFinderReplacement.add(withFinder);
2020
return withFinder;
2121
}

src/main/java/io/ebean/delegate/DelegateDelete.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ public int deleteAllPermanent(Class<?> beanType, Collection<?> ids) {
9090

9191
@Override
9292
public int deleteAllPermanent(Class<?> beanType, Collection<?> ids, Transaction transaction) {
93-
return deleteAllPermanent(beanType, ids, transaction);
93+
return delegate.deleteAllPermanent(beanType, ids, transaction);
9494
}
9595
}

src/main/java/io/ebean/delegate/DelegateFind.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ public <T> void findEachWhile(Query<T> query, Predicate<T> consumer, Transaction
8888

8989
@Override
9090
public <T> List<T> findList(Query<T> query, Transaction transaction) {
91-
92-
SpiQuery q = (SpiQuery)query;
93-
q.getBeanType();
9491
return delegate.findList(query, transaction);
9592
}
9693

src/main/java/io/ebeantest/LoggedSql.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
public class LoggedSql {
2323

24-
private static BasicAppender basicAppender = new BasicAppender();
24+
private static final BasicAppender basicAppender = new BasicAppender();
2525

2626
static {
2727

@@ -52,7 +52,7 @@ public static List<String> stop() {
5252

5353
private static class BasicAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
5454

55-
List<String> messages = new ArrayList<String>();
55+
List<String> messages = new ArrayList<>();
5656

5757
@Override
5858
protected void append(ILoggingEvent eventObject) {

src/test/java/io/ebean/MockiEbeanTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void testRunWithMock() throws Exception {
3636
public void run() {
3737
Object value = Ebean.getServer(null).getBeanId(null);
3838

39-
assertEquals(magicBeanId, value);
39+
assertEquals(value, magicBeanId);
4040
}
4141
});
4242

@@ -59,7 +59,7 @@ public void testRunWithMock_when_exceptionThrow_should_restoreServer() throws Ex
5959
public void run() {
6060
Object value = Ebean.getServer(null).getBeanId(null);
6161

62-
assertEquals(magicBeanId, value);
62+
assertEquals(value, magicBeanId);
6363
throw new IllegalStateException();
6464
}
6565
});

0 commit comments

Comments
 (0)