Problem in html to java class parsing using XMLC - java

i have compiled HTML page using XMLC jar file command..
java org.enhydra.xml.xmlc.commands.xmlc.XMLC -d \core\model\template -class core.model.template.patientPrescriptionsHTML \\core\\model\\template\\PrePrintPrescription.html
it propogate error like
patientPrescriptionsHTML.java:17: core.model.template.patientPrescript
ionsHTML is not abstract and does not override abstract method renameNode(org.w3
c.dom.Node,java.lang.String,java.lang.String) in org.w3c.dom.Document
public class patientPrescriptionsHTML extends org.enhydra.xml.xmlc.html.HTMLObje
ctImpl implements org.enhydra.xml.xmlc.XMLObject, org.enhydra.xml.xmlc.html.HTML
Object {
^
1 error
Error: compile of generated java code failed
please suggest...

In
class A extends B implements C {
}
A must implement all abstract methods from B and C (because C is an interface, all of C's methods count implicitly as abstract.)
Unless, of course, A itself is abstract.

Related

Java Generic Interface 'Type Argument Not Within Bounds' Error

I seems to be running into a compilation error with a generic interface. The goal is to create an interface for use in my test classes the defines the common methods of a domain class and a generated DTO class. I did some digging and none of the answers I have found so far seem applicable to what I am trying to do. Most of the answers either refer back to this: class not within type bounds or they suggest using wildcards which is not an option here.
When I try to compile, I get the following error:
[uberCompile] BrandDto.java:10: error: type argument T#1 is not within bounds of type-variable T#2
[uberCompile] public class BrandDto<T extends SubCollectionDto> implements IBrand<T> {
[uberCompile]
[uberCompile] where T#1,T#2 are type-variables:
[uberCompile] T#1 extends SubCollectionDto declared in class BrandDto
[uberCompile] T#2 extends ISubCollection declared in interface IBrand
My class structure looks like this:
interface IBrand<T extends ISubCollection>
interface ISubCollection
class Brand<T extends SubCollection>
extends Entity
implements IBrand<T>
class SubCollection
extends Entity
implements ISubCollection
The generated DTO classes look like:
class SubCollectionDto
implements ISubCollection
class BrandDto<T extends SubCollectionDto>
implements IBrand<T>
I'm really racking my brain trying to understand what I am doing wrong here. Any help would be greatly appreciated.
I think I finally figured it out. It was actually a classpath issue that had nothing to do with the generics. When I originally set up the DTO classes they were under the 'src' directory. I later moved them into a different directory, but apparently the old folder never got removed. So eclipse was looking at the current, correct set of files while ant was looking at the old directory. No idea why that was causing the error message I saw, but it is working now.

JAVA - Problems with Override on Interface and Class

Salve...
some Problem for me in Java. I have generated an Interface and an Class implements this Interface. In the Interface i have declared one Method... and in the Class i will Override this. But not works! But don`t why....
See my example Interface:
public interface IMyClass extends IInterfaceA<IInterfaceB> {
public List<IInterfaceB> getMethod(Integer id);
}
See now my Class File:
public class MyClass implements IMyClass {
#Override
public List<IInterfaceB> getMethod(Integer id) {
return anything;
}
}
Problem is... i think i make no mistake. And next - i surprise that i have another Interfaces and Classes with the same Logic - and it will work and make no Problems!
In my another Interfaces & Classes i don`t need to make the Class with implements the Interface an Abstract Class.
When i make an build... Jenkins will surprise me with this Message:
[ERROR] COMPILATION ERROR :
error: MyClass is not abstract and does not override abstract method getMethode() in IMyClass [ERROR]
error: method does not override or implement a method from a supertype [INFO] 2 errors [INFO]
Solved . The " solution " - was a build problem . After tens of Maven Clean Installs and re- builds he has the connection interface and class savvy and it goes... oh men ;)

Generics Compile Time error

This is from a complex build system. And I am looking for help to localize the problem.
It seems like I am hitting this problem: Covariant Return Type in Interface not compiling via Javac
My code compiles fine when I compile after cleaning all compiled code. But if I open AImpl.java and change one character (modify any string literal, which shouldn't generate any compile time error) and compile then I get following error:
AImpl.java:22: types BImpl<T> and BImpl<T> are incompatible; both define waitFor(org.openqa.selenium.support.ui.ExpectedCondition,java.lang.String,long,java.util.Collection), but with unrelated return types
public class AImpl<T extends AImpl<T>> extends BImpl<T> implements A<T> {
This is specific to AImpl.java ; changing any other code doesn't generate this vicious error.
BImpl.java is defined as:
public class BImpl<T extends BImpl<T>> extends CImpl<T> implements B<T> {
The erroring method waitFor is defined in CImpl as:
public T waitFor(ExpectedCondition<?> condition, String message, long timeOutInMillis, Collection<Class<? extends Throwable>> exceptions)

Dynamically loading classes which adhere to an interface

I have several classes which implement two interfaces. All of them implement the BaseInterface and some other interface which is specific to them.
I want to be able to use the loadClass method below to instantiate classes which are referred to in a .properties file and call the common method they all contain (because they implement BaseInterface).
public interface BaseInterface {
public void doBase();
}
public interface SpecificInterface extends BaseInterface {
public void doSpecific();
}
public class SpecificClass implements SpecificInterface {
public void doBase() { ... }
public void doSpecific() { ... }
}
public class LoadClass() {
private PropertiesLoader propertiesLoader = new PropertiesLoader();
public <C extends BaseInterface> C loadClass(String propertyName) {
Class<C> theClass;
// Load the class.
theClass = propertiesLoader.getPropertyAsClass(propertyName);
// Create an instance of the class.
C theInstance = theClass.newInstance();
// Call the common method.
theInstance.doBase();
return theInstance;
}
}
Unfortunately, when I run the code:
loadClassInstance.loadClass("SpecificClass");
I get the following exception:
Exception in thread "main" java.lang.ClassCastException:
SpecificClass cannot be cast to BaseInterface
at LoadClass.loadClass
Any ideas how I would solve this issue?
Many Thanks, Danny
Java's Service Provider Interface (SPI) libraries allow you to load classes with public parameterless constructors dynamically based on the interfaces they implement, and it's all done through the use of META-INF/services.
First, you'll need the interface:
package com.example;
public interface SomeService {
String getServiceId();
String getDisplayName();
}
Then when you need them, you can load them using Java's ServiceLoader class, which implements Iterable:
ServiceLoader<SomeService> loader = ServiceLoader.load(SomeService.class);
for (SomeService serv : loader) {
System.out.println(serv.getDisplayName());
}
Then when you have 1 or more implementing classes on your classpath, they register themselves in META-INF/services. So if you have the implementation:
package com.acme;
public class SomeImplementation implements SomeService {
// ...
public SomeImplementation() { ... }
// ...
}
Note that this class needs a default no-args constructor, this is not optional.
You register it with the class loader by creating a file in META-INF/services in your classpath (such as in the root of your jar) with the following properties:
The name of the file is the fully qualified class name of the interface, in this case, it's com.example.SomeService
The file contains a newline-separated list of implementations, so for the example implementation, it would contain one line: com.acme.SomeImplementation.
And there you go, that's it. How you build your project will determine where you put the META-INF/services stuff. Maven, Ant, etc. all have ways of handling this. I recommend asking another question about your specific build process if you have any trouble adding these files to your build.
If you replace your code with below it works. I doubt that PropertiesLoader is doing something that is not supposed to be done.
Class<?> theClass;
// Load the class.
theClass = Class.forName("SpecificClass");
// Create an instance of the class.
C theInstance = (C) theClass.newInstance();
BaseInterface base = loadClass();//There is no problem in casting
Java program normally is loaded by system classloader. The classes which are referred to in a .properties file are loaded by a user-defined classloader. Classes loaded by different classloaders are considered different even if have same name and are loaded from same classfile. In your case, the interface BaseInterface loaded by system classloader is different from the BaseInterface loaded by
PropertiesLoader.
To fix this, PropertiesLoader should delegate loading of BaseInterface to system classloader. Typical way to do so is to use system classloader as a parent classloader for PropertiesLoader.

Defining classes in Java files

I have found one error in my Java program:
The public type abc class must be defined in its own class
How can I resolve this error? I am using Eclipse. I am new to Java programming.
Each source file must contain only one public class. A class named ClassName should be in a file named ClassName.java, and only that class should be defined there.
Exceptions to this are anonymous and inner classes, but understanding you are a beginner to Java, that is an advanced topic. For now, keep one class per file.
Answering your addition: it is OK to inherit classes and that's totally fine. This does not matter, each class should still have its own file.
Public top-level classes (i.e. public classes which aren't nested within other classes) have to be defined in a file which matches the classname. So the code for class "Foo" must live in "Foo.java".
From the language specification, section 7.6:
When packages are stored in a file system (ยง7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared public (and therefore is potentially accessible from code in other packages).
This rule, which doesn't have to be followed by compilers, is pretty much universally adhered to.
Ok, maybe an example will help.
In file MySuperClass.java:
public class MySuperClass {
// whatever goes here
}
public class MySubClass1 extends MySuperClass {
// compile error: public class MySubClass1 should be in MySubClass1.java
}
class MySubClass2 extends MySuperClass {
// no problem (non-public class does not have to be in a file of the same name)
}
In file MySubClass3.java:
public class MySubClass3 extends MySuperClass {
// no problem (public class in file of the same name)
}
Does that make things clearer?
A public class with the name of "abc" must be in a file called abc.java
You can create a new class an a existing file if it's private, but you should not do this.
Create one file per class.
Eclipse does that for you, if you create a new class.
For programming Java, you have to understand the construct of classes, packages and files. Even if Eclipse helps you, you have to know it for yourself. So start reading Java books or tutorials!

Categories

Resources