SLING-10969 Remove synchronized blocks around injection points (#29)
Also, remove the original cause of SLING-6584, the "reset" of the setAccessible flag.
diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
index 67a4a21..fb6605e 100644
--- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
+++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
@@ -952,20 +952,11 @@
}
Collections.reverse(postConstructMethods);
for (Method method : postConstructMethods) {
- boolean accessible = method.isAccessible();
- try {
- if (!accessible) {
- method.setAccessible(true);
- }
- Object result = method.invoke(object);
- if (result instanceof Boolean && !((Boolean) result).booleanValue()) {
- log.debug("PostConstruct method {}.{} returned false. Returning null model.", method.getDeclaringClass().getName(), method.getName());
- return null;
- }
- } finally {
- if (!accessible) {
- method.setAccessible(false);
- }
+ method.setAccessible(true);
+ Object result = method.invoke(object);
+ if (result instanceof Boolean && !((Boolean) result).booleanValue()) {
+ log.debug("PostConstruct method {}.{} returned false. Returning null model.", method.getDeclaringClass().getName(), method.getName());
+ return null;
}
}
return object;
diff --git a/src/main/java/org/apache/sling/models/impl/model/InjectableField.java b/src/main/java/org/apache/sling/models/impl/model/InjectableField.java
index ccf17d6..161b369 100644
--- a/src/main/java/org/apache/sling/models/impl/model/InjectableField.java
+++ b/src/main/java/org/apache/sling/models/impl/model/InjectableField.java
@@ -37,20 +37,11 @@
}
public RuntimeException set(Object createdObject, Result<Object> result) {
- synchronized (field) {
- boolean accessible = field.isAccessible();
- try {
- if (!accessible) {
- field.setAccessible(true);
- }
- field.set(createdObject, result.getValue());
- } catch (Exception e) {
- return new ModelClassException("Could not inject field due to reflection issues", e);
- } finally {
- if (!accessible) {
- field.setAccessible(false);
- }
- }
+ try {
+ field.setAccessible(true);
+ field.set(createdObject, result.getValue());
+ } catch (Exception e) {
+ return new ModelClassException("Could not inject field due to reflection issues", e);
}
return null;
}
diff --git a/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java b/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
index 5629839..23460be 100644
--- a/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
+++ b/src/main/java/org/apache/sling/models/impl/model/ModelClassConstructor.java
@@ -68,19 +68,8 @@
*/
@SuppressWarnings({"java:S3011","java:S1874"})
public M newInstance(Object... parameters) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- synchronized (constructor) {
- boolean accessible = constructor.isAccessible();
- try {
- if (!accessible) {
- constructor.setAccessible(true);
- }
- return constructor.newInstance(parameters);
- } finally {
- if (!accessible) {
- constructor.setAccessible(false);
- }
- }
- }
+ constructor.setAccessible(true);
+ return constructor.newInstance(parameters);
}
public Constructor<M> getConstructor() {