chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
kevinb9n | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 17 | package com.google.inject.internal; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 18 | |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 19 | import static com.google.inject.internal.Preconditions.checkArgument; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
kevinb9n | 2394ca6 | 2007-04-20 14:33:40 +0000 | [diff] [blame] | 22 | import java.lang.reflect.Member; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 23 | import java.util.Map; |
kevinb9n | 2394ca6 | 2007-04-20 14:33:40 +0000 | [diff] [blame] | 24 | import org.objectweb.asm.AnnotationVisitor; |
| 25 | import org.objectweb.asm.Attribute; |
| 26 | import org.objectweb.asm.ClassReader; |
| 27 | import org.objectweb.asm.ClassVisitor; |
| 28 | import org.objectweb.asm.FieldVisitor; |
| 29 | import org.objectweb.asm.Label; |
| 30 | import org.objectweb.asm.MethodVisitor; |
| 31 | import org.objectweb.asm.Opcodes; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 32 | |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Looks up line numbers for classes and their members. |
| 35 | * |
| 36 | * @author Chris Nokleberg |
| 37 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 38 | final class LineNumbers { |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 39 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 40 | private final Class type; |
| 41 | private final Map<String, Integer> lines = Maps.newHashMap(); |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 42 | private String source; |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 43 | private int firstLine = Integer.MAX_VALUE; |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Reads line number information from the given class, if available. |
| 47 | * |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 48 | * @param type the class to read line number information from |
| 49 | * @throws IllegalArgumentException if the bytecode for the class cannot be found |
| 50 | * @throws java.io.IOException if an error occurs while reading bytecode |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 51 | */ |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 52 | public LineNumbers(Class type) throws IOException { |
| 53 | this.type = type; |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 54 | |
| 55 | if (!type.isArray()) { |
| 56 | InputStream in = type.getResourceAsStream("/" + type.getName().replace('.', '/') + ".class"); |
| 57 | checkArgument(in != null, "Cannot find bytecode for %s", type); |
| 58 | new ClassReader(in).accept(new LineNumberReader(), ClassReader.SKIP_FRAMES); |
| 59 | } |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the source file name as read from the bytecode. |
| 64 | * |
| 65 | * @return the source file name if available, or null |
| 66 | */ |
| 67 | public String getSource() { |
| 68 | return source; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the line number associated with the given member. |
| 73 | * |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 74 | * @param member a field, constructor, or method belonging to the class used during construction |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 75 | * @return the wrapped line number, or null if not available |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 76 | * @throws IllegalArgumentException if the member does not belong to the class used during |
| 77 | * construction |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 78 | */ |
| 79 | public Integer getLineNumber(Member member) { |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 80 | checkArgument(type == member.getDeclaringClass(), |
| 81 | "Member %s belongs to %s, not %s", member, member.getDeclaringClass(), type); |
| 82 | return lines.get(MoreTypes.memberKey(member)); |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 83 | } |
| 84 | |
limpbizkit | 564053f | 2008-06-15 11:21:18 +0000 | [diff] [blame] | 85 | /** Gets the first line number. */ |
crazyboblee | dedcabd | 2007-02-20 03:59:14 +0000 | [diff] [blame] | 86 | public int getFirstLine() { |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 87 | return firstLine == Integer.MAX_VALUE ? 1 : firstLine; |
crazyboblee | dedcabd | 2007-02-20 03:59:14 +0000 | [diff] [blame] | 88 | } |
| 89 | |
chris.nokleberg | 409df52 | 2007-03-20 04:39:42 +0000 | [diff] [blame] | 90 | private class LineNumberReader implements ClassVisitor, MethodVisitor, AnnotationVisitor { |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 91 | |
| 92 | private int line = -1; |
| 93 | private String pendingMethod; |
| 94 | private String name; |
| 95 | |
| 96 | public void visit(int version, int access, String name, String signature, |
| 97 | String superName, String[] interfaces) { |
| 98 | this.name = name; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 99 | } |
| 100 | |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 101 | public MethodVisitor visitMethod(int access, String name, String desc, |
| 102 | String signature, String[] exceptions) { |
| 103 | if ((access & Opcodes.ACC_PRIVATE) != 0) { |
| 104 | return null; |
| 105 | } |
| 106 | pendingMethod = name + desc; |
| 107 | line = -1; |
| 108 | return this; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 109 | } |
| 110 | |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 111 | public void visitSource(String source, String debug) { |
| 112 | LineNumbers.this.source = source; |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 113 | } |
| 114 | |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 115 | public void visitLineNumber(int line, Label start) { |
crazyboblee | 0f09fe3 | 2007-02-23 23:43:57 +0000 | [diff] [blame] | 116 | if (line < firstLine) { |
crazyboblee | dedcabd | 2007-02-20 03:59:14 +0000 | [diff] [blame] | 117 | firstLine = line; |
| 118 | } |
| 119 | |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 120 | this.line = line; |
| 121 | if (pendingMethod != null) { |
| 122 | lines.put(pendingMethod, line); |
| 123 | pendingMethod = null; |
| 124 | } |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 125 | } |
| 126 | |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 127 | public void visitFieldInsn(int opcode, String owner, String name, |
| 128 | String desc) { |
| 129 | if (opcode == Opcodes.PUTFIELD && this.name.equals(owner) |
| 130 | && !lines.containsKey(name) && line != -1) { |
| 131 | lines.put(name, line); |
| 132 | } |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 133 | } |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 134 | |
| 135 | public void visitEnd() { |
| 136 | } |
| 137 | |
| 138 | public void visitInnerClass(String name, String outerName, String innerName, |
| 139 | int access) { |
| 140 | } |
| 141 | |
| 142 | public void visitOuterClass(String owner, String name, String desc) { |
| 143 | } |
| 144 | |
| 145 | public void visitAttribute(Attribute attr) { |
| 146 | } |
| 147 | |
| 148 | public FieldVisitor visitField(int access, String name, String desc, |
| 149 | String signature, Object value) { |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { |
chris.nokleberg | 409df52 | 2007-03-20 04:39:42 +0000 | [diff] [blame] | 154 | return this; |
| 155 | } |
| 156 | |
| 157 | public AnnotationVisitor visitAnnotation(String name, String desc) { |
| 158 | return this; |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public AnnotationVisitor visitAnnotationDefault() { |
chris.nokleberg | 409df52 | 2007-03-20 04:39:42 +0000 | [diff] [blame] | 162 | return this; |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public AnnotationVisitor visitParameterAnnotation(int parameter, |
| 166 | String desc, boolean visible) { |
chris.nokleberg | 409df52 | 2007-03-20 04:39:42 +0000 | [diff] [blame] | 167 | return this; |
| 168 | } |
| 169 | |
| 170 | public AnnotationVisitor visitArray(String name) { |
| 171 | return this; |
| 172 | } |
| 173 | |
| 174 | public void visitEnum(String name, String desc, String value) { |
| 175 | } |
| 176 | |
| 177 | public void visit(String name, Object value) { |
crazyboblee | 7173353 | 2007-02-14 02:35:21 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public void visitCode() { |
| 181 | } |
| 182 | |
| 183 | public void visitFrame(int type, int nLocal, Object[] local, int nStack, |
| 184 | Object[] stack) { |
| 185 | } |
| 186 | |
| 187 | public void visitIincInsn(int var, int increment) { |
| 188 | } |
| 189 | |
| 190 | public void visitInsn(int opcode) { |
| 191 | } |
| 192 | |
| 193 | public void visitIntInsn(int opcode, int operand) { |
| 194 | } |
| 195 | |
| 196 | public void visitJumpInsn(int opcode, Label label) { |
| 197 | } |
| 198 | |
| 199 | public void visitLabel(Label label) { |
| 200 | } |
| 201 | |
| 202 | public void visitLdcInsn(Object cst) { |
| 203 | } |
| 204 | |
| 205 | public void visitLocalVariable(String name, String desc, String signature, |
| 206 | Label start, Label end, int index) { |
| 207 | } |
| 208 | |
| 209 | public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { |
| 210 | } |
| 211 | |
| 212 | public void visitMaxs(int maxStack, int maxLocals) { |
| 213 | } |
| 214 | |
| 215 | public void visitMethodInsn(int opcode, String owner, String name, |
| 216 | String desc) { |
| 217 | } |
| 218 | |
| 219 | public void visitMultiANewArrayInsn(String desc, int dims) { |
| 220 | } |
| 221 | |
| 222 | public void visitTableSwitchInsn(int min, int max, Label dflt, |
| 223 | Label[] labels) { |
| 224 | } |
| 225 | |
| 226 | public void visitTryCatchBlock(Label start, Label end, Label handler, |
| 227 | String type) { |
| 228 | } |
| 229 | |
| 230 | public void visitTypeInsn(int opcode, String desc) { |
| 231 | } |
| 232 | |
| 233 | public void visitVarInsn(int opcode, int var) { |
| 234 | } |
| 235 | } |
chris.nokleberg | 5db87c6 | 2007-02-13 21:16:24 +0000 | [diff] [blame] | 236 | } |