Skip to content

Commit a232b38

Browse files
authored
Merge pull request #267 from eclipse-rcptt/merge/master/release/2.8
Merge release/2.8 into master
2 parents cb691e8 + e82ac86 commit a232b38

4 files changed

Lines changed: 63 additions & 19 deletions

File tree

core/org.eclipse.rcptt.tesla.core/src/org/eclipse/rcptt/tesla/core/TeslaFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public TeslaFeatures() {
200200
.showIn(TeslaFeatures.ADV_OPTIONS, TeslaFeatures.CP_OPTIONS);
201201

202202
option(ENABLE_PROTECTED_MEMBERS)
203-
.name("Use protected members in Advanced properties")
203+
.name("Use protected members in Advanced properties (deprecated)")
204204
.description(
205205
"Enable assertions for protected fields and methods")
206206
.value("false").defaultValue("false").values(BOOLEAN_VALUES)

core/org.eclipse.rcptt.util/src/org/eclipse/rcptt/util/ReflectionUtil.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public static Object callMethod(Object instance, String name,
3434
return null;
3535
}
3636

37-
method.setAccessible(true);
3837
try {
3938
return method.invoke(instance,
4039
args.toArray(new Object[args.size()]));
@@ -103,7 +102,6 @@ public static Object getField(Object instance, String name, boolean checked)
103102
}
104103
return null;
105104
}
106-
field.setAccessible(true);
107105
try {
108106
return field.get(instance);
109107
} catch (IllegalAccessException e) {
@@ -126,12 +124,16 @@ public static Field findField(Class<?> clazz, String name) {
126124
return null;
127125
}
128126
try {
129-
return clazz.getDeclaredField(name);
127+
Field result = clazz.getDeclaredField(name);
128+
if (result.trySetAccessible()) {
129+
return result;
130+
}
130131
} catch (SecurityException e) {
131132
return null;
132133
} catch (NoSuchFieldException e) {
133-
return findField(clazz.getSuperclass(), name);
134+
// check superclass
134135
}
136+
return findField(clazz.getSuperclass(), name);
135137
}
136138

137139
public static Method findMethod(Class<?> clazz, String name) {
@@ -145,13 +147,17 @@ public static Method findMethod(Class<?> clazz, String name,
145147
}
146148

147149
try {
148-
return clazz.getDeclaredMethod(name,
150+
Method result = clazz.getDeclaredMethod(name,
149151
params.toArray(new Class<?>[params.size()]));
150-
} catch (NoSuchMethodException e) {
151-
return findMethod(clazz.getSuperclass(), name, params);
152+
if (result.trySetAccessible()) {
153+
return result;
154+
}
152155
} catch (SecurityException e) {
153156
return null;
157+
} catch (NoSuchMethodException e) {
158+
// check superclass
154159
}
160+
return findMethod(clazz.getSuperclass(), name, params);
155161
}
156162

157163
}

runtime/tesla/org.eclipse.rcptt.tesla.swt/src/org/eclipse/rcptt/tesla/swt/reflection/JavaMembersHelper.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,16 @@ private static MembersContainer collectObjectMembers(Object object) {
297297
}
298298

299299
private static void collectMembers(Class<?> clazz, MembersContainer acc) {
300-
for (Field field : clazz.getDeclaredFields()) {
301-
if (checkField(field)) {
302-
field.setAccessible(true);
303-
acc.addField(field);
300+
if ((clazz.getModifiers() & PUBLIC) != 0 || (isProtectedEnabled() && (clazz.getModifiers() & PROTECTED) != 0)) {
301+
for (Field field : clazz.getDeclaredFields()) {
302+
if (checkField(field) && field.trySetAccessible()) {
303+
acc.addField(field);
304+
}
304305
}
305-
}
306-
for (Method method : clazz.getDeclaredMethods()) {
307-
if (checkMethod(method)) {
308-
method.setAccessible(true);
309-
acc.addMethod(method);
306+
for (Method method : clazz.getDeclaredMethods()) {
307+
if (checkMethod(method) && method.trySetAccessible()) {
308+
acc.addMethod(method);
309+
}
310310
}
311311
}
312312
Class<?> parent = clazz.getSuperclass();
@@ -525,7 +525,6 @@ PropertyValue getValue(Object object) throws IllegalArgumentException,
525525
}
526526
Field field = ReflectionUtil.findField(object.getClass(),
527527
memberName);
528-
field.setAccessible(true);
529528
Object value = field.get(object);
530529
return new PropertyValue(object, value, memberName, field.getType());
531530
}
@@ -547,7 +546,6 @@ PropertyValue getValue(Object object) throws IllegalArgumentException,
547546

548547
Method method = ReflectionUtil.findMethod(object.getClass(),
549548
memberName);
550-
method.setAccessible(true);
551549
Object value = method.invoke(object);
552550
return new PropertyValue(object, value, memberName,
553551
method.getReturnType());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Xored Software Inc and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Xored Software Inc - initial API and implementation and/or initial documentation
10+
*******************************************************************************/
11+
package org.eclipse.rcptt.tesla.swt.reflection;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.util.List;
17+
import java.util.Set;
18+
19+
import org.eclipse.emf.common.util.BasicEList;
20+
import org.eclipse.emf.common.util.EList;
21+
import org.eclipse.rcptt.tesla.core.ui.PropertyNode;
22+
import org.junit.Test;
23+
24+
public class JavaMembersHelperTest {
25+
26+
@Test
27+
public void collectProperties() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
28+
Set<Integer> protectedObject = Set.of(4);
29+
EList<PropertyNode> nodes = new BasicEList<PropertyNode>();
30+
JavaMembersHelper.fillProperties(protectedObject, nodes); // should not throw
31+
// Should use size() method from a non-private super class
32+
assertEquals(List.of("1"), nodes.stream().filter(n -> n.getName().equals("size()")).map(PropertyNode::getValue).toList() );
33+
nodes.clear();
34+
JavaMembersHelper.fillProperties(protectedObject, "size()", nodes); // should not throw
35+
36+
assertEquals("1", nodes.stream().filter(n -> n.getName().equals("toString()")).findFirst().get().getValue());
37+
assertEquals("1", JavaMembersHelper.getPropertyValue(protectedObject, "size()"));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)