Actually, I think this just uses the Java reflection packages, but anyway...
I'd not done any reflection before in Java or Groovy and ended up wanting to do some for a workflow export script for a customer.
Specifically I wanted to iterate over the public fields of a class as they were being used as constants to store custom field names:
public class CustomFields {
public final static String CF_PLATFORM = "Platform Response"
public final static String CF_FUNC_TEAM = "Functional Team Response"
// Platforms relevant to specific requirement
public final static String CF_IMPACTED_PLATFORMS = "Impacted Platform/s"
public static final String CF_LEAD_PLATFORM = 'Lead Platform'
// Corresponding Groups for Issue Security Level
public final static String CF_IMPACTED_OFFERING_GROUPS = "Impacted Offering Groups"
// cross-product field representing those who have not replied
public final static String CF_NO_RESPONSE_FROM = "No response from"
// cross-product field representing those who said No Impact
public final static String CF_NO_IMPACT = "No Impact Platforms/Teams"
}
Turns out its quite easy, you can do the following to get a list of the values of those public fields:
final CustomFields cf = new CustomFields()
final Field[] customFieldDefns = CustomFields.class.getDeclaredFields()
final String[] values = customFieldDefns.findAll {
// For those who don't know, the magic variable 'it' here refers to the current element in the collection
Modifier.isPublic(it.modifiers)
}.collect { Field f ->
// This returns the value of the field from the cf object (instance of CustomFields)
f.name + " has value: " + f.get(cf)
}
Now values
contains the entries:
- CF_PLATFORM has value: Platform Response
- CF_FUNC_TEAM has value: Functional Team Response
- etc :)
- 09 Oct 2018 » A strange bug on AWS Lambda
- 17 Jan 2018 » How to run Karma tests in browsers in Docker
- 07 Dec 2017 » Switching from Javascript to Typescript
- 30 Oct 2017 » Fun with React event handlers
- 17 Jul 2017 » Switching from Groovy to Java
- 24 May 2017 » Useful Git Aliases
- 27 Mar 2017 » Practical Ratpack Promises
- 03 Nov 2016 » Custom Content in Forms for Confluence Connect
- 04 Oct 2016 » Checking user permissions from REST calls
- 30 Sep 2016 » Using the reflection API in Confluence
- 28 Sep 2016 » Creating a custom Confluence Blueprint
- 06 Sep 2016 » ReactJS in Forms for Confluence Connect
- 25 Apr 2016 » Migrating to ES6 in Atlassian Add-ons
- 17 Mar 2016 » All kinds of things I learnt trying to performance test against Fisheye/Crucible
- 24 Dec 2015 » Adaptavist’s Holiday Gift of Atlassian Deployment Automation
- 17 Dec 2015 » Getting a Custom Field value safely
- 07 Dec 2015 » Putting Google Analytics to work with plugins for Confluence
- 02 Dec 2015 » Devoxx Voting, A retrospective
- 25 Nov 2015 » Some things I've learnt about SingleSelect
- 15 Oct 2015 » Using SOY for JIRA actions
- 26 Sep 2015 » Object Reflection in Groovy
- 22 Sep 2015 » Introducing Adaptavist Labs