I have an xml schema (generated automatically using trang) which keeps changing. These changes are not very elaborate. Only some elements are added or deleted from this schema. From this schema, I am generating java classes (using cxf) by which I will unmarshall the xml document.
As schema changes, my auto-generated java classes also change. Again, as with schema, changes in java classes are not very big. For instance, if an element say elemA
is added to schema; some related functions say getElemA()
and setElemA()
are added to auto-generated java class.
Now how would I make sure that a particular function exists in these auto-generated classes? One solution is to hand-write the schema such that all possible elements of xml are covered. This is what I'll ultimately do. But for now, I have not fixed the format of xml file.
UPDATE :
There is a possibility that a method getElemA()
may be defined in auto-generated classes. I do not have control over the auto-generation of these classes. But in my main class, if have following code,
If method getElemA exists then ElemA elemA = getElemA()
This code will always be there in my main class. If method getElemA()
is generated in one of the auto-generated class then there is no problem. But if this method is not generated then compilers complain that this method does not exists in any of the class.
Is there any way that I can make compiler not to complain about this function at compile time?
While defining a method, remember that the method name must be a verb and start with a lowercase letter. If the method name has more than two words, the first name must be a verb followed by an adjective or noun. In the multi-word method name, the first letter of each word must be in uppercase except the first word.
In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined.
The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.
The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
One method is mentioned by @missingfaktor and another is below (if you know the name and parameters of the api).
Say you have one method which takes no args:
Method methodToFind = null; try { methodToFind = YouClassName.class.getMethod("myMethodToFind", (Class<?>[]) null); } catch (NoSuchMethodException | SecurityException e) { // Your exception handling goes here }
Invoke it if present:
if(methodToFind == null) { // Method not found. } else { // Method found. You can invoke the method like methodToFind.invoke(<object_on_which_to_call_the_method>, (Object[]) null); }
Say you have one method which takes native int
args:
Method methodToFind = null; methodToFind = YouClassName.class.getMethod("myMethodToFind", new Class[] { int.class });
Invoke it if present:
if(methodToFind == null) { // Method not found. } else { // Method found. You can invoke the method like methodToFind.invoke(<object_on_which_to_call_the_method>, invoke(this, Integer.valueOf(10))); }
Say you have one method which takes boxed Integer
args:
Method methodToFind = null; methodToFind = YouClassName.class.getMethod("myMethodToFind", new Class[] { Integer.class });
Invoke it if present:
if(methodToFind == null) { // Method not found. } else { // Method found. You can invoke the method like methodToFind.invoke(<object_on_which_to_call_the_method>, invoke(this, Integer.valueOf(10))); }
Using the above soln to invoke method won't give you compilation errors. Updated as per @Foumpie
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With