Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-26093] Extending ParameterValue symbol handling to SymbolLoo…
…kup where it belongs anyway.
  • Loading branch information
jglick committed Feb 5, 2018
1 parent a07d4f8 commit 8d4446f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
Expand Up @@ -6,6 +6,7 @@
import hudson.PluginWrapper;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.model.ParameterValue;
import jenkins.model.Jenkins;
import org.codehaus.groovy.tools.Utilities;
import org.jenkinsci.Symbol;
Expand Down Expand Up @@ -237,6 +238,12 @@ public static SymbolLookup get() {
Symbol s = c.getAnnotation(Symbol.class);
if (s != null) {
Collections.addAll(symbolValues, s.value());
} else if (j != null && ParameterValue.class.isAssignableFrom(c)) { // TODO JENKINS-26093 hack, pending core change
try {
symbolValues.addAll(getSymbolValue(c.getClassLoader().loadClass(c.getName().replaceFirst("Value$", "Definition"))));
} catch (ClassNotFoundException x) {
// ignore
}
}
}
return symbolValues;
Expand Down
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.structs.describable;

import com.google.common.collect.Maps;
import com.google.common.primitives.Primitives;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import groovy.lang.GString;
Expand Down Expand Up @@ -623,35 +622,8 @@ public UninstantiatedDescribable uninstantiate2(T o) throws UnsupportedOperation
* Finds a symbol for an instance if there's one, or return null.
*/
/*package*/ static String symbolOf(Object o) {
if (o instanceof Describable) {
Descriptor d = ((Describable) o).getDescriptor();
return symbolOf(d.getClass());
}
// TODO JENKINS-26093 hack, pending core change
if (o instanceof ParameterValue) {
try {
Class def = o.getClass().getClassLoader().loadClass(o.getClass().getName().replaceFirst("Value$", "Definition"));
Jenkins j = Jenkins.getInstance();
if (j==null) throw new IllegalStateException();
Descriptor d = j.getDescriptor(def);
if (d!=null)
return symbolOf(d.getClass());
} catch (ClassNotFoundException x) {
// ignore
}
}
return null;
}

private static String symbolOf(Class<?> c) {
Symbol symbol = c.getAnnotation(Symbol.class);
if (symbol!=null) {
String[] v = symbol.value();
if (v.length>0) {
return v[0];
}
}
return null;
Set<String> symbols = SymbolLookup.getSymbolValue(o);
return symbols.isEmpty() ? null : symbols.iterator().next();
}

/**
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.structs;

import hudson.model.BooleanParameterValue;
import hudson.model.Descriptor;
import java.util.Collections;
import java.util.Set;
Expand Down Expand Up @@ -86,6 +87,13 @@ public void symbolValueFromClass() {
assertEquals(fooSet, SymbolLookup.getSymbolValue(Foo.class));
}

@Issue("JENKINS-26093")
@Test
public void parameters() {
assertEquals(Collections.singleton("booleanParam"), SymbolLookup.getSymbolValue(BooleanParameterValue.class));
assertEquals(Collections.singleton("booleanParam"), SymbolLookup.getSymbolValue(new BooleanParameterValue("flag", true)));
}

@Issue("JENKINS-37820")
@Test
public void descriptorIsDescribable() {
Expand Down
Expand Up @@ -616,6 +616,7 @@ public static final class DefaultStructArray {
map(CLAZZ, "StringParameterValue", "name", "n", "value", "stuff"),
map(CLAZZ, "TextParameterValue", "name", "text", "value", "here\nthere"))),
"TakesParams;BooleanParameterValue:flag=true;StringParameterValue:n=stuff;TextParameterValue:text=here\nthere");
assertEquals("(parameters=[@booleanParam$BooleanParameterValue(name=flag,value=true)])", DescribableModel.uninstantiate2_(new TakesParams(Collections.<ParameterValue>singletonList(new BooleanParameterValue("flag", true)))).toString());
}
public static final class TakesParams {
public final List<ParameterValue> parameters;
Expand Down

0 comments on commit 8d4446f

Please sign in to comment.