Skip to content

Commit

Permalink
Run google-java-format on all Guice code.
Browse files Browse the repository at this point in the history
configure a presubmit to ensure that it stays formatted.

Highlights include:
* simplified import order
* method annotations are now consistently defined on the preceding line
* javadoc reformatted to 100 chars column width

One test that contained line numbers in error messages had to be modified and
the formatter didn't like some of the more complicated preprocessor directives
(MOE and AOP).

To avoid formatting the copyright notices as javadoc i did a preprocessing step to rewrite the initial '/**' to '/*' using perl

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132693141
  • Loading branch information
lukesandberg authored and sameb committed Sep 13, 2016
1 parent 751b555 commit 462a195
Show file tree
Hide file tree
Showing 36 changed files with 613 additions and 632 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,11 +20,11 @@

/**
* Abstract implementation for all servlet module bindings
*
*
* @author [email protected] (Sam Berlin)
*/
class AbstractServletModuleBinding<T> implements ServletModuleBinding {

private final Map<String, String> initParams;
private final T target;
private final UriPatternMatcher patternMatcher;
Expand Down Expand Up @@ -54,10 +54,9 @@ protected T getTarget() {
public UriPatternType getUriPatternType() {
return patternMatcher.getPatternType();
}

@Override
public boolean matchesUri(String uri) {
return patternMatcher.matches(uri);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,19 +18,16 @@

import com.google.common.collect.Maps;
import com.google.inject.OutOfScopeException;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;

/**
* A wrapper for requests that makes requests immutable, taking a snapshot
* of the original request.
* A wrapper for requests that makes requests immutable, taking a snapshot of the original request.
*
* @author [email protected] (Dhanji R. Prasanna)
*/
Expand Down Expand Up @@ -63,31 +60,38 @@ public ContinuingHttpServletRequest(HttpServletRequest request) {
}
}

@Override public HttpSession getSession() {
@Override
public HttpSession getSession() {
throw new OutOfScopeException("Cannot access the session in a continued request");
}

@Override public HttpSession getSession(boolean create) {
@Override
public HttpSession getSession(boolean create) {
throw new UnsupportedOperationException("Cannot access the session in a continued request");
}

@Override public ServletInputStream getInputStream() throws IOException {
@Override
public ServletInputStream getInputStream() throws IOException {
throw new UnsupportedOperationException("Cannot access raw request on a continued request");
}

@Override public void setAttribute(String name, Object o) {
@Override
public void setAttribute(String name, Object o) {
attributes.put(name, o);
}

@Override public void removeAttribute(String name) {
@Override
public void removeAttribute(String name) {
attributes.remove(name);
}

@Override public Object getAttribute(String name) {
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}

@Override public Cookie[] getCookies() {
@Override
public Cookie[] getCookies() {
// NOTE(dhanji): Cookies themselves are mutable. However a ContinuingHttpServletRequest
// snapshots the original set of cookies it received and imprisons them in immutable
// form. Unfortunately, the cookie array itself is mutable and there is no way for us
Expand All @@ -112,31 +116,38 @@ public ImmutableCookie(Cookie original) {
}
}

@Override public void setComment(String purpose) {
@Override
public void setComment(String purpose) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}

@Override public void setDomain(String pattern) {
@Override
public void setDomain(String pattern) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}

@Override public void setMaxAge(int expiry) {
@Override
public void setMaxAge(int expiry) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}

@Override public void setPath(String uri) {
@Override
public void setPath(String uri) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}

@Override public void setSecure(boolean flag) {
@Override
public void setSecure(boolean flag) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}

@Override public void setValue(String newValue) {
@Override
public void setValue(String newValue) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}

@Override public void setVersion(int v) {
@Override
public void setVersion(int v) {
throw new UnsupportedOperationException("Cannot modify cookies on a continued request");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,7 +16,6 @@
package com.google.inject.servlet;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
Expand All @@ -31,8 +30,8 @@
* @see com.google.inject.servlet.ManagedFilterPipeline See Also ManagedFilterPipeline.
*/
class DefaultFilterPipeline implements FilterPipeline {
@Inject DefaultFilterPipeline() {
}
@Inject
DefaultFilterPipeline() {}

@Override
public void initPipeline(ServletContext context) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,11 +18,9 @@
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
Expand All @@ -35,19 +33,19 @@
* A Filter chain impl which basically passes itself to the "current" filter and iterates the chain
* on {@code doFilter()}. Modeled on something similar in Apache Tomcat.
*
* Following this, it attempts to dispatch to guice-servlet's registered servlets using the
* <p>Following this, it attempts to dispatch to guice-servlet's registered servlets using the
* ManagedServletPipeline.
*
* And the end, it proceeds to the web.xml (default) servlet filter chain, if needed.
* <p>And the end, it proceeds to the web.xml (default) servlet filter chain, if needed.
*
* @author Dhanji R. Prasanna
* @since 1.0
*/
class FilterChainInvocation implements FilterChain {
private static final Set<String> SERVLET_INTERNAL_METHODS = ImmutableSet.of(
FilterChainInvocation.class.getName() + ".doFilter");

private static final Set<String> SERVLET_INTERNAL_METHODS =
ImmutableSet.of(FilterChainInvocation.class.getName() + ".doFilter");

private final FilterDefinition[] filterDefinitions;
private final FilterChain proceedingChain;
private final ManagedServletPipeline servletPipeline;
Expand All @@ -57,8 +55,10 @@ class FilterChainInvocation implements FilterChain {
// whether or not we've caught an exception & cleaned up stack traces
private boolean cleanedStacks = false;

public FilterChainInvocation(FilterDefinition[] filterDefinitions,
ManagedServletPipeline servletPipeline, FilterChain proceedingChain) {
public FilterChainInvocation(
FilterDefinition[] filterDefinitions,
ManagedServletPipeline servletPipeline,
FilterChain proceedingChain) {

this.filterDefinitions = filterDefinitions;
this.servletPipeline = servletPipeline;
Expand All @@ -71,8 +71,8 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
GuiceFilter.Context previous = GuiceFilter.localContext.get();
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest originalRequest
= (previous != null) ? previous.getOriginalRequest() : request;
HttpServletRequest originalRequest =
(previous != null) ? previous.getOriginalRequest() : request;
GuiceFilter.localContext.set(new GuiceFilter.Context(originalRequest, request, response));
try {
Filter filter = findNextFilter(request);
Expand Down Expand Up @@ -107,8 +107,8 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
}

/**
* Iterates over the remaining filter definitions.
* Returns the first applicable filter, or null if none apply.
* Iterates over the remaining filter definitions. Returns the first applicable filter, or null if
* none apply.
*/
private Filter findNextFilter(HttpServletRequest request) {
while (++index < filterDefinitions.length) {
Expand All @@ -119,10 +119,10 @@ private Filter findNextFilter(HttpServletRequest request) {
}
return null;
}

/**
* Removes stacktrace elements related to AOP internal mechanics from the
* throwable's stack trace and any causes it may have.
* Removes stacktrace elements related to AOP internal mechanics from the throwable's stack trace
* and any causes it may have.
*/
private void pruneStacktrace(Throwable throwable) {
for (Throwable t = throwable; t != null; t = t.getCause()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,14 +22,12 @@
import com.google.inject.spi.BindingTargetVisitor;
import com.google.inject.spi.ProviderInstanceBinding;
import com.google.inject.spi.ProviderWithExtensionVisitor;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
Expand Down Expand Up @@ -70,17 +68,13 @@ public FilterDefinition get() {
@Override
public <B, V> V acceptExtensionVisitor(
BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) {
if(visitor instanceof ServletModuleTargetVisitor) {
if(filterInstance != null) {
return ((ServletModuleTargetVisitor<B, V>)visitor).visit(
new InstanceFilterBindingImpl(initParams,
filterInstance,
patternMatcher));
if (visitor instanceof ServletModuleTargetVisitor) {
if (filterInstance != null) {
return ((ServletModuleTargetVisitor<B, V>) visitor)
.visit(new InstanceFilterBindingImpl(initParams, filterInstance, patternMatcher));
} else {
return ((ServletModuleTargetVisitor<B, V>)visitor).visit(
new LinkedFilterBindingImpl(initParams,
filterKey,
patternMatcher));
return ((ServletModuleTargetVisitor<B, V>) visitor)
.visit(new LinkedFilterBindingImpl(initParams, filterKey, patternMatcher));
}
} else {
return visitor.visit(binding);
Expand All @@ -91,13 +85,16 @@ private boolean shouldFilter(String uri) {
return uri != null && patternMatcher.matches(uri);
}

public void init(final ServletContext servletContext, Injector injector,
Set<Filter> initializedSoFar) throws ServletException {
public void init(
final ServletContext servletContext, Injector injector, Set<Filter> initializedSoFar)
throws ServletException {

// This absolutely must be a singleton, and so is only initialized once.
if (!Scopes.isSingleton(injector.getBinding(filterKey))) {
throw new ServletException("Filters must be bound as singletons. "
+ filterKey + " was not bound in singleton scope.");
throw new ServletException(
"Filters must be bound as singletons. "
+ filterKey
+ " was not bound in singleton scope.");
}

Filter filter = injector.getInstance(filterKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,33 +16,30 @@
package com.google.inject.servlet;

import com.google.inject.ImplementedBy;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* An internal dispatcher for guice-servlet registered servlets and filters.
* By default, we assume a Guice 1.0 style servlet module is in play. In other
* words, we dispatch directly to the web.xml pipeline after setting up scopes.
* An internal dispatcher for guice-servlet registered servlets and filters. By default, we assume a
* Guice 1.0 style servlet module is in play. In other words, we dispatch directly to the web.xml
* pipeline after setting up scopes.
*
* <p>
* If on the other hand, {@link ServletModule} is used to register managed
* servlets and/or filters, then a different pipeline is bound instead. Which,
* after dispatching to Guice-injected filters and servlets continues to the web.xml
* pipeline (if necessary).
* <p>If on the other hand, {@link ServletModule} is used to register managed servlets and/or
* filters, then a different pipeline is bound instead. Which, after dispatching to Guice-injected
* filters and servlets continues to the web.xml pipeline (if necessary).
*
* @author [email protected] (Dhanji R. Prasanna)
*/
@ImplementedBy(DefaultFilterPipeline.class)
interface FilterPipeline {
void initPipeline(ServletContext context) throws ServletException;

void destroyPipeline();

void dispatch(ServletRequest request, ServletResponse response,
FilterChain defaultFilterChain) throws IOException, ServletException;
void dispatch(ServletRequest request, ServletResponse response, FilterChain defaultFilterChain)
throws IOException, ServletException;
}
Loading

0 comments on commit 462a195

Please sign in to comment.