Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Fix for IndexOutOfBoundsException #595

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -1,40 +1,32 @@
package io.fourfinance.activity_tracker.activity;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;

import java.util.List;

import javaslang.Tuple;
import javaslang.collection.HashMap;
import javaslang.collection.List;
import javaslang.collection.Map;
import javaslang.control.Option;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import static com.google.common.base.Preconditions.checkNotNull;

class JoinPointParameters {

private final List<String> parameterNames;

private final List<Object> parameterValues;
private final Map<String, String> parameters;

JoinPointParameters(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
parameterNames = signature.getParameterNames() != null ? asList(signature.getParameterNames()) : java.util
.Collections.<String>emptyList();
parameterValues = joinPoint.getArgs() != null ? asList(joinPoint.getArgs()) : emptyList();
List<String> names = signature.getParameterNames() != null ? List.of(signature.getParameterNames()) : List.empty();
List<Object> values = joinPoint.getArgs() != null ? List.of(joinPoint.getArgs()) : List.empty();
parameters = HashMap.ofEntries(names.zip(values).map(t -> Tuple.of(t._1, t._2.toString())));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need for that easy thing java slang ? :D

Copy link
Contributor

Choose a reason for hiding this comment

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

We can use java 8 Optional and slang is definitely not needed. Is the bug still valid?

}

Optional<Object> getValue(String parameterName) {
Preconditions.checkNotNull(parameterName, "JoinPoint param must not be null");
if(parameterNames.contains(parameterName)) {
return Optional.fromNullable(parameterValues.get(indexOfParameter(parameterName)));
Option<String> getValue(String parameterName) {
checkNotNull(parameterName, "JoinPoint param must not be null");
if (parameters.containsKey(parameterName)) {
return parameters.get(parameterName);
} else {
return Optional.absent();
return Option.none();
}
}

private int indexOfParameter(final String parameterName) {
return parameterNames.indexOf(parameterName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ private String readActivityName(JoinPoint joinPoint) {
}

private String extractParamValue(JoinPoint joinPoint, final String field) {
return new JoinPointParameters(joinPoint).getValue(field).or("<not available>").toString();
return new JoinPointParameters(joinPoint).getValue(field).getOrElse("<not available>");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.fourfinance.activity_tracker.activity

import org.aspectj.lang.JoinPoint
import org.aspectj.lang.reflect.MethodSignature
import org.junit.Test
import spock.lang.Specification

public class JoinPointParametersSpec extends Specification {

JoinPoint joinPoint = Mock(JoinPoint)

void setup() {
MethodSignature signature = Mock(MethodSignature)
joinPoint.signature >> signature
signature.getParameterNames() >> ['foo']
}

@Test
public void shouldReturnEmptyValueWhenThereIsNoValue() {
given:
JoinPointParameters joinPointParameters = new JoinPointParameters(joinPoint)
expect:
joinPointParameters.getValue("foo").isEmpty()
}


}
1 change: 1 addition & 0 deletions activity-tracker-root/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ project(':activity-tracker-root:activity-tracker') {
compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.8.9'
compile group: 'io.dropwizard.metrics', name: 'metrics-core', version: '3.1.2'
compile 'commons-beanutils:commons-beanutils:1.9.2'
compile 'io.javaslang:javaslang:2.0.2'
compile 'com.google.guava:guava'
}
}
Expand Down