Skip to content

Commit 7a9d8bd

Browse files
authored
Merge pull request #34 from intergral/reflection
change(reflection): reduce duplicate reflection code and proxy style …
2 parents 4631ebb + f745fed commit 7a9d8bd

File tree

11 files changed

+84
-69
lines changed

11 files changed

+84
-69
lines changed

agent-api/src/main/java/com/intergral/deep/agent/api/plugin/ISnapshotContext.java

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.intergral.deep.agent.api.plugin;
1919

20+
import com.intergral.deep.agent.api.reflection.IReflection;
21+
2022
/**
2123
* This is the context passed to plugins. This allows for the data of a context to be exposed to the plugin in a controlled manor.
2224
*/
@@ -30,4 +32,11 @@ public interface ISnapshotContext {
3032
* @throws EvaluationException if there were any issues evaluating the expression
3133
*/
3234
String evaluateExpression(String expression) throws EvaluationException;
35+
36+
/**
37+
* Get the current reflection service.
38+
*
39+
* @return the active reflection service.
40+
*/
41+
IReflection reflectionService();
3342
}

agent/src/main/java/com/intergral/deep/agent/AgentImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public IDeep deepService() {
9191

9292
@Override
9393
public IReflection reflectionService() {
94-
return ReflectionUtils.getReflection();
94+
return Reflection.getInstance();
9595
}
9696
};
9797
}

agent/src/main/java/com/intergral/deep/agent/DeepAgent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public DeepAgent(final Settings settings,
7676
* Start deep.
7777
*/
7878
public void start() {
79-
final List<IPlugin> iLoadedPlugins = PluginLoader.loadPlugins(settings, ReflectionUtils.getReflection());
79+
final List<IPlugin> iLoadedPlugins = PluginLoader.loadPlugins(settings, Reflection.getInstance());
8080
final Resource resource = ResourceDetector.configureResource(settings, DeepAgent.class.getClassLoader());
8181
this.settings.setPlugins(iLoadedPlugins);
8282
this.settings.setResource(Resource.DEFAULT.merge(resource));

agent/src/main/java/com/intergral/deep/agent/ReflectionUtils.java renamed to agent/src/main/java/com/intergral/deep/agent/Reflection.java

+11-31
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,34 @@
1919

2020
import com.intergral.deep.agent.api.reflection.IReflection;
2121
import com.intergral.deep.reflect.ReflectionImpl;
22-
import java.lang.reflect.Field;
23-
import java.lang.reflect.Method;
24-
import java.util.Iterator;
25-
import java.util.Set;
2622

2723
/**
2824
* A collection of utils that simplify the use of reflection.
2925
*/
30-
public final class ReflectionUtils {
26+
public final class Reflection {
3127

32-
private ReflectionUtils() {
28+
private Reflection() {
3329
}
3430

3531
private static final IReflection reflection;
3632

3733
static {
34+
// we need to change the reflection service we use based on version of java
3835
if (Utils.getJavaVersion() >= 9) {
36+
// if we are version 9 or more. Then use the version 9 reflection
3937
reflection = new com.intergral.deep.reflect.Java9ReflectionImpl();
4038
} else {
39+
// else use the java 8 version
4140
reflection = new ReflectionImpl();
4241
}
4342
}
4443

45-
public static IReflection getReflection() {
44+
/**
45+
* Get the active version of reflection to use.
46+
*
47+
* @return the reflection service.
48+
*/
49+
public static IReflection getInstance() {
4650
return reflection;
4751
}
48-
49-
public static <T> T callMethod(Object target, String methodName, Object... args) {
50-
return getReflection().callMethod(target, methodName, args);
51-
}
52-
53-
public static Method findMethod(Class<?> clazz, String methodName, Class<?>... argTypes) {
54-
return getReflection().findMethod(clazz, methodName, argTypes);
55-
}
56-
57-
public static <T> T getFieldValue(Object target, String fieldName) {
58-
return getReflection().getFieldValue(target, fieldName);
59-
}
60-
61-
public static Iterator<Field> getFieldIterator(final Class<?> clazz) {
62-
return getReflection().getFieldIterator(clazz);
63-
}
64-
65-
public static <T> T callField(final Object target, final Field field) {
66-
return getReflection().callField(target, field);
67-
}
68-
69-
public static Set<String> getModifiers(final Field field) {
70-
return getReflection().getModifiers(field);
71-
}
7252
}

agent/src/main/java/com/intergral/deep/agent/grpc/GrpcService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.intergral.deep.agent.grpc;
1919

20-
import com.intergral.deep.agent.ReflectionUtils;
20+
import com.intergral.deep.agent.Reflection;
2121
import com.intergral.deep.agent.api.auth.AuthProvider;
2222
import com.intergral.deep.agent.api.auth.IAuthProvider;
2323
import com.intergral.deep.agent.api.settings.ISettings;
@@ -166,7 +166,7 @@ public SnapshotServiceGrpc.SnapshotServiceStub snapshotService() {
166166
}
167167

168168
private Metadata buildMetaData() {
169-
final IAuthProvider provider = AuthProvider.provider(this.settings, ReflectionUtils.getReflection());
169+
final IAuthProvider provider = AuthProvider.provider(this.settings, Reflection.getInstance());
170170
final Map<String, String> headers = provider.provide();
171171

172172
final Metadata metadata = new Metadata();

agent/src/main/java/com/intergral/deep/agent/tracepoint/cf/CFFrameProcessor.java

+18-15
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
package com.intergral.deep.agent.tracepoint.cf;
1919

20-
import com.intergral.deep.agent.ReflectionUtils;
20+
import com.intergral.deep.agent.Reflection;
2121
import com.intergral.deep.agent.Utils;
2222
import com.intergral.deep.agent.api.plugin.IEvaluator;
23+
import com.intergral.deep.agent.api.reflection.IReflection;
2324
import com.intergral.deep.agent.settings.Settings;
2425
import com.intergral.deep.agent.tracepoint.handler.FrameProcessor;
2526
import com.intergral.deep.agent.types.TracePointConfig;
@@ -87,6 +88,7 @@ Map<String, Object> mapCFScopes(final Map<String, Object> variables) {
8788
final Object localScope = variables.get("__localScope");
8889

8990
if (CFUtils.isScope(localScope)) {
91+
//noinspection unchecked,rawtypes
9092
final Map<Object, Object> lclMap = (Map) localScope;
9193
for (Map.Entry<Object, Object> entry : lclMap.entrySet()) {
9294
final Object name = entry.getKey();
@@ -102,14 +104,15 @@ Map<String, Object> mapCFScopes(final Map<String, Object> variables) {
102104
}
103105

104106
// handle var scope
105-
final Object varScope = ReflectionUtils.getFieldValue(pageContext, "SymTab_varScope");
107+
final IReflection reflection = Reflection.getInstance();
108+
final Object varScope = reflection.getFieldValue(pageContext, "SymTab_varScope");
106109

107110
if (CFUtils.isScope(varScope)) {
108111
cfVars.put("VARIABLES", varScope);
109112
}
110113

111114
// find the other build in scopes
112-
final Map<Object, Object> scopes = ReflectionUtils.getFieldValue(pageContext,
115+
final Map<Object, Object> scopes = reflection.getFieldValue(pageContext,
113116
"SymTab_builtinCFScopes");
114117
if (scopes == null) {
115118
return cfVars;
@@ -146,13 +149,14 @@ Map<String, Object> convertLuceeScopes(final Map<String, Object> variables) {
146149

147150
final Map<String, Object> scopes = new HashMap<>();
148151
// process the scopes from lucee
149-
scopes.put("variables", ReflectionUtils.getFieldValue(param0, "variables"));
150-
scopes.put("argument", ReflectionUtils.getFieldValue(param0, "argument"));
151-
scopes.put("local", getAndCheckLocal("local", param0));
152+
final IReflection reflection = Reflection.getInstance();
153+
scopes.put("variables", reflection.getFieldValue(param0, "variables"));
154+
scopes.put("argument", reflection.getFieldValue(param0, "argument"));
155+
scopes.put("local", getAndCheckLocal(param0));
152156
scopes.put("cookie", getAndCheckScope("cookie", param0));
153-
scopes.put("server", ReflectionUtils.getFieldValue(param0, "server"));
157+
scopes.put("server", reflection.getFieldValue(param0, "server"));
154158
scopes.put("session", getAndCheckScope("session", param0));
155-
scopes.put("application", ReflectionUtils.getFieldValue(param0, "application"));
159+
scopes.put("application", reflection.getFieldValue(param0, "application"));
156160
scopes.put("cgi", getAndCheckScope("cgiR", param0));
157161
scopes.put("request", getAndCheckScope("request", param0));
158162
scopes.put("form", getAndCheckScope("_form", param0));
@@ -165,15 +169,13 @@ Map<String, Object> convertLuceeScopes(final Map<String, Object> variables) {
165169

166170

167171
/**
168-
* This method will get anc check that the field is a local scope as some parts can have no local
169-
* scope.
172+
* This method will get and check that the field is a local scope as some parts can have no local scope.
170173
*
171-
* @param local the name of the field to look for
172174
* @param param0 the object to look at
173175
* @return {@code null} if the field is not a valid local scope
174176
*/
175-
private Object getAndCheckLocal(final String local, final Object param0) {
176-
final Object o = ReflectionUtils.getFieldValue(param0, local);
177+
private Object getAndCheckLocal(final Object param0) {
178+
final Object o = Reflection.getInstance().getFieldValue(param0, "local");
177179
if (o == null || o.getClass().getName()
178180
.equals("lucee.runtime.type.scope.LocalNotSupportedScope")) {
179181
return null;
@@ -192,9 +194,10 @@ private Object getAndCheckLocal(final String local, final Object param0) {
192194
* discovered.
193195
*/
194196
private Object getAndCheckScope(final String name, final Object target) {
195-
final Object local = ReflectionUtils.getFieldValue(target, name);
197+
final IReflection reflection = Reflection.getInstance();
198+
final Object local = reflection.getFieldValue(target, name);
196199
if (local != null) {
197-
final Object isInitalized = ReflectionUtils.callMethod(local, "isInitalized");
200+
final Object isInitalized = reflection.callMethod(local, "isInitalized");
198201
if (isInitalized == null) {
199202
return null;
200203
}

agent/src/main/java/com/intergral/deep/agent/tracepoint/cf/CFUtils.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
package com.intergral.deep.agent.tracepoint.cf;
1919

20-
import com.intergral.deep.agent.ReflectionUtils;
20+
import com.intergral.deep.agent.Reflection;
2121
import com.intergral.deep.agent.Utils;
2222
import com.intergral.deep.agent.api.plugin.IEvaluator;
23+
import com.intergral.deep.agent.api.reflection.IReflection;
2324
import com.intergral.deep.agent.types.TracePointConfig;
2425
import java.lang.reflect.Method;
2526
import java.net.URL;
@@ -56,9 +57,10 @@ public static IEvaluator findCfEval(final Map<String, Object> variables) {
5657
return null;
5758
}
5859

59-
Method evaluate = ReflectionUtils.findMethod(page.getClass(), "Evaluate", String.class);
60+
final IReflection reflection = Reflection.getInstance();
61+
Method evaluate = reflection.findMethod(page.getClass(), "Evaluate", String.class);
6062
if (evaluate == null) {
61-
evaluate = ReflectionUtils.findMethod(page.getClass(), "Evaluate", Object.class);
63+
evaluate = reflection.findMethod(page.getClass(), "Evaluate", Object.class);
6264
if (evaluate == null) {
6365
return null;
6466
}
@@ -73,7 +75,7 @@ private static IEvaluator findLuceeEvaluator(final Map<String, Object> map) {
7375
return null;
7476
}
7577

76-
final Method evaluate = ReflectionUtils.findMethod(param0.getClass(), "evaluate", String.class);
78+
final Method evaluate = Reflection.getInstance().findMethod(param0.getClass(), "evaluate", String.class);
7779
if (evaluate == null) {
7880
return null;
7981
}
@@ -98,7 +100,7 @@ public static String findUdfName(final Map<String, Object> variables, final Stri
98100
return null;
99101
}
100102
// explicitly set to Object as otherwise the call to String.valueOf can become the char[] version.
101-
return String.valueOf(ReflectionUtils.<Object>getFieldValue(aThis, "key"));
103+
return String.valueOf(Reflection.getInstance().<Object>getFieldValue(aThis, "key"));
102104
} else if (className.startsWith("cf") && className.contains("$func")) {
103105
return className.substring(className.indexOf("$func") + 5);
104106
} else {
@@ -151,7 +153,7 @@ public static Object findPageContext(final Map<String, Object> localVars) {
151153
if (page == null) {
152154
return null;
153155
}
154-
return ReflectionUtils.getFieldValue(page, "pageContext");
156+
return Reflection.getInstance().getFieldValue(page, "pageContext");
155157
}
156158

157159

agent/src/main/java/com/intergral/deep/agent/tracepoint/handler/FrameProcessor.java

+10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
package com.intergral.deep.agent.tracepoint.handler;
1919

20+
import com.intergral.deep.agent.Reflection;
2021
import com.intergral.deep.agent.Utils;
2122
import com.intergral.deep.agent.api.plugin.EvaluationException;
2223
import com.intergral.deep.agent.api.plugin.IEvaluator;
2324
import com.intergral.deep.agent.api.plugin.ISnapshotContext;
25+
import com.intergral.deep.agent.api.reflection.IReflection;
2426
import com.intergral.deep.agent.api.resource.Resource;
2527
import com.intergral.deep.agent.settings.Settings;
2628
import com.intergral.deep.agent.types.TracePointConfig;
@@ -162,6 +164,14 @@ public String evaluateExpression(final String expression) throws EvaluationExcep
162164
}
163165
}
164166

167+
/**
168+
* {@inheritDoc}
169+
*/
170+
@Override
171+
public IReflection reflectionService() {
172+
return Reflection.getInstance();
173+
}
174+
165175
/**
166176
* This defines a functional interface to allow for creating difference processors in the Callback.
167177
*/

agent/src/main/java/com/intergral/deep/agent/tracepoint/handler/VariableProcessor.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
package com.intergral.deep.agent.tracepoint.handler;
1919

20-
import com.intergral.deep.agent.ReflectionUtils;
20+
import com.intergral.deep.agent.Reflection;
2121
import com.intergral.deep.agent.Utils;
22+
import com.intergral.deep.agent.api.reflection.IReflection;
2223
import com.intergral.deep.agent.api.utils.ArrayObjectIterator;
2324
import com.intergral.deep.agent.api.utils.CompoundIterator;
2425
import com.intergral.deep.agent.tracepoint.handler.bfs.Node;
@@ -342,6 +343,7 @@ protected boolean checkDepth(final int depth) {
342343
return depth + 1 < this.frameConfig.maxDepth();
343344
}
344345

346+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
345347
protected boolean checkVarCount() {
346348
return varCache.size() <= this.frameConfig.maxVariables();
347349
}
@@ -496,14 +498,16 @@ public INamedItem next() {
496498
final String name = getFieldName(next);
497499
seenNames.add(next.getName());
498500
return new INamedItem() {
501+
private final IReflection reflection = Reflection.getInstance();
502+
499503
@Override
500504
public String name() {
501505
return name;
502506
}
503507

504508
@Override
505509
public Object item() {
506-
return ReflectionUtils.callField(target, next);
510+
return reflection.callField(target, next);
507511
}
508512

509513
@Override
@@ -517,7 +521,7 @@ public String originalName() {
517521

518522
@Override
519523
public Set<String> modifiers() {
520-
return ReflectionUtils.getModifiers(next);
524+
return reflection.getModifiers(next);
521525
}
522526
};
523527
}
@@ -540,20 +544,22 @@ private String getFieldName(final Field next) {
540544
}
541545

542546
/**
543-
* A simple iterator that used {@link ReflectionUtils} to create iterators for the {@link Field} that exist on an object. This allows us
547+
* A simple iterator that used {@link Reflection} to create iterators for the {@link Field} that exist on an object. This allows us
544548
* to feed the fields into the {@link NamedFieldIterator}.
545549
*/
546550
private static class FieldIterator implements Iterator<Field> {
547551

552+
@SuppressWarnings("FieldCanBeLocal")
548553
private final Class<?> clazz;
549554
private final Iterator<Field> iterator;
550555

551556
public FieldIterator(final Class<?> clazz) {
552557
this.clazz = clazz;
558+
final IReflection reflection = Reflection.getInstance();
553559
if (this.clazz.getSuperclass() == null || this.clazz.getSuperclass() == Object.class) {
554-
this.iterator = ReflectionUtils.getFieldIterator(clazz);
560+
this.iterator = reflection.getFieldIterator(clazz);
555561
} else {
556-
this.iterator = new CompoundIterator<>(ReflectionUtils.getFieldIterator(clazz),
562+
this.iterator = new CompoundIterator<>(reflection.getFieldIterator(clazz),
557563
new FieldIterator(this.clazz.getSuperclass()));
558564
}
559565
}

0 commit comments

Comments
 (0)