01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.bm.ejb3guice.internal;
16:
17: import static com.bm.ejb3guice.internal.ReferenceType.SOFT;
18: import static com.bm.ejb3guice.internal.ReferenceType.WEAK;
19:
20: import com.bm.ejb3guice.spi.SourceProviders;
21:
22: import java.io.IOException;
23: import java.lang.reflect.Constructor;
24: import java.lang.reflect.Member;
25: import java.util.Map;
26:
27: /**
28: * Creates stack trace elements for members.
29: *
30: * @author crazybob@google.com (Bob Lee)
31: */
32: public class StackTraceElements {
33:
34: static final Map<Class<?>, LineNumbers> lineNumbersCache = new ReferenceCache<Class<?>, LineNumbers>(
35: WEAK, SOFT) {
36: protected LineNumbers create(Class<?> key) {
37: try {
38: return new LineNumbers(key);
39: } catch (IOException e) {
40: throw new RuntimeException(e);
41: }
42: }
43: };
44:
45: public static Object forMember(Member member) {
46: if (member == null) {
47: return SourceProviders.UNKNOWN_SOURCE;
48: }
49:
50: Class declaringClass = member.getDeclaringClass();
51: LineNumbers lineNumbers = lineNumbersCache.get(declaringClass);
52: Integer lineNumber = lineNumbers.getLineNumber(member);
53: String memberName = member instanceof Constructor ? "<init>"
54: : member.getName();
55: StackTraceElement element = new StackTraceElement(
56: declaringClass.getName(), memberName, lineNumbers
57: .getSource(), lineNumber == null ? lineNumbers
58: .getFirstLine() : lineNumber);
59: return element;
60: }
61:
62: public static Object forType(Class<?> implementation) {
63: LineNumbers lineNumbers = lineNumbersCache.get(implementation);
64: return new StackTraceElement(implementation.getName(), "class",
65: lineNumbers.getSource(), lineNumbers.getFirstLine());
66: }
67: }
|