|
| 1 | +package org.evomaster.client.java.instrumentation.coverage.methodreplacement.thirdpartyclasses; |
| 2 | + |
| 3 | +import org.evomaster.client.java.instrumentation.coverage.methodreplacement.Replacement; |
| 4 | +import org.evomaster.client.java.instrumentation.coverage.methodreplacement.ThirdPartyCast; |
| 5 | +import org.evomaster.client.java.instrumentation.coverage.methodreplacement.UsageFilter; |
| 6 | +import org.evomaster.client.java.instrumentation.shared.ReplacementCategory; |
| 7 | +import org.evomaster.client.java.instrumentation.shared.ReplacementType; |
| 8 | + |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +/** |
| 17 | + * Replacement class for instrumenting {@code org.neo4j.driver.Session.run()} methods. |
| 18 | + * Tracks Cypher queries, parameters, and execution time. |
| 19 | + */ |
| 20 | +public class Neo4JSessionClassReplacement extends Neo4JOperationClassReplacement { |
| 21 | + private static final Neo4JSessionClassReplacement singleton = new Neo4JSessionClassReplacement(); |
| 22 | + |
| 23 | + private static final String ID_RUN_STRING = "runString"; |
| 24 | + private static final String ID_RUN_STRING_MAP = "runStringMap"; |
| 25 | + private static final String ID_RUN_STRING_VALUE = "runStringValue"; |
| 26 | + private static final String ID_RUN_STRING_RECORD = "runStringRecord"; |
| 27 | + private static final String ID_RUN_QUERY = "runQuery"; |
| 28 | + |
| 29 | + @Override |
| 30 | + protected String getNameOfThirdPartyTargetClass() { |
| 31 | + return "org.neo4j.driver.Session"; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Replacement for {@code Session.run(String query)}. |
| 36 | + */ |
| 37 | + @Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = ID_RUN_STRING, usageFilter = UsageFilter.ANY, category = ReplacementCategory.NEO4J, castTo = "org.neo4j.driver.Result") |
| 38 | + public static Object run(Object session, String query) { |
| 39 | + return handleRun(ID_RUN_STRING, session, query, null, Collections.singletonList(query)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Replacement for {@code Session.run(String query, Map<String, Object> parameters)}. |
| 44 | + */ |
| 45 | + @Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = ID_RUN_STRING_MAP, usageFilter = UsageFilter.ANY, category = ReplacementCategory.NEO4J, castTo = "org.neo4j.driver.Result") |
| 46 | + public static Object run(Object session, String query, Map<String, Object> parameters) { |
| 47 | + return handleRun(ID_RUN_STRING_MAP, session, query, parameters, Arrays.asList(query, parameters)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Replacement for {@code Session.run(String query, Value parameters)}. |
| 52 | + * Uses _EM_0 suffix to avoid signature conflicts. |
| 53 | + */ |
| 54 | + @Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = ID_RUN_STRING_VALUE, usageFilter = UsageFilter.ANY, category = ReplacementCategory.NEO4J, castTo = "org.neo4j.driver.Result") |
| 55 | + public static Object run_EM_0(Object session, String query, @ThirdPartyCast(actualType = "org.neo4j.driver.Value") Object parameters) { |
| 56 | + return handleRun(ID_RUN_STRING_VALUE, session, query, parameters, Arrays.asList(query, parameters)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Replacement for {@code Session.run(String query, Record parameters)}. |
| 61 | + * Uses _EM_1 suffix to avoid signature conflicts. |
| 62 | + */ |
| 63 | + @Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = ID_RUN_STRING_RECORD, usageFilter = UsageFilter.ANY, category = ReplacementCategory.NEO4J, castTo = "org.neo4j.driver.Result") |
| 64 | + public static Object run_EM_1(Object session, String query, @ThirdPartyCast(actualType = "org.neo4j.driver.Record") Object parameters) { |
| 65 | + return handleRun(ID_RUN_STRING_RECORD, session, query, parameters, Arrays.asList(query, parameters)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Replacement for {@code Session.run(Query query)}. |
| 70 | + */ |
| 71 | + @Replacement(replacingStatic = false, type = ReplacementType.TRACKER, id = ID_RUN_QUERY, usageFilter = UsageFilter.ANY, category = ReplacementCategory.NEO4J, castTo = "org.neo4j.driver.Result") |
| 72 | + public static Object run(Object session, @ThirdPartyCast(actualType = "org.neo4j.driver.Query") Object query) { |
| 73 | + String queryText = extractQueryText(query); |
| 74 | + Object parameters = extractQueryParameters(query); |
| 75 | + return handleRun(ID_RUN_QUERY, session, queryText, parameters, Collections.singletonList(query)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Extracts the Cypher query text from a Query object using reflection. |
| 80 | + * |
| 81 | + * @param queryObject the Query object |
| 82 | + * @return the query text |
| 83 | + * @throws RuntimeException if extraction fails |
| 84 | + */ |
| 85 | + private static String extractQueryText(Object queryObject) { |
| 86 | + try { |
| 87 | + Method textMethod = queryObject.getClass().getMethod("text"); |
| 88 | + return (String) textMethod.invoke(queryObject); |
| 89 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 90 | + throw new RuntimeException("Failed to extract query text from Neo4j Query object", e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Extracts the parameters from a Query object using reflection. |
| 96 | + * |
| 97 | + * @param queryObject the Query object |
| 98 | + * @return the parameters |
| 99 | + * @throws RuntimeException if extraction fails |
| 100 | + */ |
| 101 | + private static Object extractQueryParameters(Object queryObject) { |
| 102 | + try { |
| 103 | + Method parametersMethod = queryObject.getClass().getMethod("parameters"); |
| 104 | + return parametersMethod.invoke(queryObject); |
| 105 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 106 | + throw new RuntimeException("Failed to extract parameters from Neo4j Query object", e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Handles the execution of a run method and tracks the query execution. |
| 112 | + * |
| 113 | + * @param id the method identifier |
| 114 | + * @param session the Session object |
| 115 | + * @param query the Cypher query string |
| 116 | + * @param parameters the query parameters |
| 117 | + * @param args the method arguments |
| 118 | + * @return the Result object |
| 119 | + */ |
| 120 | + private static Object handleRun(String id, Object session, String query, Object parameters, List<Object> args) { |
| 121 | + long start = System.currentTimeMillis(); |
| 122 | + try { |
| 123 | + Method runMethod = retrieveRunMethod(id, session); |
| 124 | + Object result = runMethod.invoke(session, args.toArray()); |
| 125 | + long end = System.currentTimeMillis(); |
| 126 | + handleNeo4J(query, parameters, true, end - start); |
| 127 | + return result; |
| 128 | + } catch (IllegalAccessException e) { |
| 129 | + throw new RuntimeException(e); |
| 130 | + } catch (InvocationTargetException e) { |
| 131 | + Throwable cause = e.getCause(); |
| 132 | + if (cause instanceof RuntimeException) { |
| 133 | + throw (RuntimeException) cause; |
| 134 | + } |
| 135 | + throw new RuntimeException(cause); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Retrieves the original run method from the Session object. |
| 141 | + * |
| 142 | + * @param id the method identifier |
| 143 | + * @param session the Session object |
| 144 | + * @return the original Method |
| 145 | + */ |
| 146 | + private static Method retrieveRunMethod(String id, Object session) { |
| 147 | + return getOriginal(singleton, id, session); |
| 148 | + } |
| 149 | +} |
0 commit comments