From 3aceb2449157482a3600059cab6ab1f990766dca Mon Sep 17 00:00:00 2001 From: Luke Sandberg Date: Fri, 10 Jan 2025 09:36:12 -0800 Subject: [PATCH] Adopt java 11 in guice builds Use `var` in a few places to prove it out. PiperOrigin-RevId: 714080340 --- BUILD | 2 +- build_defs.bzl | 4 ++-- core/src/com/google/inject/BUILD | 1 + .../google/inject/internal/InjectorImpl.java | 17 ++++++++--------- .../inject/internal/InjectorJitBindingData.java | 8 +++++--- .../internal/aop/AbstractGlueGenerator.java | 7 +++++-- .../internal/aop/AnonymousClassDefiner.java | 1 + .../inject/internal/aop/HiddenClassDefiner.java | 1 + .../inject/internal/aop/UnsafeGetter.java | 1 + examples/guice-demo/pom.xml | 4 ++-- pom-template.xml | 2 +- tools/OsgiWrapper.java | 2 +- 12 files changed, 29 insertions(+), 21 deletions(-) diff --git a/BUILD b/BUILD index 7c065b2d83..c632756301 100644 --- a/BUILD +++ b/BUILD @@ -33,7 +33,7 @@ javadoc_library( ], doctitle = "Guice Dependency Injection API", external_javadoc_links = [ - "https://docs.oracle.com/javase/8/docs/api/", + "https://docs.oracle.com/javase/11/docs/api/", "https://guava.dev/releases/snapshot-jre/api/docs/", "https://google.github.io/truth/api/latest/", "http://errorprone.info/api/latest/", diff --git a/build_defs.bzl b/build_defs.bzl index 1aa149141b..b9745bd529 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -16,9 +16,9 @@ JAVAC_OPTS = [ "-Xdoclint:html,syntax", "-source", - "1.8", + "11", "-target", - "1.8", + "11", ] POM_VERSION = "${project.version}" diff --git a/core/src/com/google/inject/BUILD b/core/src/com/google/inject/BUILD index d15096d96d..cf473e92e6 100644 --- a/core/src/com/google/inject/BUILD +++ b/core/src/com/google/inject/BUILD @@ -95,6 +95,7 @@ java_library( ":caller_finder_impl", ":implemented_by", ":provided_by", + "//java/com/google/devtools/staticanalysis/errorprone/annotations:suppress_do_not_use_unsafe_memory_access", "//third_party/java/aopalliance", "//third_party/java/asm", "//third_party/java/error_prone:annotations", diff --git a/core/src/com/google/inject/internal/InjectorImpl.java b/core/src/com/google/inject/internal/InjectorImpl.java index 40d3addcca..8d15efd94e 100644 --- a/core/src/com/google/inject/internal/InjectorImpl.java +++ b/core/src/com/google/inject/internal/InjectorImpl.java @@ -150,7 +150,7 @@ enum JitLimitation { @Override public List> findBindingsByType(TypeLiteral type) { @SuppressWarnings("unchecked") // safe because we only put matching entries into the map - List> list = + var list = (List>) (List) bindingData.getIndexedExplicitBindings().get(checkNotNull(type, "type")); return Collections.unmodifiableList(list); @@ -161,7 +161,7 @@ public List> findBindingsByType(TypeLiteral type) { public BindingImpl getBinding(Key key) { Errors errors = new Errors(checkNotNull(key, "key")); try { - BindingImpl result = getBindingOrThrow(key, errors, JitLimitation.EXISTING_JIT); + var result = getBindingOrThrow(key, errors, JitLimitation.EXISTING_JIT); errors.throwConfigurationExceptionIfErrorsExist(); return result; } catch (ErrorsException e) { @@ -175,15 +175,14 @@ public BindingImpl getBinding(Key key) { @Override public BindingImpl getExistingBinding(Key key) { // Check explicit bindings, i.e. bindings created by modules. - BindingImpl explicitBinding = bindingData.getExplicitBinding(checkNotNull(key, "key")); + var explicitBinding = bindingData.getExplicitBinding(checkNotNull(key, "key")); if (explicitBinding != null) { return explicitBinding; } synchronized (jitBindingData.lock()) { // See if any jit bindings have been created for this key. for (InjectorImpl injector = this; injector != null; injector = injector.parent) { - @SuppressWarnings("unchecked") - BindingImpl jitBinding = (BindingImpl) injector.jitBindingData.getJitBinding(key); + var jitBinding = injector.jitBindingData.getJitBinding(key); if (jitBinding != null) { return jitBinding; } @@ -196,7 +195,7 @@ public BindingImpl getExistingBinding(Key key) { try { // This is safe because isProvider above ensures that T is a Provider @SuppressWarnings("unchecked") - Key providedKey = (Key) getProvidedKey((Key) key, new Errors()); + var providedKey = (Key) getProvidedKey((Key) key, new Errors()); if (getExistingBinding(providedKey) != null) { return getBinding(key); } @@ -268,8 +267,8 @@ private BindingImpl getJustInTimeBinding(Key key, Errors errors, JitLi synchronized (jitBindingData.lock()) { // first try to find a JIT binding that we've already created for (InjectorImpl injector = this; injector != null; injector = injector.parent) { - @SuppressWarnings("unchecked") // we only store bindings that match their key - BindingImpl binding = (BindingImpl) injector.jitBindingData.getJitBinding(key); + + var binding = injector.jitBindingData.getJitBinding(key); if (binding != null) { // If we found a JIT binding and we don't allow them, @@ -634,7 +633,7 @@ private boolean cleanup(BindingImpl binding, Set> encountered) { Key depKey = dep.getKey(); InjectionPoint ip = dep.getInjectionPoint(); if (encountered.add(depKey)) { // only check if we haven't looked at this key yet - BindingImpl depBinding = jitBindingData.getJitBinding(depKey); + var depBinding = jitBindingData.getJitBinding(depKey); if (depBinding != null) { // if the binding still exists, validate boolean failed = cleanup(depBinding, encountered); // if children fail, we fail if (depBinding instanceof ConstructorBindingImpl) { diff --git a/core/src/com/google/inject/internal/InjectorJitBindingData.java b/core/src/com/google/inject/internal/InjectorJitBindingData.java index 07a747a067..0d78dafad1 100644 --- a/core/src/com/google/inject/internal/InjectorJitBindingData.java +++ b/core/src/com/google/inject/internal/InjectorJitBindingData.java @@ -45,11 +45,13 @@ Map, BindingImpl> getJitBindings() { return Collections.unmodifiableMap(jitBindings); } - BindingImpl getJitBinding(Key key) { - return jitBindings.get(key); + BindingImpl getJitBinding(Key key) { + @SuppressWarnings("unchecked") // safe because putJitBinding maintains this relationship + BindingImpl binding = (BindingImpl) jitBindings.get(key); + return binding; } - void putJitBinding(Key key, BindingImpl binding) { + void putJitBinding(Key key, BindingImpl binding) { jitBindings.put(key, binding); } diff --git a/core/src/com/google/inject/internal/aop/AbstractGlueGenerator.java b/core/src/com/google/inject/internal/aop/AbstractGlueGenerator.java index 09abda9374..8b76db9798 100644 --- a/core/src/com/google/inject/internal/aop/AbstractGlueGenerator.java +++ b/core/src/com/google/inject/internal/aop/AbstractGlueGenerator.java @@ -142,8 +142,11 @@ private static Function> bindSignat return signature -> { try { // pass this signature's index into the table function to retrieve the invoker - return (BiFunction) - invokerTable.invokeExact(signatureTable.applyAsInt(signature)); + @SuppressWarnings("unchecked") + var invoker = + (BiFunction) + invokerTable.invokeExact(signatureTable.applyAsInt(signature)); + return invoker; } catch (Throwable e) { throw asIfUnchecked(e); } diff --git a/core/src/com/google/inject/internal/aop/AnonymousClassDefiner.java b/core/src/com/google/inject/internal/aop/AnonymousClassDefiner.java index ec43a6c3d3..cc9fe49267 100644 --- a/core/src/com/google/inject/internal/aop/AnonymousClassDefiner.java +++ b/core/src/com/google/inject/internal/aop/AnonymousClassDefiner.java @@ -23,6 +23,7 @@ * * @author mcculls@gmail.com (Stuart McCulloch) */ +@SuppressWarnings("SunApi") final class AnonymousClassDefiner implements ClassDefiner { private static final sun.misc.Unsafe THE_UNSAFE; diff --git a/core/src/com/google/inject/internal/aop/HiddenClassDefiner.java b/core/src/com/google/inject/internal/aop/HiddenClassDefiner.java index 8fb2a900c5..a63ec59533 100644 --- a/core/src/com/google/inject/internal/aop/HiddenClassDefiner.java +++ b/core/src/com/google/inject/internal/aop/HiddenClassDefiner.java @@ -26,6 +26,7 @@ * * @author mcculls@gmail.com (Stuart McCulloch) */ +@SuppressWarnings("SunApi") final class HiddenClassDefiner implements ClassDefiner { private static final sun.misc.Unsafe THE_UNSAFE; diff --git a/core/src/com/google/inject/internal/aop/UnsafeGetter.java b/core/src/com/google/inject/internal/aop/UnsafeGetter.java index c7221b658f..f0f5e9be20 100644 --- a/core/src/com/google/inject/internal/aop/UnsafeGetter.java +++ b/core/src/com/google/inject/internal/aop/UnsafeGetter.java @@ -16,6 +16,7 @@ package com.google.inject.internal.aop; +@SuppressWarnings("SunApi") final class UnsafeGetter { private UnsafeGetter() {} diff --git a/examples/guice-demo/pom.xml b/examples/guice-demo/pom.xml index 2c290679eb..0f1360e206 100644 --- a/examples/guice-demo/pom.xml +++ b/examples/guice-demo/pom.xml @@ -14,8 +14,8 @@ 5.1.0 UTF-8 - 1.8 - 1.8 + 11 + 11 diff --git a/pom-template.xml b/pom-template.xml index 75c6bf2014..9d50dcee95 100644 --- a/pom-template.xml +++ b/pom-template.xml @@ -30,7 +30,7 @@ See the Apache License Version 2.0 for the specific language governing permissio - Guice is a lightweight dependency injection framework for Java 8 and above + Guice is a lightweight dependency injection framework for Java 11 and above https://github.com/google/guice diff --git a/tools/OsgiWrapper.java b/tools/OsgiWrapper.java index 24d659501b..f93545f038 100644 --- a/tools/OsgiWrapper.java +++ b/tools/OsgiWrapper.java @@ -33,7 +33,7 @@ public final class OsgiWrapper implements Callable { .collect(Collectors.joining(",")); private static final String DESCRIPTION = - "Guice is a lightweight dependency injection framework for Java 8 and above"; + "Guice is a lightweight dependency injection framework for Java 11 and above"; private static final String COPYRIGHT = "Copyright (C) 2022 Google Inc.";