Search in sources :

Example 41 with ImmutableList

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);
}
Also used : Context(com.sun.tools.javac.util.Context) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ImmutableList(com.google.common.collect.ImmutableList) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 42 with ImmutableList

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) URLClassLoader(java.net.URLClassLoader) FilePermission(java.io.FilePermission) Permission(java.security.Permission) JarFile(java.util.jar.JarFile) Files.createFile(java.nio.file.Files.createFile) File(java.io.File) FilePermission(java.io.FilePermission) URL(java.net.URL)

Example 43 with ImmutableList

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));
}
Also used : ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Method(java.lang.reflect.Method)

Example 44 with ImmutableList

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();
}
Also used : WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) ImmutableList(com.google.common.collect.ImmutableList)

Example 45 with ImmutableList

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;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Beta(com.google.common.annotations.Beta)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)552 Path (java.nio.file.Path)130 List (java.util.List)112 SourcePath (com.facebook.buck.rules.SourcePath)91 Test (org.junit.Test)78 Step (com.facebook.buck.step.Step)76 ImmutableMap (com.google.common.collect.ImmutableMap)69 IOException (java.io.IOException)64 ArrayList (java.util.ArrayList)63 Map (java.util.Map)62 ExplicitBuildTargetSourcePath (com.facebook.buck.rules.ExplicitBuildTargetSourcePath)52 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)52 BuildTarget (com.facebook.buck.model.BuildTarget)47 ImmutableSet (com.google.common.collect.ImmutableSet)44 MkdirStep (com.facebook.buck.step.fs.MkdirStep)39 Arguments (com.spectralogic.ds3autogen.api.models.Arguments)38 Optional (java.util.Optional)38 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)36 HumanReadableException (com.facebook.buck.util.HumanReadableException)36 HashMap (java.util.HashMap)35