Import Java Custom Method in Xquery - java

I am using Weblogic Integration framework. While transforming one XML format to another using .xq file, I want to apply some logic written in a custom Java Class.
For example, XML1 has tag: <UnitCode>XYZ</UnitCode>
Custom Java Class:
public class unitcodemapper{
public static String getMappedUnitCode(String unitCode){
if(unitCode=="XYZ")
return <<value from DB table>>
else
return unitCode;
}
}
XML2 will have a tag: <UnitCode>unitcodemapper.getMappedUnitCode(XML1/UnitCode)</UnitCode>
I cannot find any documentation or example to do this. Can someone please help in understanding how this can be done?

This is known as an "extension function". The documentation for your XQuery implementation should have a section telling you how to write such functions and plug them into the processor. (The details may differ from one XQuery processor to another, which is why I'm referring you to the manual.)

Whilst #keshlam mentions Extension Functions, which are indeed supported by many implementations each with their own API.
I think perhaps what you are looking for instead is Java Binding from XQuery. Many implementations also support this and tend to use the same approach. I do not know whether WebLogic supports this or not! If it does, the trick is to use java: at the start of your namespace URI declaration, you can then use the fully qualified Java class name of a static class, each static method you may then call directly from that namespace.
You can from two examples of implementations that offer the same Java Binding from XQuery functionality here:
http://exist-db.org/exist/apps/doc/xquery.xml#calling-java
http://docs.basex.org/wiki/Java_Bindings
These could serve as examples for you to try on WebLogic to see if it is supported in the same way. However, I strongly suggest you check their documentation as they may take a different approach.

Related

Pattern for processing custom Java annotations

I have read a lot of tutorials about Java annotations lately and I like the idea of creating a custom one. Most articles cover the very basic idea and fairly simple implementations. I'm missing a proper pattern to process my annotation, though.
Lets say I have a custom annotation #Foobar to initialize fields. I need to pass all classes that use this annotation to my processor, let's call it FoobarProcessor:
public class AnnotatedClass {
#Foobar
private String test = "";
static {
FoobarProcessor.process(AnnotatedClass.class);
}
}
Is there any approach to overcome this drawback? Is there any single point that all classes pass, where I can easily apply my annotation processor?
A common pattern to process annotations or any language elements is the visitor pattern.
Java even includes a standard API for to this: SimpleElementVisitor7
If you need an example implementation of a processor using the pattern, take a look at the code of the PrintingProcessor. The processor traverses all kind of elements it find and prints some information. It's used for javac's non-standard Xprint option (you can try it in your command line: javac -Xprint java.lang.Object).
You need to register the processor in a META-INF file. This answer should give you more info:
What is the default annotation processors discovery process?
If you want to process your annotation at Runtime, you need to scan the classes from information of the classLoader, this answer give more information about it:
How do I read all classes from a Java package in the classpath?

Using Dot Operator(JSTL) in Java

I have a requirement where I need to traverse a hierarchy of Java beans and the hierarchy is different based on the starting point. What would be ideal is if I would be able to use the "dot operator" from JSTL in my Java class.
Then I can have a static map of Strings to describe my hierarchy..something like:
clazz1=attribute1.attribute2
clazz2=attribute3.attribute4
I look up which class and which attributes I need to drill down and go to the root object.
I am coding for it anyway, just checking if BeanUtils etc had such a facility already since seems to me like it can be an useful feature.
You could write some of your code in groovy - it compiles to java bytecode so interoperates perfectly.

Java API to get metadata about Java Source Code

I would like to create some reverse egineered design docs based on Java code (not bytecode), instead of writing my own interpreter, what tools and APIs are available to traverse Java code, using Java code?
Reflection is on bytecode, and is limited to the method level, I want to "objectize" also the method code.
Java doc is ignoring the code itself and only based on comments, automatic UML sequnces are too strict
E.g. an API like this (forgive my ignorance of official Programming Languages Structure terms):
JavaCodeDom jcd = new JavaCodeDom(new File(pathToJavaSource), CompilerEnum.Java16)
List <ClassSrc> classes = jcd.getClasses();
ClassSrc cls = classes.get(0);
Map<MethodSignatureSrc,MethodSrc> methods = cls.getMethodsMap();
MethodSrc main = mothds.get(new MethodSignatureSrc(Modifiers.Public, Modifiers.Static, ReturnTypes.Void, "main", new MethodParams(String[].class))
List<StatementSrc> statements = main.getStatements();
for(StatementSrc statement : statements){
if(statement.getType()==StatementTypes.Assignment()){
AssignmentStatementSrc assignment = (AssignmentStatementSrc)statement;
Identifier src = assignment.getAssigneeVariable();
ExpressinoSrc = assignment.getAssignmentValue();
}
}
List<AnnotationsSrc> annotations = cls.getAnnotations();
There are several such APIs in existence (and delivered with the JDK), some of them build in in the Java Compiler (javac).
The most extensive is the Compiler Tree API, which gets you access to individual expressions (and subexpressions) in the Java source.
The language model API models types and members of types (constructors, methods, fields) - it is used by the compiler tree API and also for annotation processing. It does not give access to the contents of the methods.
Of course, on runtime you have the Reflection API (java.lang.Class and java.lang.reflect.*, together with java.lang.annotation).
To use the compiler tree API, you have to invoke the compiler, with the compiler API.
Additionally, there is the Doclet API for Javadoc, which gives you a similar view like the language model API, but additionally with the documentation comments (and parsed tags).
I once used a combination of Doclet API and Compiler Tree API to format source code beautifully (this is not online, sadly).
BCEL supports reading an manipulating Java class files. (I have not used it myself, but saw it used successfully in a third-party product.)
The Byte Code Engineering Library is intended to give users a convenient
possibility to analyze, create, and manipulate (binary) Java class files
(those ending with .class). Classes are represented by objects which
contain all the symbolic information of the given class: methods,
fields and byte code instructions, in particular.
If you're just interested in decompiling, you might find it sufficient to decompile to source code. Here's a comparison of several options for Java.
I seems ANTLR is one option, but I haven't used it
This seems to answer my question: How to generate AST from Java source-code? ( Spoon )

Java Interface Reflection Alternatives

I am developing an application that makes use of the Java Interface as more than a Java interface, i.e., During runtime, the user should be able to list the available methods within the interface class, which may be anything:
private Class<? extends BaseInterface> interfaceClass.
At runtime, I would like to enum the available methods, and then based on what the user chooses, invoke some method.
My question is: Does the Java "Interface" architecture provide any method for me to peek and invoke methods without using the Reflection API?
I wish there were something like this (Maybe there is):
private Interface<? extends BaseInterface> interfaceAPI;
public void someMethod(){
interfaceAPI.listMethods();
interfaceAPI.getAnnotations();
}
Maybe there is some way to use Type Generics to accomplish what I want?
Thanks,
Phaedrus
This is exactly what Reflection was built for.
Take a look at Apache Commons BeanUtils. It's an excellent library for programmatically discovering an object's methods and properties easily (i.e. without writing low-level reflection code). I've used it on several projects. It also provides an easier API for accessing those object members once they're discovered. PropertyUtils and MethodUtils are good places to start.
My question is: Does the Java "Interface" architecture provide any method for me to peek and invoke methods without using the Reflection API?
a) No, reflection is the only way to do that
b) don't do that at all. If you want a dynamic language, use Groovy. It's syntax is (can be) almost identical to java, but it has most of this functionality built in already. (Many other languages will also work, but Groovy is closest to Java when comparing the syntax)
Reflection is the only way to list methods of an arbitrary object. Well, parsing the bytecode also works, but it's tedious.
Unfortunately it looks like reflection is the only way to go. Unfortunate because reflection is not only slower but programming and maintaining is a hassle. So I suggest you use apache bean util library. (specifically methodutils.)
Although there's no way around reflection here, it can be made relatively painless if you use annotations to mark your callable methods. An example of this approach at work is the javax.jws annotation set for webservices. The ideas used there can be really useful, you can define a display name for each of your methods/parameters for example.
Reflection with Annotations is working Nicely
for (Method m : interfaceClass.getMethods()) {
if (m.isAnnotationPresent(UIf.class)) {
UIf x = m.getAnnotation(UIf.class);
addDefinedCommand( new CommandDef(
interfaceClass,
u,
x.name().isEmpty() ? m.getName() : x.name(),
x.description())
);
}
}

Java Annotations

What is the purpose of annotations in Java? I have this fuzzy idea of them as somewhere in between a comment and actual code. Do they affect the program at run time?
What are their typical usages?
Are they unique to Java? Is there a C++ equivalent?
Annotations are primarily used by code that is inspecting other code. They are often used for modifying (i.e. decorating or wrapping) existing classes at run-time to change their behavior. Frameworks such as JUnit and Hibernate use annotations to minimize the amount of code you need to write yourself to use the frameworks.
Oracle has a good explanation of the concept and its meaning in Java on their site.
Also, are they unique to Java, is there a C++ equivalent?
No, but VB and C# have attributes which are the same thing.
Their use is quite diverse. One typical Java example, #Override has no effect on the code but it can be used by the compiler to generate a warning (or error) if the decorated method doesn't actually override another method. Similarly, methods can be marked obsolete.
Then there's reflection. When you reflect a type of a class in your code, you can access the attributes and act according to the information found there. I don't know any examples in Java but in .NET this is used by the compiler to generate (de)serialization information for classes, determine the memory layout of structures and declare function imports from legacy libraries (among others). They also control how the IDE form designer works.
/EDIT: Attributes on classes are comparable to tag interfaces (like Serializable in Java). However, the .NET coding guidelines say not to use tag interfaces. Also, they only work on class level, not on method level.
Anders gives a good summary, and here's an example of a JUnit annotation
#Test(expected=IOException.class)
public void flatfileMissing() throws IOException {
readFlatFile("testfiles"+separator+"flatfile_doesnotexist.dat");
}
Here the #Test annotation is telling JUnit that the flatfileMissing method is a test that should be executed and that the expected result is a thrown IOException. Thus, when you run your tests, this method will be called and the test will pass or fail based on whether an IOException is thrown.
Java also has the Annotation Processing Tool (apt) where not only you create annotations, but decide also how do these annotations work on the source code.
Here is an introduction.
To see some cool stuff you can do with Annotations, check out my JavaBean annotations and annotation processor.
They're great for generating code, adding extra validations during your build, and I've also been using them for an error message framework (not yet published -- need to clear with the bosses...).
The first thing a newcomer to annotations will ask about annotations is: "What is an annotation?" It turns out that there is no answer to this question, in the sense that there is no common behavior which is present in all of the various kinds of java annotations. There is, in other words, nothing that binds them together into an abstract conceptual group other than the fact that they all start with an "#" symbol.
For example, there is the #Override annotation, which tells the compiler to check that this member function overrides one in the parent class. There is the #Target annotation, which is used to specify what kinds of objects a user defined annotation (a third type of construct with nothing in common with other kinds of annotation) can be attached to. These have nothing to do with one another except for starting with an # symbol.
Basically, what appears to have happened is that some committee responsible for maintaining the java language definition is gatekeeping the addition of new keywords to the java language, and therefore other developers are doing an end run around that by calling new keywords "annotations". And that's why it is hard to understand, in general what an annotation is: because there is no common feature linking all annotations that could be used to put them in a conceptual group. In other words, annotations as a concept do not exist.
Therefore I would recommend studying the behavior of every different kind of annotation individually, and do not expect understanding one kind of annotation to tell you anything about the others.
Many of the other answers to this question assume the user is asking about user defined annotations specifically, which are one kind of annotation that defines a set of integers or strings or other data, static to the class or method or variable they are attached to, that can be queried at compile time or run time. Sadly, there is no marker that distinguishes this kind of annotation from other kinds like #interface that do different things.
By literal definition an annotation adds notes to an element. Likewise, Java annotations are tags that we insert into source code for providing more information about the code. Java annotations associate information with the annotated program element. Beside Java annotations Java programs have copious amounts of informal documentation that typically is contained within comments in the source code file. But, Java annotations are different from comments they annotate the program elements directly using annotation types to describe the form of the annotations. Java Annotations present the information in a standard and structured way so that it could be used amenably by processing tools.
When do you use Java's #Override annotation and why?
The link refers to a question on when one should use the override annotation(#override)..
This might help understand the concept of annotation better.Check out.
Annotations when it comes to EJB is known as choosing Implicit middle-ware approach over an explicit middle-ware approach , when you use annotation you're customizing what you exactly need from the API
for example you need to call transaction method for a bank transfer :
without using annotation :
the code will be
transfer(Account account1, Account account2, long amount)
{
// 1: Call middleware API to perform a security check
// 2: Call middleware API to start a transaction
// 3: Call middleware API to load rows from the database
// 4: Subtract the balance from one account, add to the other
// 5: Call middleware API to store rows in the database
// 6: Call middleware API to end the transaction
}
while using Annotation your code contains no cumbersome API calls to use the middle-
ware services. The code is clean and focused on business logic
transfer(Account account1, Account account2, long amount)
{
// 1: Subtract the balance from one account, add to the other
}

Categories