Why the setServletContext method of ServletContextAware interface is throwing an error? - java

I have implemented the method as below but there is an error saying - remove override annotation, which should be there. Under which circumstances this error would occur? Due to this my beans are not being created and I am not able to run the application.
#Override
public void setServletContext(ServletContext servletContext) {
}
The error which shows up on the console is - "The method setServletContext(ServletContext) of type MenuHelper must override a superclass method". But the method is there and goes undetected. If I remove the method, there should be an error saying add the unimplemented methods - which is also not showing up. Kindly help me with this situation. Thanks

I can't be sure without seeing the rest of your code, but from that error I'm guessing that your class does not implement ServletContextAware properly.

Related

What is the difference between java.lang.NoSuchMethodError main Exception in thread "main " and Error: Main method not found in class

I have this class
public class demo3 {
private static void sum()
{
}
}
when I tried to run this class, I expected the error to be java.lang.NoSuchMethodError main Exception in thread "main "
however, the output was a bit different and I got below message
Error: Main method not found in class demo3, please define the main method as:
public static void main(String[] args)
now this got my curiosity as to in which case I will get java.lang.NoSuchMethodError or in which case I will get the other error message.
You get the Main method not found message when public static void main(String[]) can't be found in the class that you've asked the JVM to start running. That is, the entry point of the overall program cannot be found.
You get the java.lang.NoSuchMethodError message if your (already running) code attempts to invoke a method on a class which was available at compile time, but not available in the version of the class you are using at runtime (for example, you compile against one version of the library, and then update the library jar without recompiling). This can occur at any point in the program.
There doesn't look to be anything in JLS which says that NoSuchMethodError can't be thrown, rather than the Main method not found; however, failing to write a main method (either entirely, or writing one with the wrong signature) is a far more common mistake than the "class changed after compilation" case, especially for beginners, for whom the NoSuchMethodError might be too cryptic. There is no harm in providing a more user-friendly message in this one case.

Getting the actual class used for a MethodInvocation rather than the declaring class

I'm digging through a web application in an effort to fix some problems. The application uses Tomcat, Jersey and Guice. One of the issues is occurring in a MethodInterceptor used for authorization purposes. Here's the method, trimmed to the relevant part:
public Object invoke(MethodInvocation invoc) throws Throwable {
// ...
//Check that the annotation actually exists
if(! invoc.getMethod().getDeclaringClass().isAnnotationPresent(Tool.class))
{
throw new BaseException("...");
}
// ...
}
Now the problem is that some of the "web-facing" methods are inherited from a parent class without being overridden in the child. If I understand getDeclaringClass() correctly, it will return the parent class in this case, but what we really want here is the child class. Some testing seems to confirm this--if I override the method in the child class everything is fine, but if I don't put in the override the exception is thrown.
So, given a MethodInvocation object, is there a way to trace it back to the "actual" class instantiated, rather than the class where the method was declared? Or is some other approach necessary? Worst-case, I could just annotate each method as necessary rather than annotating the class.
Sorry if this is a long-winded question for an easy answer - my Java is pretty rusty.
Simple enough, needed to use getThis().getClass() on the MethodInvocation instead of getMethod().getDeclaringClass():
if(! invoc.getThis().getClass().isAnnotationPresent(Tool.class))
{
throw new BaseException("...");
}
Although in my case, Guice complicated things a bit by putting in an auto-generated child class (e.g., a class name ending in "$$EnhancerByGuice..." That was fixed by moving one up the tree with getSuperclass():
if(! invoc.getThis().getClass().getSuperclass().isAnnotationPresent(Tool.class))
{
throw new BaseException("...");
}
It looks like that the answer is No. I created simple test to check it:
class Run implements Runnable {
#Override
public void run() {
}
}
class Run2 extends Run{}
Method method = Run2.class.getMethods()[0];
System.out.println(method);
As we can see in debug window method doesn't have any information of class Run2:
I guess it would be better to stick on actual methods with its annotations rather then on actual class instances where these methods get invoked.

com.liferay.portlet.ActionResponseImpl cannot be cast to com.liferay.portlet.ActionResponseImpl

I tried to cast Action response like this:
private void mappingMethod(ActionResponse response) {
ActionResponseImpl actionResponseImpl = (ActionResponseImpl)response;
...}
In debugging on "Expression Evaluation" window I can see the type of "responce" is ActionResponseImpl, and cast does not cause exception. But on runtime I have this exeption:
java.lang.ClassCastException: com.liferay.portlet.ActionResponseImpl cannot be cast to com.liferay.portlet.ActionResponseImpl
Please tell me what is the problem.
P.S.: In PortalImpl class, the copyRequestParameters method has same string, but here it's working....
It would do you good if you refrained from using classes from portal-impl.jar in your custom plugin portlet.
ActionResponseImpl is a class in the portal-impl.jar. So if you can give a use-case as to why you are planning to use this class, then we can suggest alternative.
You have ActionResponseImpl twice on the classpath - what jars from Liferay did you include in your project? As Prakash mentions, it's portal-impl.jar, which you can not have in a plugin.
What the ClassCastException wants to tell you is that your class extends one implementation of ActionResponseImpl, but the runtime environments expects it to be another implementation - they might be identical, but as they are loaded from two different sources, the classloader cannot refer one to another superclass than it extends.
I saw this issue, when I call a method using Ajax from XHTML page. I modified my method to return simple string and removed the code that tries to modify the actionResponseImpl and that resolved the issue.
This type of errors are produced by different classloaders. A class is identified by their fully qualified class name AND its Classloader. So if you embedd portal-impl in your portlet; the action is going to receive an ActionResponseImpl instance from the portal classloader and is trying to cast this one to a class of your portlet class loader .
see more: http://www.javaspecialists.eu/archive/Issue018.html

Overriding instantiateItem(...) won't compile

I am trying to use a PageAdapter. I found out that public Object instantiateItem( View pager, int position ) has been deprecated. So I am trying up update but ran into a problem. The new definition changes the deceration to public Object instantiateItem( ViewPager pager, int position ), when I do this and push it to my device the app crashed.
Here is my logcat output.
12-26 19:24:30.701: ERROR/AndroidRuntime(25431): FATAL EXCEPTION: main
java.lang.UnsupportedOperationException: Required method instantiateItem was not overridden
at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:175)
at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
So I added #Override to the method call, but when I compile it ,using maven, I get the following output that corresponds to my method.
Chronos/ChronosApp/src/com/kopysoft/chronos/view/ClockViewer.java:[67,4] error: method does not override or implement a method from a supertype
I am at a lose as what to do. Any advice would be greatly appreciated!
The entire code can be found here: http://pastebin.com/da5Kqcmg
The runtime code in PagerAdapter.instantiateItem() is throwing the exception because it wants you to override that. So make sure you are overriding the method that is throwing the exception. You probably just want to switch your code back to overriding the deprecated method since that's what your runtime library is expecting.
Might it be possible that you have an older runtime you are running with?

Couldn't find a matching Java operation for WSDD operation

I hv created a web service for the following code but am getting an exception:
org.apache.axis.InternalException: java.lang.Exception: Couldn't find a matching Java operation for WSDD operation "andrQues" (0 args)" on invoking the function.
public class Ques {
public String[] AndrQues(){
String ques[] = {"name??", "age??", "grade??"};
return ques;
}
}
Does anyone know why its occuring? Also the wsdl is not getting generated.
I found the error.
Just need to change the "AndrQues" to "andrQues" and program runs fine.
There is something wrong with your method name make sure that you have spelled your method Name correct. take care about the thing that use your method first latter in small means lower case.
just u need to change "AndrQues" to "andrQues" because by Default the web-services taking name into the lower case.
this will help..
In my case, entry into the interface WSPort.java invoking WSSoapHttpBindingImpl.java was missing.
public interface <classname> extends java.rmi.Remote
{
public <methodname>(<params>) throws java.rmi.RemoteException;
}

Categories