Recording API Invocations for Debugging

Today I encountered a problem when using the Oracle JDBC API that only manifested itself in a certain complicated sequence of API invocations. I wanted to understand exactly what the sequence of API invocations was and also be able to extract them, so I could spit them out into a standalone test-case that I could use to debug the problem. Below is a class I whipped up to help automate most of this task.

So now I can do something like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 Invocations t = new Invocations(); 
 PoolDataSource ods= t.trace(PoolDataSourceFactory.getPoolDataSource(),PoolDataSource.class); 
 
 ods.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); 
 ods.setURL("jdbc:oracle:thin://localhost:1521/orcl"); 
 ods.setUser("scott"); ods.setPassword("tiger"); 
 Connection conn = ods.getConnection(); 
 CallableStatement stmt = conn.prepareCall("begin dbms_output.put_line('Hello'); end;"); 
 stmt.execute(); 
 stmt.close(); 
 conn.close(); 
 System.out.print(t); 

Which produces the following output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 oracle.ucp.jdbc.PoolDataSource v1; 
 v1.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); 
 v1.setURL("jdbc:oracle:thin://localhost:1521/orcl"); 
 v1.setUser("scott"); 
 v1.setPassword("tiger"); 
 java.sql.Connection v2; 
 v2 = v1.getConnection(); 
 java.sql.CallableStatement v3; 
 v3 = v2.prepareCall("begin dbms_output.put_line('Hello'); end;"); 
 boolean v4; 
 v4 = v3.execute(); 
 v3.close(); 
 v2.close(); 

Which is a pretty good facsimile of the original code. Note there are obviously limitations to what this kind of approach can achieve:

  • Invocations can only handle primitive values and values created within the API itself
  • Any non primitive values (including arrays) created outside of the API can only be declared, code to initialize these values will need to be added manually.

The major benefit of the generated code is that it does not have any dependencies other than the API being traced, thus making it easier to extract a stand-alone test case that exhibits the problem. This makes it easier to focus on the problem by eradicating superfluous code. It also very useful when you need a stand-alone test-case for a bug report for a third party API.

The Source Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.IdentityHashMap;

public class Invocations {
    @Override
    public String toString() {
        return b.toString();
    }

    public <T> T trace(final Object target, final Class<T> type) {
        return trace(target, type, false);
    }

    private void declare(final Object object, final Class<?> type, final boolean force) {
        if (!void.class.equals(type)) {
            if (force || (object != null && !variables.containsKey(object) && !isPrimitive(type))) {
                b.append(typeName(type));
                b.append(" ");
                b.append(register(object));
                b.append(";\n");
            }
        }
    }

    private boolean isPrimitive(final Class<?> type) {
        boolean isPrimitive = type.isPrimitive();
        if (!isPrimitive) {
            for (final Class<?> c : PRIMITIVES) {
                if (c.equals(type)) {
                    isPrimitive = true;
                    break;
                }
            }
        }
        return isPrimitive;
    }

    private String name(final Object object) {
        String name = "null";
        if (object != null) {
            name = variables.get(object);
            if (name == null) {
                if (isPrimitive(object.getClass())) {
                    return render(object);
                } else {
                    throw new RuntimeException("Could not find name for: " + object);
                }
            }
        }
        return name;
    }

    private String register(final Object object) {
        String name;
        name = variables.get(object);
        if (name == null) {
            name = "v" + (++var);
            variables.put(object, name);
        }
        return name;
    }

    private String render(final Object object) {
        final Class<?> type = object.getClass();
        if (String.class.equals(type)) {
            return "\"" + object.toString() + "\"";
        } else if (short.class.equals(type) || Short.class.equals(type)) {
            return "(short)" + object.toString();
        } else if (long.class.equals(type) || Long.class.equals(type)) {
            return object.toString() + "L";
        } else if (char.class.equals(type) || Character.class.equals(type)) {
            return "'" + object.toString() + "'";
        } else {
            return object.toString();
        }
    }

    @SuppressWarnings("unchecked")
    private <T> T trace(final Object target, final Class<T> type, final boolean force) {
        T result = null;
        if (target != null) {
            if (target.getClass().getInterfaces().length > 0 && !isPrimitive(target.getClass())) {
                result =
                        type.cast(
                                Proxy.newProxyInstance(
                                        Thread.currentThread().getContextClassLoader(),
                                        target.getClass().getInterfaces(),
                                        new Recorder(target)));
            } else {
                result = (T) target;
            }
        }
        declare(result, type, force);
        return result;
    }

    private String typeName(final Class<?> type) {
        if (type.isArray()) {
            return typeName(type.getComponentType()) + "[]";
        } else {
            return type.getName();
        }
    }

    private class Recorder implements InvocationHandler {
        Recorder(final Object target) {
            this.target = target;
        }

        public Object invoke(final Object proxy, final Method method, final Object[] args)
                throws Throwable {
            Object result = null;
            final Class<?> type = method.getReturnType();
            try {
                result = method.invoke(target, args);
            } catch (final InvocationTargetException e) {
                final Throwable t = e.getCause();
                for (final Class<?> ex : method.getExceptionTypes()) {
                    if (ex.isAssignableFrom(t.getClass())) {
                        throw t;
                    }
                }
                throw e;
            }
            result = trace(result, type, true);
            record(result, proxy, method, args);
            return result;
        }

        private void record(
                final Object result, final Object proxy, final Method method, final Object[] args) {
            if (args != null) {
                final Class<?>[] types = method.getParameterTypes();
                for (int i = 0; i < args.length; ++i) {
                    declare(args[i], types[i], false);
                }
            }
            if (result != null) {
                b.append(name(result));
                b.append(" = ");
            }
            b.append(name(proxy));
            b.append('.');
            b.append(method.getName());
            b.append('(');
            if (args != null) {
                for (int i = 0; i < args.length; ++i) {
                    b.append(name(args[i]));
                    if (i < args.length - 1) {
                        b.append(',');
                    }
                }
            }
            b.append(");\n");
        }

        private final Object target;
    }
    ;

    private final StringBuilder b = new StringBuilder();
    private int var = 0;
    private final IdentityHashMap<Object, String> variables = new IdentityHashMap<Object, String>();
    private static final Class<?>[] PRIMITIVES = {
        String.class,
        Integer.class,
        Boolean.class,
        Double.class,
        Long.class,
        Short.class,
        Byte.class,
        Character.class,
        Float.class
    };
}

I’ve only used this code with the specific problem I was facing, I’m sure there are issues with it, I haven’t tested it comprehensively, YMMV. For example I know it doesn’t handle arrays very well, particularly those with multiple dimensions.

Ⓗ Home   Ⓑ Blog   Ⓐ About