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 17 package com.google.inject.internal.util; 18 19 import com.google.common.base.Preconditions; 20 import com.google.common.collect.ImmutableSet; 21 import com.google.common.collect.Lists; 22 import java.util.List; 23 24 /** 25 * Provides access to the calling line of code. 26 * 27 * @author crazybob@google.com (Bob Lee) 28 */ 29 public final class SourceProvider { 30 31 /** Indicates that the source is unknown. */ 32 public static final Object UNKNOWN_SOURCE = "[unknown source]"; 33 34 private final SourceProvider parent; 35 private final ImmutableSet<String> classNamesToSkip; 36 37 public static final SourceProvider DEFAULT_INSTANCE = 38 new SourceProvider(ImmutableSet.of(SourceProvider.class.getName())); 39 SourceProvider(Iterable<String> classesToSkip)40 private SourceProvider(Iterable<String> classesToSkip) { 41 this(null, classesToSkip); 42 } 43 SourceProvider(SourceProvider parent, Iterable<String> classesToSkip)44 private SourceProvider(SourceProvider parent, Iterable<String> classesToSkip) { 45 this.parent = parent; 46 47 ImmutableSet.Builder<String> classNamesToSkipBuilder = ImmutableSet.builder(); 48 for (String classToSkip : classesToSkip) { 49 if (parent == null || !parent.shouldBeSkipped(classToSkip)) { 50 classNamesToSkipBuilder.add(classToSkip); 51 } 52 } 53 this.classNamesToSkip = classNamesToSkipBuilder.build(); 54 } 55 56 /** Returns a new instance that also skips {@code moreClassesToSkip}. */ plusSkippedClasses(Class... moreClassesToSkip)57 public SourceProvider plusSkippedClasses(Class... moreClassesToSkip) { 58 return new SourceProvider(this, asStrings(moreClassesToSkip)); 59 } 60 61 /** Returns true if the className should be skipped. */ shouldBeSkipped(String className)62 private boolean shouldBeSkipped(String className) { 63 return (parent != null && parent.shouldBeSkipped(className)) 64 || classNamesToSkip.contains(className); 65 } 66 67 /** Returns the class names as Strings */ asStrings(Class... classes)68 private static List<String> asStrings(Class... classes) { 69 List<String> strings = Lists.newArrayList(); 70 for (Class c : classes) { 71 strings.add(c.getName()); 72 } 73 return strings; 74 } 75 76 /** 77 * Returns the calling line of code. The selected line is the nearest to the top of the stack that 78 * is not skipped. 79 */ get(StackTraceElement[] stackTraceElements)80 public StackTraceElement get(StackTraceElement[] stackTraceElements) { 81 Preconditions.checkNotNull(stackTraceElements, "The stack trace elements cannot be null."); 82 for (final StackTraceElement element : stackTraceElements) { 83 String className = element.getClassName(); 84 85 if (!shouldBeSkipped(className)) { 86 return element; 87 } 88 } 89 throw new AssertionError(); 90 } 91 92 /** Returns the non-skipped module class name. */ getFromClassNames(List<String> moduleClassNames)93 public Object getFromClassNames(List<String> moduleClassNames) { 94 Preconditions.checkNotNull(moduleClassNames, "The list of module class names cannot be null."); 95 for (final String moduleClassName : moduleClassNames) { 96 if (!shouldBeSkipped(moduleClassName)) { 97 return new StackTraceElement(moduleClassName, "configure", null, -1); 98 } 99 } 100 return UNKNOWN_SOURCE; 101 } 102 } 103