Skip to content
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

fix two problems in special kind handling #23

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions findbugs/src/java/edu/umd/cs/findbugs/OpcodeStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -183,6 +186,9 @@ static class HttpParameterInjection {

public static class Item {

/**
* A type qualifier to mark {@code int} value as SpecialKind type.
*/
@Documented
@TypeQualifier(applicableTo = Integer.class)
@Retention(RetentionPolicy.RUNTIME)
Expand Down Expand Up @@ -269,15 +275,21 @@ public static class Item {
int TYPE_ONLY = 24;

@edu.umd.cs.findbugs.internalAnnotations.StaticConstant
public static final HashMap<Integer, String> specialKindNames = new HashMap<Integer, String>();
public static final ConcurrentMap<Integer, String> specialKindNames = new ConcurrentHashMap<Integer, String>();

private static @SpecialKind int nextSpecialKind = asSpecialKind(TYPE_ONLY + 1);
private static AtomicInteger nextSpecialKind = new AtomicInteger(TYPE_ONLY + 1);

/**
* Define new SpecialKind with given name, and return its {@code int} value
* @param name
* the name of new SpecialKind, can be null
* @return
* {@code int} value which expresses new SpecialKind
*/
public static @SpecialKind
int defineNewSpecialKind(String name) {
specialKindNames.put(nextSpecialKind, name);
@SpecialKind int result = asSpecialKind( nextSpecialKind+1);
nextSpecialKind = result;
@SpecialKind int result = asSpecialKind(nextSpecialKind.getAndIncrement());
specialKindNames.put(Integer.valueOf(result), name);
return result;
}

Expand Down
16 changes: 16 additions & 0 deletions findbugs/src/junit/edu/umd/cs/findbugs/OpcodeStackItemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,20 @@ public void testMergeTypeOnly() {
assertEquals(0,m2.getConstant());
}

private static final String NEW_ITEM_KIND_NAME = "newItemKindName";
public void testDefineNewItemKind() {
int defined = OpcodeStack.Item.defineNewSpecialKind(NEW_ITEM_KIND_NAME);
assertEquals(NEW_ITEM_KIND_NAME,
OpcodeStack.Item.specialKindNames.get(Integer.valueOf(defined)));
}

public void testDefinedItemKindIsUsedInToStringMethod() {
int defined = OpcodeStack.Item.defineNewSpecialKind(NEW_ITEM_KIND_NAME);
OpcodeStack.Item intItem = new OpcodeStack.Item("I");
intItem.setSpecialKind(defined);
String result = intItem.toString();
assertTrue("Item.toString() does not use proper name of special kind:" + result,
result.contains(NEW_ITEM_KIND_NAME));
}

}