Skip to content

Formatting and making some methods private #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ private static LogSink createLogSink() {
}

public static void main(String[] args) {
// TODO validate arguments, show usage
startRuntime(args[0]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* This class loads all of the classes that are in jars on the classpath.
*
* <p>
* It is used to generate a class list and Application CDS archive that includes all the possible classes that could be
* loaded by the runtime. This simplifies the process of generating the Application CDS archive.
*/
Expand All @@ -38,7 +38,7 @@ private static void loadClass(String name) {
try {
Class.forName(name, true, SYSTEM_CLASS_LOADER);
} catch (ClassNotFoundException e) {
System.err.println("[WARN] Failed to load " + name + ": " + e.getMessage());
System.err.println("[WARN] Failed to load " + name + ": " + e.getMessage());
}
}

Expand All @@ -48,13 +48,13 @@ private static void loadClassesInJar(File file) throws IOException {
while (en.hasMoreElements()) {
JarEntry entry = en.nextElement();

if(!entry.getName().endsWith(".class")) {
if (!entry.getName().endsWith(".class")) {
continue;
}

String name = pathToClassName(entry.getName());

if(BLOCKLIST.contains(name)) {
if (BLOCKLIST.contains(name)) {
continue;
}

Expand All @@ -65,11 +65,11 @@ private static void loadClassesInJar(File file) throws IOException {
private static void loadClassesInClasspathEntry(String entry) throws IOException {
File file = new File(entry);

if(!file.exists()) {
if (!file.exists()) {
throw new FileNotFoundException("Classpath entry does not exist: " + file.getPath());
}

if(file.isDirectory() || !file.getPath().endsWith(".jar")) {
if (file.isDirectory() || !file.getPath().endsWith(".jar")) {
System.err.println("[WARN] Only jar classpath entries are supported. Skipping " + file.getPath());
return;
}
Expand All @@ -79,10 +79,10 @@ private static void loadClassesInClasspathEntry(String entry) throws IOException

private static void loadAllClasses() throws IOException {
final String classPath = System.getProperty("java.class.path");
if(classPath == null) {
if (classPath == null) {
return;
}
for(String classPathEntry : classPath.split(File.pathSeparator)) {
for (String classPathEntry : classPath.split(File.pathSeparator)) {
loadClassesInClasspathEntry(classPathEntry);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@

import com.amazonaws.services.lambda.runtime.ClientContext;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaRuntimeInternal;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.LambdaRuntimeInternal;

import com.amazonaws.services.lambda.runtime.api.client.LambdaRequestHandler.UserFaultHandler;
import com.amazonaws.services.lambda.runtime.api.client.api.LambdaClientContext;
import com.amazonaws.services.lambda.runtime.api.client.api.LambdaCognitoIdentity;
import com.amazonaws.services.lambda.runtime.api.client.api.LambdaContext;
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.InvocationRequest;
import com.amazonaws.services.lambda.runtime.api.client.util.UnsafeUtil;
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
import com.amazonaws.services.lambda.runtime.serialization.events.LambdaEventSerializers;
import com.amazonaws.services.lambda.runtime.serialization.factories.GsonFactory;
import com.amazonaws.services.lambda.runtime.serialization.factories.JacksonFactory;
import com.amazonaws.services.lambda.runtime.serialization.util.Functions;
import com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil;
import com.amazonaws.services.lambda.runtime.api.client.LambdaRequestHandler.UserFaultHandler;
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.InvocationRequest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -42,9 +41,7 @@
import java.util.Map;
import java.util.Optional;

import static com.amazonaws.services.lambda.runtime.api.client.UserFault.filterStackTrace;
import static com.amazonaws.services.lambda.runtime.api.client.UserFault.makeUserFault;
import static com.amazonaws.services.lambda.runtime.api.client.UserFault.trace;
import static com.amazonaws.services.lambda.runtime.api.client.UserFault.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer explicit imports


public final class EventHandlerLoader {
private static final byte[] _JsonNull = new byte[]{'n', 'u', 'l', 'l'};
Expand All @@ -57,12 +54,14 @@ private enum Platform {

private static final EnumMap<Platform, Map<Type, PojoSerializer<Object>>> typeCache = new EnumMap<>(Platform.class);

private EventHandlerLoader() { }
private EventHandlerLoader() {
}

/**
* returns the appropriate serializer for the class based on platform and whether the class is a supported event
*
* @param platform enum platform
* @param type Type of object used
* @param type Type of object used
* @return PojoSerializer
* @see Platform for which platforms are used
* @see LambdaEventSerializers for how mixins and modules are added to the serializer
Expand All @@ -76,7 +75,7 @@ private static PojoSerializer<Object> getSerializer(Platform platform, Type type

// if serializing a Class that is a Lambda supported event, use Jackson with customizations
if (type instanceof Class) {
Class<Object> clazz = ((Class)type);
Class<Object> clazz = ((Class) type);
if (LambdaEventSerializers.isLambdaSupportedEvent(clazz.getName())) {
return LambdaEventSerializers.serializerFor(clazz, AWSLambda.customerClassLoader);
}
Expand Down Expand Up @@ -150,7 +149,7 @@ private static Platform getPlatform(Context context) {
}

private static boolean isVoid(Type type) {
return Void.TYPE.equals(type) || (type instanceof Class) && Void.class.isAssignableFrom((Class<?>)type);
return Void.TYPE.equals(type) || (type instanceof Class) && Void.class.isAssignableFrom((Class<?>) type);
}

/**
Expand Down Expand Up @@ -393,7 +392,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
}
}

public static <T> Constructor<T> getConstructor(Class<T> clazz) throws Exception {
private static <T> Constructor<T> getConstructor(Class<T> clazz) throws Exception {
final Constructor<T> constructor;
try {
constructor = clazz.getConstructor();
Expand All @@ -409,7 +408,7 @@ public static <T> Constructor<T> getConstructor(Class<T> clazz) throws Exception
return constructor;
}

public static <T> T newInstance(Constructor<? extends T> constructor) {
private static <T> T newInstance(Constructor<? extends T> constructor) {
try {
return constructor.newInstance();
} catch (UserFault e) {
Expand Down Expand Up @@ -458,15 +457,15 @@ public ClassContext(ParameterizedType type, ClassContext curContext) {
for (int i = 0; i < types.length; i++) {
Type t = types[i];
if (t instanceof TypeVariable) {
types[i] = curContext.resolveTypeVariable((TypeVariable)t);
types[i] = curContext.resolveTypeVariable((TypeVariable) t);
}
}

Type t = type.getRawType();
if (t instanceof Class) {
this.clazz = (Class)t;
this.clazz = (Class) t;
} else if (t instanceof TypeVariable) {
this.clazz = (Class)((TypeVariable)t).getGenericDeclaration();
this.clazz = (Class) ((TypeVariable) t).getGenericDeclaration();
} else {
throw new RuntimeException("Type " + t + " is of unexpected type " + t.getClass());
}
Expand Down Expand Up @@ -499,30 +498,30 @@ private TypeVariable[] getTypeParameters() {
*
* @return null of no type found. Otherwise the type found.
*/
public static Type[] findInterfaceParameters(Class<?> clazz, Class<?> iface) {
private static Type[] findInterfaceParameters(Class<?> clazz, Class<?> iface) {
LinkedList<ClassContext> clazzes = new LinkedList<>();
clazzes.addFirst(new ClassContext(clazz, (Type[])null));
clazzes.addFirst(new ClassContext(clazz, (Type[]) null));
while (!clazzes.isEmpty()) {
final ClassContext curContext = clazzes.removeLast();
Type[] interfaces = curContext.clazz.getGenericInterfaces();

for (Type type : interfaces) {
if (type instanceof ParameterizedType) {
ParameterizedType candidate = (ParameterizedType)type;
ParameterizedType candidate = (ParameterizedType) type;
Type rawType = candidate.getRawType();
if (!(rawType instanceof Class)) {
//should be impossible
System.err.println("raw type is not a class: " + rawType);
continue;
}
Class<?> rawClass = (Class<?>)rawType;
Class<?> rawClass = (Class<?>) rawType;
if (iface.isAssignableFrom(rawClass)) {
return new ClassContext(candidate, curContext).actualTypeArguments;
} else {
clazzes.addFirst(new ClassContext(candidate, curContext));
}
} else if (type instanceof Class) {
clazzes.addFirst(new ClassContext((Class<?>)type, curContext));
clazzes.addFirst(new ClassContext((Class<?>) type, curContext));
} else {
//should never happen?
System.err.println("Unexpected type class " + type.getClass().getName());
Expand All @@ -531,17 +530,17 @@ public static Type[] findInterfaceParameters(Class<?> clazz, Class<?> iface) {

final Type superClass = curContext.clazz.getGenericSuperclass();
if (superClass instanceof ParameterizedType) {
clazzes.addFirst(new ClassContext((ParameterizedType)superClass, curContext));
clazzes.addFirst(new ClassContext((ParameterizedType) superClass, curContext));
} else if (superClass != null) {
clazzes.addFirst(new ClassContext((Class<?>)superClass, curContext));
clazzes.addFirst(new ClassContext((Class<?>) superClass, curContext));
}
}
return null;
}


@SuppressWarnings({"rawtypes"})
public static LambdaRequestHandler wrapRequestHandlerClass(final Class<? extends RequestHandler> clazz) {
private static LambdaRequestHandler wrapRequestHandlerClass(final Class<? extends RequestHandler> clazz) {
Type[] ptypes = findInterfaceParameters(clazz, RequestHandler.class);
if (ptypes == null) {
return new UserFaultHandler(makeUserFault("Class "
Expand All @@ -555,7 +554,7 @@ public static LambdaRequestHandler wrapRequestHandlerClass(final Class<? extends

for (Type t : ptypes) {
if (t instanceof TypeVariable) {
Type[] bounds = ((TypeVariable)t).getBounds();
Type[] bounds = ((TypeVariable) t).getBounds();
boolean foundBound = false;
if (bounds != null) {
for (Type bound : bounds) {
Expand Down Expand Up @@ -588,7 +587,7 @@ public static LambdaRequestHandler wrapRequestHandlerClass(final Class<? extends
}
}

public static LambdaRequestHandler wrapRequestStreamHandlerClass(final Class<? extends RequestStreamHandler> clazz) {
private static LambdaRequestHandler wrapRequestStreamHandlerClass(final Class<? extends RequestStreamHandler> clazz) {
final Constructor<? extends RequestStreamHandler> constructor;
try {
constructor = getConstructor(clazz);
Expand All @@ -600,7 +599,7 @@ public static LambdaRequestHandler wrapRequestStreamHandlerClass(final Class<? e
}
}

public static LambdaRequestHandler loadStreamingRequestHandler(Class<?> clazz) {
private static LambdaRequestHandler loadStreamingRequestHandler(Class<?> clazz) {
if (RequestStreamHandler.class.isAssignableFrom(clazz)) {
return wrapRequestStreamHandlerClass(clazz.asSubclass(RequestStreamHandler.class));
} else if (RequestHandler.class.isAssignableFrom(clazz)) {
Expand Down Expand Up @@ -730,10 +729,9 @@ private static final boolean lastParameterIsContext(Class<?>[] params) {
public int compare(Method lhs, Method rhs) {

//1. Non bridge methods are preferred over bridge methods.
if (! lhs.isBridge() && rhs.isBridge()) {
if (!lhs.isBridge() && rhs.isBridge()) {
return -1;
}
else if (!rhs.isBridge() && lhs.isBridge()) {
} else if (!rhs.isBridge() && lhs.isBridge()) {
return 1;
}

Expand Down Expand Up @@ -828,13 +826,13 @@ private static LambdaRequestHandler loadEventPojoHandler(HandlerInfo handlerInfo
}

@SuppressWarnings({"rawtypes"})
public static LambdaRequestHandler wrapPojoHandler(RequestHandler instance, Type pType, Type rType) {
private static LambdaRequestHandler wrapPojoHandler(RequestHandler instance, Type pType, Type rType) {
return wrapRequestStreamHandler(new PojoHandlerAsStreamHandler(instance, Optional.ofNullable(pType),
isVoid(rType) ? Optional.<Type>empty() : Optional.of(rType)
));
}

public static String exceptionToString(Throwable t) {
private static String exceptionToString(Throwable t) {
StringWriter writer = new StringWriter(65536);
try (PrintWriter wrapped = new PrintWriter(writer)) {
t.printStackTrace(wrapped);
Expand All @@ -849,7 +847,7 @@ public static String exceptionToString(Throwable t) {
return buffer.toString();
}

public static LambdaRequestHandler wrapRequestStreamHandler(final RequestStreamHandler handler) {
private static LambdaRequestHandler wrapRequestStreamHandler(final RequestStreamHandler handler) {
return new LambdaRequestHandler() {
private final ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
private Functions.V2<String, String> log4jContextPutMethod = null;
Expand All @@ -860,14 +858,15 @@ private void safeAddRequestIdToLog4j(String log4jContextClassName,
Class<?> log4jContextClass = ReflectUtil.loadClass(AWSLambda.customerClassLoader, log4jContextClassName);
log4jContextPutMethod = ReflectUtil.loadStaticV2(log4jContextClass, "put", false, String.class, contextMapValueClass);
log4jContextPutMethod.call("AWSRequestId", request.getId());
} catch (Exception e) {}
} catch (Exception e) {
}
}

public ByteArrayOutputStream call(InvocationRequest request) throws Error, Exception {
output.reset();

LambdaCognitoIdentity cognitoIdentity = null;
if(request.getCognitoIdentity() != null && !request.getCognitoIdentity().isEmpty()) {
if (request.getCognitoIdentity() != null && !request.getCognitoIdentity().isEmpty()) {
cognitoIdentity = getCognitoSerializer().fromJson(request.getCognitoIdentity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Failure(Throwable t) {
this.errorType = t.getClass().getName();
StackTraceElement[] trace = t.getStackTrace();
this.stackTrace = new String[trace.length];
for( int i = 0; i < trace.length; i++) {
for (int i = 0; i < trace.length; i++) {
this.stackTrace[i] = trace[i].toString();
}
Throwable cause = t.getCause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class InvalidHandlerException extends RuntimeException {
public final Class<?> clazz;
public final String methodName;

public HandlerInfo (Class<?> clazz, String methodName) {
public HandlerInfo(Class<?> clazz, String methodName) {
this.clazz = clazz;
this.methodName = methodName;
}
Expand All @@ -19,15 +19,15 @@ public static HandlerInfo fromString(String handler, ClassLoader cl) throws Clas
final int colonLoc = handler.lastIndexOf("::");
final String className;
final String methodName;
if(colonLoc < 0) {
if (colonLoc < 0) {
className = handler;
methodName = null;
} else {
className = handler.substring(0, colonLoc);
methodName = handler.substring(colonLoc + 2);
}

if(className.isEmpty() || (methodName != null && methodName.isEmpty())) {
if (className.isEmpty() || (methodName != null && methodName.isEmpty())) {
throw new InvalidHandlerException();
}
return new HandlerInfo(Class.forName(className, true, cl), methodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import com.amazonaws.services.lambda.runtime.api.client.util.EnvReader;

import static com.amazonaws.services.lambda.runtime.api.client.ReservedRuntimeEnvironmentVariables.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer explicit imports

import static java.lang.Integer.parseInt;

public class LambdaEnvironment {
public static final EnvReader ENV_READER = new EnvReader();
public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_MEMORY_SIZE, "128"));
public static final String LOG_GROUP_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_GROUP_NAME);
public static final String LOG_STREAM_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_STREAM_NAME);
public static final String FUNCTION_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_NAME);
public static final String FUNCTION_VERSION = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_VERSION);
public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault(AWS_LAMBDA_FUNCTION_MEMORY_SIZE, "128"));
public static final String LOG_GROUP_NAME = ENV_READER.getEnv(AWS_LAMBDA_LOG_GROUP_NAME);
public static final String LOG_STREAM_NAME = ENV_READER.getEnv(AWS_LAMBDA_LOG_STREAM_NAME);
public static final String FUNCTION_NAME = ENV_READER.getEnv(AWS_LAMBDA_FUNCTION_NAME);
public static final String FUNCTION_VERSION = ENV_READER.getEnv(AWS_LAMBDA_FUNCTION_VERSION);
}
Loading