Can we get closure information when using reflection on Grails? - grails

So we have a task to produce documentation for a REST API we are building. We are using Grails and it's all nicely defined in UrlMappings but that doesn't generate a documentation the way we want.
We thought of using reflection to extract the mappings into some format that we can manipulate through code.
There's also a possibility of maybe parsing the UrlMappings as a text file and produce some data format that we can manipulate.
For the reflection bit, can we extract info from Closures - such as properties, methods, etc - in Groovy?
Any other ideas would be appreciated.

Related

OpenOffice Basic macro vs openoffice API

I am planning to write openoffice macros using openoffice basic language. I am struggling to find the objects and methods related to openoffice documents. I googled and found some example codes and the API page openoffice.org/api but I could not locate where are the objects and methods in the api.
For example read this line of code
oSheet=oDoc.Sheets.getByName("Sheet1")
There is no method/object in the name "sheets", getByName when i referred the API page (openoffice.org/api). Where should I look for?
Short answer:
Sheets can be obtained by name, index or enumeration through the interface com.sun.star.sheet.XSpreadsheetDocument.
oSheets = oDoc.getSheets().getByIndex(0)
Explanation:
Then what is oDoc.Sheets?
To really understand the UNO API, it's best to use Java. Here is some code that you don't need to read, but I show it here because the results will be helpful.
XSpreadsheetDocument xSpreadSheetDocument = UnoRuntime.queryInterface(
XSpreadsheetDocument.class, xComponent);
XSpreadsheets xSpreadsheets = xSpreadSheetDocument.getSheets();
XPropertySet propSet = (XPropertySet)UnoRuntime.queryInterface(
XPropertySet.class, xComponent);
Using code like this, we can verify that getSheets() works in Java, but Sheets() does not. Nor is there any property named Sheets in the property set. In short, there is no Sheets in Java, so Sheets must not be part of the UNO API.
Now, languages that are less strict than Java, such as Basic and Python, have various shortcuts to work with UNO. For one thing, they do not need queryInterface to get a specific object, because they don't care about the type.
Another shortcut is that they provide properties that can be used as alternatives to the method of a particular interface. So, the method XSpreadsheetDocument.getSheets() gets a shortcut property Sheets. Similarly, the method XDocumentSupplier.getDocumentProperties() gets a shortcut property DocumentProperties. Both of these properties are members of oDoc when writing in Basic or Python.
Finding Information:
Searching through the API documentation is not easy. Before trying that, it's better to search for example code. Andrew Pitonyak's macro document contains a set of examples that cover many needs, and searching in Google will bring up example code from sites such as ask.libreoffice.org.
Also, an introspection tool such as XrayTool or MRI is essential. Such tools give a list of all properties and methods available for an object, and since the list is flat, there is less need to dig into the interface of an inherited interface of another interface and so on.

Apache Beam Coder for org.json.JSONObject

I am writing a data pipeline in Apache Beam that reads from Pub/Sub, deserializes the message into JSONObjects and pass them to some other pipeline stages. The issue is, when I try to submit my code I get the following error:
An exception occured while executing the Java class. Unable to return a default Coder for Convert to JSON and obfuscate PII data/ParMultiDo(JSONifyAndObfuscate).output [PCollection]. Correct one of the following root causes:
[ERROR] No Coder has been manually specified; you may do so using .setCoder().
[ERROR] Inferring a Coder from the CoderRegistry failed: Unable to provide a Coder for org.json.JSONObject.
[ERROR] Building a Coder using a registered CoderProvider failed.
[ERROR] See suppressed exceptions for detailed failures.
[ERROR] Using the default output Coder from the producing PTransform failed: PTransform.getOutputCoder called.
basically the error says Beam cannot find a Coder for org.json.JSONObject objects. I have no idea where to get such a coder or how to build one. Any ideas?
Thanks!
The best starting point for understanding coders is in the Beam Programming Guide: Data Encoding and Type Safety. The short version is that Coders are used to specify how different types of data are encoded to and from byte strings at certain points in a Beam pipeline (usually at stage boundaries). Unfortunately there is no coder for JSONObjects by default, so you have two options here:
Avoid creating JSONObjects in PCollections. Instead of passing JSONObjects throughout your pipeline, you could extract desired data from the JSON and either pass it around as basic data types, or have your own class encapsulating the data you need. Java's basic data types all have default coders assigned, and coders can easily be generated for classes that are just structs of those types. As a side benefit, this is how Beam pipelines are expected to be built, so it's likely to work more optimally if you stick with basic data and well-known coders when possible.
If JSONObjects are necessary, you'll want to create a custom coder for them. The programming guide contains info for how to set a custom coder as a default coder. For the implementation itself, the easiest way with JSONObject is to encode it to a JSON string with JSONObject.toString and then decode it from the string with JSONObject's string constructor. For details on how to do this, check out the programming guide above and take a look at the Coder documentation.

How can I override the #context when loading json-ld in Jena

I am loading a JSON-LD document using Jena:
Model mj = RDFDataMgr.loadModel([filename]);
The actual content being loaded is here: http://build.fhir.org/account-example.jsonld
Jena goes off and resolves the context, and returns an error (LOADING_REMOTE_CONTEXT_FAILED - lovely suppression of the actual cause in the Jena code :-(). But I need to override the context anyway, and use a different source, because I am doing the build for what will be posted at build.fhir.org, and I need to use my local versions instead. I can't see how to override the context resolution
Alternatively, I could use the loading method documented here: https://github.com/jsonld-java/jsonld-java#code-example - but I have no idea how to get to a Jena graph from there (and I haven't figured out how make the custom resolution work in my Eclipse context either)
How can I get to a Jena graph using a context defined in code somewhere?
I think Jena devs are subscribed to the relevant tag RSS streams. They might weigh in on the clarity of LOADING_REMOTE_CONTEXT_FAILED error. But it seems pretty clear to me.
In order to override the context, you can use read(Model model, String uri, Context context) method. ModelFactory.createDefaultModel() will create a intance of a Model that you can pass as a first argument. See more examples here: https://github.com/apache/jena/tree/master/jena-arq/src-examples/arq/examples/riot
Alternative library is not Jena-compatible (nor RDF4J, which strikes me as rather silly), so there is no easy way to use it with Jena-dependent code.
Finally, you provided the code example for getting a model but now mention a graph – there is a method for that as well: read(Graph graph, String uri, Context context).

What does # do in Dart programs?

I've just spent 20 hours in learning about the basics of Dart language, but when I find the # prefix in an open-source Dart program such as here, which I found that most programs use, I wonder what the # directive do in those programs...
For your information, the official documentation says the following:
Metadata
Use metadata to give additional information about your code. A metadata annotation begins with the character #, followed by either a reference to a compile-time constant (such as deprecated) or a call to a constant constructor.
Three annotations are available to all Dart code: #deprecated, #override, and #proxy. For examples of using #override and #proxy, see the section called “Extending a Class”. Here’s an example of using the #deprecated annotation:
However, what "additional information" does the # directive add to the code? If you create an instance by writing the following constructor
#todo('seth', 'make this do something')
, instead of the following constructor, which is the default:
todo('seth", 'make this do something')
, what is the benefit I can get from the first constructor?
I've got that using the built-in metadata such as #deprecated and #override can give me an advantage of being warned in running the app, but what can I get from the case on the custom #todo, or the aforementioned linked sample code over Github?
Annotations can be accessed through the dart:mirrors library. You can use custom annotations whenever you want to provide additional information about a class, method, etc. For instance #MirrorsUsed is used to provide the dart2js compiler with extra information to optimize the size of the generated JavaScript.
Annotations are generally more useful to framework or library authors than application authors. For instance, if you were creating a REST server framework in Dart, you could use annotations to turn methods into web resources. For example it might look something like the following (assuming you have created the #GET annotation):
#GET('/users/')
List<User> getUsers() {
// ...
}
You could then have your framework scan your code at server startup using mirrors to find all methods with the #GET annotation and bind the method to the URL specified in the annotation.
You can do some 'reasoning' about code.
You can query for fields/methods/classes/libraries/... that have a specific annotation.
You acquire those code parts using reflection. In Dart reflection is done by the 'dart:mirrors' package.
You can find an code example here: How to retrieve metadata in Dartlang?
An example where annotations are regularly used is for serialization or database persistence where you add metatdata to the class that can be used as configuration settings by the serialization/persistence framework to know how to process a field or method.
For example you add an #Entity() annotation to indicate that this class should be persisted.
On each field that should be persisted you add another annotation like #Column().
Many persistence frameworks generate the database table automatically from this metadata.
For this they need more information so you add a #Id() on the field that should be used as primary key and #Column(name: 'first_name', type: 'varchar', length: 32) to define parameters for the database table and columns.
This is just an example. The limit is you imagination.

Grails Controllers - dynamic methods documentation

I was wondering what is the best way of finding out what dynamic methods are added to certain variables. I don't see it documented in the documentation or the javadoc of the controller class.
An example is the params object in a grails controller:
params.int('max')
The int methods is injected and I'd like to know what extra methods are available for this particular map.
I got my copy of The Definitive Guide to Grails 2 several days ago and as it is stated here, there are 8 converting methods for primitive types:
... Grails provides convenience methods for doing this type of
conversion on request parameters. Those methods are available on the
params object and have names that correspond to all eight of the
primitive types defined by Java (Boolean, byte, char, short,
int, long, float and double). The methods each accept one or
two arguments. The first argument is the name of the request parameter
that is to be converted, and the optional second argument is a default
value that will be returned if a corresponding request parameter
cannot be found or if an error occurs during the conversion ...
And there is also mentioned a converting method named list:
... Another type of converting method is named list. The list method is useful
when dealing with multiple request parameters of the same name ...
Brown, J. S., Rocher, G., (2013). The Definitive Guide to Grails 2. New York: Apress.
ISBN:978-1-4302-4377-9
Amazon
I hope it's not against any copyright to cite from it...
And yes, I consider books a great source for questions that go a little further and I definitely would recommend The Definitive Guide to Grails 2 to you or any other programmer who wants to gain a deeper understanding on Grails. Also, the Grails Mailing Lists and the Groovy Zone over at dzone.com are good resources for any Grails related questions and/or news. The Groovy JDK documentation is also of great help, as #doelleri mentioned in his answer.
Unfortunately, those methods are poorly documented. The best documentation there is on them is in the Simple Type Converters section of the docs. They are also briefly mentioned in the Release Notes for Grails 1.2, which is perhaps even more unhelpful.
The Groovy JDK is also a good resource to see what methods Groovy adds to the standard Java classes.

Resources