Skip to content

Commit

Permalink
Merge pull request #34 from intergral/reflection
Browse files Browse the repository at this point in the history
change(reflection): reduce duplicate reflection code and proxy style …
  • Loading branch information
Umaaz committed Sep 25, 2023
2 parents 4631ebb + f745fed commit 7a9d8bd
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

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

import com.intergral.deep.agent.api.reflection.IReflection;

/**
* 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.
*/
Expand All @@ -30,4 +32,11 @@ public interface ISnapshotContext {
* @throws EvaluationException if there were any issues evaluating the expression
*/
String evaluateExpression(String expression) throws EvaluationException;

/**
* Get the current reflection service.
*
* @return the active reflection service.
*/
IReflection reflectionService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public IDeep deepService() {

@Override
public IReflection reflectionService() {
return ReflectionUtils.getReflection();
return Reflection.getInstance();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public DeepAgent(final Settings settings,
* Start deep.
*/
public void start() {
final List<IPlugin> iLoadedPlugins = PluginLoader.loadPlugins(settings, ReflectionUtils.getReflection());
final List<IPlugin> iLoadedPlugins = PluginLoader.loadPlugins(settings, Reflection.getInstance());
final Resource resource = ResourceDetector.configureResource(settings, DeepAgent.class.getClassLoader());
this.settings.setPlugins(iLoadedPlugins);
this.settings.setResource(Resource.DEFAULT.merge(resource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,34 @@

import com.intergral.deep.agent.api.reflection.IReflection;
import com.intergral.deep.reflect.ReflectionImpl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Set;

/**
* A collection of utils that simplify the use of reflection.
*/
public final class ReflectionUtils {
public final class Reflection {

private ReflectionUtils() {
private Reflection() {
}

private static final IReflection reflection;

static {
// we need to change the reflection service we use based on version of java
if (Utils.getJavaVersion() >= 9) {
// if we are version 9 or more. Then use the version 9 reflection
reflection = new com.intergral.deep.reflect.Java9ReflectionImpl();
} else {
// else use the java 8 version
reflection = new ReflectionImpl();
}
}

public static IReflection getReflection() {
/**
* Get the active version of reflection to use.
*
* @return the reflection service.
*/
public static IReflection getInstance() {
return reflection;
}

public static <T> T callMethod(Object target, String methodName, Object... args) {
return getReflection().callMethod(target, methodName, args);
}

public static Method findMethod(Class<?> clazz, String methodName, Class<?>... argTypes) {
return getReflection().findMethod(clazz, methodName, argTypes);
}

public static <T> T getFieldValue(Object target, String fieldName) {
return getReflection().getFieldValue(target, fieldName);
}

public static Iterator<Field> getFieldIterator(final Class<?> clazz) {
return getReflection().getFieldIterator(clazz);
}

public static <T> T callField(final Object target, final Field field) {
return getReflection().callField(target, field);
}

public static Set<String> getModifiers(final Field field) {
return getReflection().getModifiers(field);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package com.intergral.deep.agent.grpc;

import com.intergral.deep.agent.ReflectionUtils;
import com.intergral.deep.agent.Reflection;
import com.intergral.deep.agent.api.auth.AuthProvider;
import com.intergral.deep.agent.api.auth.IAuthProvider;
import com.intergral.deep.agent.api.settings.ISettings;
Expand Down Expand Up @@ -166,7 +166,7 @@ public SnapshotServiceGrpc.SnapshotServiceStub snapshotService() {
}

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

final Metadata metadata = new Metadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

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

import com.intergral.deep.agent.ReflectionUtils;
import com.intergral.deep.agent.Reflection;
import com.intergral.deep.agent.Utils;
import com.intergral.deep.agent.api.plugin.IEvaluator;
import com.intergral.deep.agent.api.reflection.IReflection;
import com.intergral.deep.agent.settings.Settings;
import com.intergral.deep.agent.tracepoint.handler.FrameProcessor;
import com.intergral.deep.agent.types.TracePointConfig;
Expand Down Expand Up @@ -87,6 +88,7 @@ Map<String, Object> mapCFScopes(final Map<String, Object> variables) {
final Object localScope = variables.get("__localScope");

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

// handle var scope
final Object varScope = ReflectionUtils.getFieldValue(pageContext, "SymTab_varScope");
final IReflection reflection = Reflection.getInstance();
final Object varScope = reflection.getFieldValue(pageContext, "SymTab_varScope");

if (CFUtils.isScope(varScope)) {
cfVars.put("VARIABLES", varScope);
}

// find the other build in scopes
final Map<Object, Object> scopes = ReflectionUtils.getFieldValue(pageContext,
final Map<Object, Object> scopes = reflection.getFieldValue(pageContext,
"SymTab_builtinCFScopes");
if (scopes == null) {
return cfVars;
Expand Down Expand Up @@ -146,13 +149,14 @@ Map<String, Object> convertLuceeScopes(final Map<String, Object> variables) {

final Map<String, Object> scopes = new HashMap<>();
// process the scopes from lucee
scopes.put("variables", ReflectionUtils.getFieldValue(param0, "variables"));
scopes.put("argument", ReflectionUtils.getFieldValue(param0, "argument"));
scopes.put("local", getAndCheckLocal("local", param0));
final IReflection reflection = Reflection.getInstance();
scopes.put("variables", reflection.getFieldValue(param0, "variables"));
scopes.put("argument", reflection.getFieldValue(param0, "argument"));
scopes.put("local", getAndCheckLocal(param0));
scopes.put("cookie", getAndCheckScope("cookie", param0));
scopes.put("server", ReflectionUtils.getFieldValue(param0, "server"));
scopes.put("server", reflection.getFieldValue(param0, "server"));
scopes.put("session", getAndCheckScope("session", param0));
scopes.put("application", ReflectionUtils.getFieldValue(param0, "application"));
scopes.put("application", reflection.getFieldValue(param0, "application"));
scopes.put("cgi", getAndCheckScope("cgiR", param0));
scopes.put("request", getAndCheckScope("request", param0));
scopes.put("form", getAndCheckScope("_form", param0));
Expand All @@ -165,15 +169,13 @@ Map<String, Object> convertLuceeScopes(final Map<String, Object> variables) {


/**
* This method will get anc check that the field is a local scope as some parts can have no local
* scope.
* This method will get and check that the field is a local scope as some parts can have no local scope.
*
* @param local the name of the field to look for
* @param param0 the object to look at
* @return {@code null} if the field is not a valid local scope
*/
private Object getAndCheckLocal(final String local, final Object param0) {
final Object o = ReflectionUtils.getFieldValue(param0, local);
private Object getAndCheckLocal(final Object param0) {
final Object o = Reflection.getInstance().getFieldValue(param0, "local");
if (o == null || o.getClass().getName()
.equals("lucee.runtime.type.scope.LocalNotSupportedScope")) {
return null;
Expand All @@ -192,9 +194,10 @@ private Object getAndCheckLocal(final String local, final Object param0) {
* discovered.
*/
private Object getAndCheckScope(final String name, final Object target) {
final Object local = ReflectionUtils.getFieldValue(target, name);
final IReflection reflection = Reflection.getInstance();
final Object local = reflection.getFieldValue(target, name);
if (local != null) {
final Object isInitalized = ReflectionUtils.callMethod(local, "isInitalized");
final Object isInitalized = reflection.callMethod(local, "isInitalized");
if (isInitalized == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

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

import com.intergral.deep.agent.ReflectionUtils;
import com.intergral.deep.agent.Reflection;
import com.intergral.deep.agent.Utils;
import com.intergral.deep.agent.api.plugin.IEvaluator;
import com.intergral.deep.agent.api.reflection.IReflection;
import com.intergral.deep.agent.types.TracePointConfig;
import java.lang.reflect.Method;
import java.net.URL;
Expand Down Expand Up @@ -56,9 +57,10 @@ public static IEvaluator findCfEval(final Map<String, Object> variables) {
return null;
}

Method evaluate = ReflectionUtils.findMethod(page.getClass(), "Evaluate", String.class);
final IReflection reflection = Reflection.getInstance();
Method evaluate = reflection.findMethod(page.getClass(), "Evaluate", String.class);
if (evaluate == null) {
evaluate = ReflectionUtils.findMethod(page.getClass(), "Evaluate", Object.class);
evaluate = reflection.findMethod(page.getClass(), "Evaluate", Object.class);
if (evaluate == null) {
return null;
}
Expand All @@ -73,7 +75,7 @@ private static IEvaluator findLuceeEvaluator(final Map<String, Object> map) {
return null;
}

final Method evaluate = ReflectionUtils.findMethod(param0.getClass(), "evaluate", String.class);
final Method evaluate = Reflection.getInstance().findMethod(param0.getClass(), "evaluate", String.class);
if (evaluate == null) {
return null;
}
Expand All @@ -98,7 +100,7 @@ public static String findUdfName(final Map<String, Object> variables, final Stri
return null;
}
// explicitly set to Object as otherwise the call to String.valueOf can become the char[] version.
return String.valueOf(ReflectionUtils.<Object>getFieldValue(aThis, "key"));
return String.valueOf(Reflection.getInstance().<Object>getFieldValue(aThis, "key"));
} else if (className.startsWith("cf") && className.contains("$func")) {
return className.substring(className.indexOf("$func") + 5);
} else {
Expand Down Expand Up @@ -151,7 +153,7 @@ public static Object findPageContext(final Map<String, Object> localVars) {
if (page == null) {
return null;
}
return ReflectionUtils.getFieldValue(page, "pageContext");
return Reflection.getInstance().getFieldValue(page, "pageContext");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

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

import com.intergral.deep.agent.Reflection;
import com.intergral.deep.agent.Utils;
import com.intergral.deep.agent.api.plugin.EvaluationException;
import com.intergral.deep.agent.api.plugin.IEvaluator;
import com.intergral.deep.agent.api.plugin.ISnapshotContext;
import com.intergral.deep.agent.api.reflection.IReflection;
import com.intergral.deep.agent.api.resource.Resource;
import com.intergral.deep.agent.settings.Settings;
import com.intergral.deep.agent.types.TracePointConfig;
Expand Down Expand Up @@ -162,6 +164,14 @@ public String evaluateExpression(final String expression) throws EvaluationExcep
}
}

/**
* {@inheritDoc}
*/
@Override
public IReflection reflectionService() {
return Reflection.getInstance();
}

/**
* This defines a functional interface to allow for creating difference processors in the Callback.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

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

import com.intergral.deep.agent.ReflectionUtils;
import com.intergral.deep.agent.Reflection;
import com.intergral.deep.agent.Utils;
import com.intergral.deep.agent.api.reflection.IReflection;
import com.intergral.deep.agent.api.utils.ArrayObjectIterator;
import com.intergral.deep.agent.api.utils.CompoundIterator;
import com.intergral.deep.agent.tracepoint.handler.bfs.Node;
Expand Down Expand Up @@ -342,6 +343,7 @@ protected boolean checkDepth(final int depth) {
return depth + 1 < this.frameConfig.maxDepth();
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
protected boolean checkVarCount() {
return varCache.size() <= this.frameConfig.maxVariables();
}
Expand Down Expand Up @@ -496,14 +498,16 @@ public INamedItem next() {
final String name = getFieldName(next);
seenNames.add(next.getName());
return new INamedItem() {
private final IReflection reflection = Reflection.getInstance();

@Override
public String name() {
return name;
}

@Override
public Object item() {
return ReflectionUtils.callField(target, next);
return reflection.callField(target, next);
}

@Override
Expand All @@ -517,7 +521,7 @@ public String originalName() {

@Override
public Set<String> modifiers() {
return ReflectionUtils.getModifiers(next);
return reflection.getModifiers(next);
}
};
}
Expand All @@ -540,20 +544,22 @@ private String getFieldName(final Field next) {
}

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

@SuppressWarnings("FieldCanBeLocal")
private final Class<?> clazz;
private final Iterator<Field> iterator;

public FieldIterator(final Class<?> clazz) {
this.clazz = clazz;
final IReflection reflection = Reflection.getInstance();
if (this.clazz.getSuperclass() == null || this.clazz.getSuperclass() == Object.class) {
this.iterator = ReflectionUtils.getFieldIterator(clazz);
this.iterator = reflection.getFieldIterator(clazz);
} else {
this.iterator = new CompoundIterator<>(ReflectionUtils.getFieldIterator(clazz),
this.iterator = new CompoundIterator<>(reflection.getFieldIterator(clazz),
new FieldIterator(this.clazz.getSuperclass()));
}
}
Expand Down
Loading

0 comments on commit 7a9d8bd

Please sign in to comment.