Skip to content
Permalink
Browse files
[FIXED JENKINS-37403] Add static methods for fetching Symbol values.
Both from Objects and from Classes.
  • Loading branch information
abayer committed Aug 14, 2016
1 parent 53768bf commit bc43d9ad717be5beaf70e2678be58d0a63ebcb34
@@ -9,6 +9,7 @@
import org.jenkinsci.Symbol;
import org.jvnet.hudson.annotation_indexer.Index;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
@@ -152,5 +153,29 @@ public static SymbolLookup get() {
return j.getInjector().getInstance(SymbolLookup.class);
}

/**
* Get the {@link Symbol} value for the class of the given object, if the annotation is present
*
* @param o An object
* @return The {@link Symbol} annotation value for the class that object represents, or null if the annotation is not present.
*/
public static String getSymbolValue(Object o) {
return getSymbolValue(o.getClass());
}

/**
* Get the {@link Symbol} value for the given class, if the annotation is present
*
* @param c A class.
* @return The {@link Symbol} annotation value for the given class, or null if the annotation is not present.
*/
public static String getSymbolValue(@Nonnull Class<?> c) {
Symbol s = c.getAnnotation(Symbol.class);
if (s != null && s.value()[0] != null) {
return s.value()[0];
}
return null;
}

private static final Logger LOGGER = Logger.getLogger(SymbolLookup.class.getName());
}
@@ -61,4 +61,18 @@ public void descriptorLookup() {
assertThat(lookup.findDescriptor(Fishing.class, "net"), is(sameInstance((Descriptor)fishingNetDescriptor)));
assertThat(lookup.findDescriptor(Tech.class, "net"), is(sameInstance((Descriptor)internetDescriptor)));
}

@Test
public void symbolValueFromObject() {
assertNull(SymbolLookup.getSymbolValue("some-string"));
assertEquals("net", SymbolLookup.getSymbolValue(fishingNetDescriptor));
assertEquals("foo", SymbolLookup.getSymbolValue(foo));
}

@Test
public void symbolValueFromClass() {
assertNull(SymbolLookup.getSymbolValue(String.class));
assertEquals("net", SymbolLookup.getSymbolValue(FishingNet.DescriptorImpl.class));
assertEquals("foo", SymbolLookup.getSymbolValue(Foo.class));
}
}

0 comments on commit bc43d9a

Please sign in to comment.