use of com.google.common.collect.ImmutableList in project error-prone by google.
the class BlockTemplate method replace.
@Override
public Fix replace(BlockTemplateMatch match) {
checkNotNull(match);
SuggestedFix.Builder fix = SuggestedFix.builder();
Inliner inliner = match.createInliner();
Context context = inliner.getContext();
if (annotations().containsKey(UseImportPolicy.class)) {
ImportPolicy.bind(context, annotations().getInstance(UseImportPolicy.class).value());
} else {
ImportPolicy.bind(context, ImportPolicy.IMPORT_TOP_LEVEL);
}
ImmutableList<JCStatement> targetStatements = match.getStatements();
try {
ImmutableList.Builder<JCStatement> inlinedStatementsBuilder = ImmutableList.builder();
for (UStatement statement : templateStatements()) {
inlinedStatementsBuilder.addAll(statement.inlineStatements(inliner));
}
ImmutableList<JCStatement> inlinedStatements = inlinedStatementsBuilder.build();
int nInlined = inlinedStatements.size();
int nTargets = targetStatements.size();
if (nInlined <= nTargets) {
for (int i = 0; i < nInlined; i++) {
fix.replace(targetStatements.get(i), printStatement(context, inlinedStatements.get(i)));
}
for (int i = nInlined; i < nTargets; i++) {
fix.delete(targetStatements.get(i));
}
} else {
for (int i = 0; i < nTargets - 1; i++) {
fix.replace(targetStatements.get(i), printStatement(context, inlinedStatements.get(i)));
}
int last = nTargets - 1;
ImmutableList<JCStatement> remainingInlined = inlinedStatements.subList(last, nInlined);
fix.replace(targetStatements.get(last), CharMatcher.whitespace().trimTrailingFrom(printStatements(context, remainingInlined)));
}
} catch (CouldNotResolveImportException e) {
logger.log(SEVERE, "Failure to resolve import in replacement", e);
}
return addImports(inliner, fix);
}
use of com.google.common.collect.ImmutableList in project guava by google.
the class ClassPathTest method doTestExistsThrowsSecurityException.
private void doTestExistsThrowsSecurityException() throws IOException, URISyntaxException {
URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
URL[] urls = myLoader.getURLs();
ImmutableList.Builder<File> filesBuilder = ImmutableList.builder();
for (URL url : urls) {
if (url.getProtocol().equalsIgnoreCase("file")) {
filesBuilder.add(new File(url.toURI()));
}
}
ImmutableList<File> files = filesBuilder.build();
assertThat(files).isNotEmpty();
SecurityManager disallowFilesSecurityManager = new SecurityManager() {
@Override
public void checkPermission(Permission p) {
if (p instanceof FilePermission) {
throw new SecurityException("Disallowed: " + p);
}
}
};
System.setSecurityManager(disallowFilesSecurityManager);
try {
files.get(0).exists();
fail("Did not get expected SecurityException");
} catch (SecurityException expected) {
}
ClassPath classPath = ClassPath.from(myLoader);
assertThat(classPath.getResources()).isEmpty();
}
use of com.google.common.collect.ImmutableList in project guava by google.
the class TypeTokenTest method testMethod_declaredBySuperclass.
public void testMethod_declaredBySuperclass() throws Exception {
Method toStringMethod = Object.class.getMethod("toString");
ImmutableList<String> list = ImmutableList.of("foo");
assertEquals(list.toString(), TypeToken.of(List.class).method(toStringMethod).invoke(list));
}
use of com.google.common.collect.ImmutableList in project guava by google.
the class TypeToken method getGenericInterfaces.
/**
* Returns the generic interfaces that this type directly {@code implements}. This method is
* similar but different from {@link Class#getGenericInterfaces()}. For example, {@code new
* TypeToken<List<String>>() {}.getGenericInterfaces()} will return a list that contains
* {@code new TypeToken<Iterable<String>>() {}}; while {@code List.class.getGenericInterfaces()}
* will return an array that contains {@code Iterable<T>}, where the {@code T} is the type
* variable declared by interface {@code Iterable}.
*
* <p>If this type is a type variable or wildcard, its upper bounds are examined and those that
* are either an interface or upper-bounded only by interfaces are returned. This means that the
* returned types could include type variables too.
*/
final ImmutableList<TypeToken<? super T>> getGenericInterfaces() {
if (runtimeType instanceof TypeVariable) {
return boundsAsInterfaces(((TypeVariable<?>) runtimeType).getBounds());
}
if (runtimeType instanceof WildcardType) {
return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds());
}
ImmutableList.Builder<TypeToken<? super T>> builder = ImmutableList.builder();
for (Type interfaceType : getRawType().getGenericInterfaces()) {
// interface of T
@SuppressWarnings("unchecked") TypeToken<? super T> resolvedInterface = (TypeToken<? super T>) resolveSupertype(interfaceType);
builder.add(resolvedInterface);
}
return builder.build();
}
use of com.google.common.collect.ImmutableList in project guava by google.
the class Futures method inCompletionOrder.
/**
* Returns a list of delegate futures that correspond to the futures received in the order that
* they complete. Delegate futures return the same value or throw the same exception as the
* corresponding input future returns/throws.
*
* <p>"In the order that they complete" means, for practical purposes, about what you would
* expect, but there are some subtleties. First, we do guarantee that, if the output future at
* index n is done, the output future at index n-1 is also done. (But as usual with futures, some
* listeners for future n may complete before some for future n-1.) However, it is possible, if
* one input completes with result X and another later with result Y, for Y to come before X in
* the output future list. (Such races are impossible to solve without global synchronization of
* all future completions. And they should have little practical impact.)
*
* <p>Cancelling a delegate future has no effect on any input future, since the delegate future
* does not correspond to a specific input future until the appropriate number of input futures
* have completed. At that point, it is too late to cancel the input future. The input future's
* result, which cannot be stored into the cancelled delegate future, is ignored.
*
* @since 17.0
*/
@Beta
public static <T> ImmutableList<ListenableFuture<T>> inCompletionOrder(Iterable<? extends ListenableFuture<? extends T>> futures) {
ImmutableList<ListenableFuture<? extends T>> copy = ImmutableList.copyOf(futures);
ImmutableList.Builder<SettableFuture<T>> delegatesBuilder = ImmutableList.builder();
for (int i = 0; i < copy.size(); i++) {
delegatesBuilder.add(SettableFuture.create());
}
final ImmutableList<SettableFuture<T>> delegates = delegatesBuilder.build();
final AtomicInteger delegateIndex = new AtomicInteger();
for (final ListenableFuture<? extends T> future : copy) {
future.addListener(new Runnable() {
@Override
public void run() {
for (int i = delegateIndex.get(); i < delegates.size(); i++) {
if (delegates.get(i).setFuture(future)) {
// this is technically unnecessary, but should speed up later accesses
delegateIndex.set(i + 1);
return;
}
}
// if we get here it means that one of the output futures was cancelled
// nothing we can do
}
}, directExecutor());
}
@SuppressWarnings("unchecked") ImmutableList<ListenableFuture<T>> delegatesCast = (ImmutableList) delegates;
return delegatesCast;
}
Aggregations