This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Fix for IndexOutOfBoundsException #595
Open
mariuszs
wants to merge
1
commit into
master
Choose a base branch
from
fix/activity_tracker_index_out_of_band
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 15 additions & 23 deletions
38
...y-tracker/src/main/java/io/fourfinance/activity_tracker/activity/JoinPointParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
|
||
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...r/src/test/groovy/io/fourfinance/activity_tracker/activity/JoinPointParametersSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?