Skip to content

Commit

Permalink
changing cache in MethodPool from LinkedList to HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
bseiller committed May 14, 2018
1 parent f09dd13 commit e134c27
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public boolean matches(final MethodIdentifier identifier) {
return this.identifier.equals(identifier);
}

@Override
public MethodIdentifier getIdentifier() {
return identifier;
}

private static Element addToArray(final Element object, final List<Element> arguments) {
return addToArray(object, arguments.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ public boolean matches(final MethodIdentifier identifier) {
return this.identifier.equals(identifier);
}

@Override
public MethodIdentifier getIdentifier() {
return identifier;
}

private static Element addHeader(final Element object, final String header) {
object.getPossibleValues().stream().filter(r -> r instanceof HttpResponse).map(r -> (HttpResponse) r).forEach(r -> r.getHeaders().add(header));
return object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.sebastian_daschner.jaxrs_analyzer.analysis.bytecode.simulation;

import com.sebastian_daschner.jaxrs_analyzer.LogProvider;
import com.sebastian_daschner.jaxrs_analyzer.model.elements.Element;
import com.sebastian_daschner.jaxrs_analyzer.model.methods.IdentifiableMethod;
import com.sebastian_daschner.jaxrs_analyzer.model.methods.Method;
import com.sebastian_daschner.jaxrs_analyzer.model.methods.MethodIdentifier;
import com.sebastian_daschner.jaxrs_analyzer.model.methods.ProjectMethod;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
Expand All @@ -49,16 +49,16 @@ public class MethodPool {
return null;
};

private final List<IdentifiableMethod> availableMethods;
private final Map<MethodIdentifier, IdentifiableMethod> availableMethods;
private final ReadWriteLock readWriteLock;

private MethodPool() {
availableMethods = new LinkedList<>();
availableMethods = new HashMap<>();

// order matters, known methods are taken first
Stream.of(KnownResponseResultMethod.values()).forEach(availableMethods::add);
Stream.of(KnownJsonResultMethod.values()).forEach(availableMethods::add);
Stream.of(KnownResponseResultMethod.values()).forEach(knownResponseResultMethod -> availableMethods.put(knownResponseResultMethod.getIdentifier(), knownResponseResultMethod));
Stream.of(KnownJsonResultMethod.values()).forEach(knownJsonResultMethod -> availableMethods.put(knownJsonResultMethod.getIdentifier(), knownJsonResultMethod));

// could be made obsolete by using ConcurrentHashMap instead a HashMap for availableMethods
readWriteLock = new ReentrantReadWriteLock();
}

Expand All @@ -70,7 +70,15 @@ private MethodPool() {
public void addProjectMethod(final ProjectMethod method) {
readWriteLock.writeLock().lock();
try {
availableMethods.add(method);
availableMethods.put(method.getIdentifier(), method);

// FIXME: just for debugging/testing
if (availableMethods.containsKey(method.getIdentifier())) {
final IdentifiableMethod method2 = availableMethods.get(method.getIdentifier());
if(!method2.getIdentifier().equals(method.getIdentifier())) {
LogProvider.debug("replacing existing method in pool");
}
}
} finally {
readWriteLock.writeLock().unlock();
}
Expand All @@ -86,9 +94,9 @@ public Method get(final MethodIdentifier identifier) {
// search for available methods
readWriteLock.readLock().lock();
try {
final Optional<? extends IdentifiableMethod> method = availableMethods.stream().filter(m -> m.matches(identifier)).findAny();
if (method.isPresent())
return method.get();
if (availableMethods.containsKey(identifier)) {
return availableMethods.get(identifier);
}
} finally {
readWriteLock.readLock().unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public interface IdentifiableMethod extends Method {
*/
boolean matches(final MethodIdentifier identifier);

MethodIdentifier getIdentifier();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.asList;

Expand Down Expand Up @@ -87,39 +85,22 @@ public boolean isStaticMethod() {
}

@Override
public boolean equals(Object o) {
@SuppressWarnings({"squid:S00122", "squid:S1067"})
// generated code so it's fine to have more than one statement in one line and more than 3 conditional operators
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

MethodIdentifier that = (MethodIdentifier) o;

if (staticMethod != that.staticMethod) return false;
if (!containingClass.equals(that.containingClass)) return false;
if (!methodName.equals(that.methodName)) return false;

if (parameters.equals(that.parameters))
return true;

// fallback if signature matches after type erasure
if (parameters.size() == that.parameters.size()) {
final List<String> erasedTypes = parameters.stream().map(JavaUtils::toClassName).collect(Collectors.toList());
final List<String> erasedThatTypes = that.parameters.stream().map(JavaUtils::toClassName).collect(Collectors.toList());
final String erasedReturnType = JavaUtils.toClassName(returnType);
final String erasedThatReturnType = JavaUtils.toClassName(that.returnType);
return erasedTypes.equals(erasedThatTypes) && erasedReturnType.equals(erasedThatReturnType);
}

return false;
final MethodIdentifier that = (MethodIdentifier) o;
return staticMethod == that.staticMethod &&
Objects.equals(containingClass, that.containingClass) &&
Objects.equals(methodName, that.methodName) &&
Objects.equals(returnType, that.returnType) &&
Objects.equals(parameters, that.parameters);
}

@Override
public int hashCode() {
int result = containingClass.hashCode();
result = 31 * result + methodName.hashCode();
result = 31 * result + returnType.hashCode();
result = 31 * result + (staticMethod ? 1 : 0);
result = 31 * result + parameters.hashCode();
return result;
return Objects.hash(containingClass, methodName, returnType, staticMethod, parameters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public boolean matches(final MethodIdentifier identifier) {
return this.identifier.equals(identifier);
}

@Override
public MethodIdentifier getIdentifier() {
return identifier;
}

@Override
public Element invoke(final Element object, final List<Element> arguments) {
return new InjectableArgumentMethodSimulator().simulate(arguments, instructions, identifier);
Expand Down

0 comments on commit e134c27

Please sign in to comment.