You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2020/01/06 08:12:02 UTC
[calcite] 04/07: Contribute class Resources to ASF,
and change its header
This is an automated email from the ASF dual-hosted git repository.
jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git
commit 61da868ea50fa9fd1584eaafd3fcb0c644e9c953
Author: Julian Hyde <jh...@apache.org>
AuthorDate: Tue Dec 10 21:03:18 2019 -0800
Contribute class Resources to ASF, and change its header
Minor refactorings, such as converting anonymous classes to lambdas.
---
.../java/org/apache/calcite/runtime/Resources.java | 83 +++++++---------------
1 file changed, 27 insertions(+), 56 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/runtime/Resources.java b/core/src/main/java/org/apache/calcite/runtime/Resources.java
index f00a81e..8ec97ee 100644
--- a/core/src/main/java/org/apache/calcite/runtime/Resources.java
+++ b/core/src/main/java/org/apache/calcite/runtime/Resources.java
@@ -1,10 +1,10 @@
/*
- * Licensed to Julian Hyde under one or more contributor license
- * agreements. See the NOTICE file distributed with this work for
- * additional information regarding copyright ownership. Julian Hyde
- * licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@@ -23,6 +23,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.*;
+import java.security.PrivilegedAction;
import java.text.*;
import java.util.*;
import java.util.concurrent.Callable;
@@ -30,18 +31,12 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* Defining wrapper classes around resources that allow the compiler to check
- * whether the resources exist and have the argument types that your code
- * expects.
- *
- * <p>If this class belongs to a package other than
- * {@code net.hydromatic.resource}, it was probably generated by the Maven
- * plugin (groupId: "net.hydromatic", artifactId:
- * "hydromatic-resource-maven-plugin"). Code generation allows projects to use
- * this resource library without adding a runtime dependency on another JAR.
+ * whether the resources exist, and that uses of resources have the appropriate
+ * number and types of arguments to match the message.
*/
public class Resources {
private static final ThreadLocal<Locale> MAP_THREAD_TO_LOCALE =
- new ThreadLocal<Locale>();
+ new ThreadLocal<>();
private Resources() {}
@@ -140,8 +135,7 @@ public class Resources {
return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
new Class[] {clazz},
new InvocationHandler() {
- final Map<String, Object> cache =
- new ConcurrentHashMap<String, Object>();
+ final Map<String, Object> cache = new ConcurrentHashMap<>();
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
@@ -353,7 +347,7 @@ public class Resources {
String raw = raw();
MessageFormat format = new MessageFormat(raw);
final Format[] formats = format.getFormatsByArgumentIndex();
- final List<Class> types = new ArrayList<Class>();
+ final List<Class> types = new ArrayList<>();
final Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < formats.length; i++) {
Format format1 = formats[i];
@@ -471,11 +465,8 @@ public class Resources {
}
}
return ex;
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } catch (NoSuchMethodException e) {
+ } catch (InstantiationException | IllegalAccessException
+ | NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Error) {
@@ -528,35 +519,22 @@ public class Resources {
if (ex == null) {
cause = new NullPointerException();
}
- } catch (AssertionError e) {
- cause = e;
- } catch (RuntimeException e) {
- cause = e;
- } catch (Exception e) {
- // This can never happen since exSupplier should be just a ex() call.
+ } catch (AssertionError | Exception e) {
// catch(Exception) is required since Callable#call throws Exception.
- // Just in case we get exception somehow, we will rethrow it as a part
- // of AssertionError below.
+ // But in practice, e will always be AssertionError or RuntimeException,
+ // since exSupplier should be just a ex() call.
cause = e;
}
if (cause != null) {
- AssertionError assertionError = new AssertionError(
- "error instantiating exception for resource '"
- + method.getName() + "'");
- assertionError.initCause(cause);
- throw assertionError;
+ throw new AssertionError("error instantiating exception for resource '"
+ + method.getName() + "'", cause);
}
}
@Override public void validate(EnumSet<Validation> validations) {
super.validate(validations);
if (validations.contains(Validation.CREATE_EXCEPTION)) {
- validateException(
- new Callable<Exception>() {
- public Exception call() throws Exception {
- return ex(new NullPointerException("test"));
- }
- });
+ validateException(() -> ex(new NullPointerException("test")));
}
}
}
@@ -575,12 +553,7 @@ public class Resources {
@Override public void validate(EnumSet<Validation> validations) {
super.validate(validations);
if (validations.contains(Validation.CREATE_EXCEPTION)) {
- validateException(
- new Callable<Exception>() {
- public Exception call() throws Exception {
- return ex();
- }
- });
+ validateException(this::ex);
}
}
}
@@ -944,14 +917,12 @@ public class Resources {
private static InputStream openPropertiesFile(Class clazz) {
final ClassLoader loader = clazz.getClassLoader();
final String resName = clazz.getName().replace('.', '/') + ".properties";
- return (InputStream) java.security.AccessController.doPrivileged(
- new java.security.PrivilegedAction() {
- public Object run() {
- if (loader != null) {
- return loader.getResourceAsStream(resName);
- } else {
- return ClassLoader.getSystemResourceAsStream(resName);
- }
+ return java.security.AccessController.doPrivileged(
+ (PrivilegedAction<InputStream>) () -> {
+ if (loader != null) {
+ return loader.getResourceAsStream(resName);
+ } else {
+ return ClassLoader.getSystemResourceAsStream(resName);
}
});
}