Eclipse DI does not create a custom object annotated with Created - java

I'm developing an Eclipse RCP application based on 4.4 Luna version.
I have the following classes:
public class NewProjectDialog extends TitleAreaDialog {
#Inject
private ProjectManager projectManager;
// some code
}
and
#Creatable
#Singleton
public class ProjectManager {
// some Code
}
When I run the application and open NewProjectDialog the following exception is thrown:
org.eclipse.e4.core.di.InjectionException: org.eclipse.e4.core.di.
InjectionException: Unable to process "NewProjectDialog.projectManager":
no actual value was found for the argument "ProjectManager".
Apart #Creatable annotation must I do something more to make Eclipse DI instantiate this class when it cannot find it in the context?

I have also face the same issue and the answer already given by greg-449 is in the comment section of the question is correct. Yes, it is problem in injecting/creating the fields of such objects.

Related

IllegalAccessError when declaring a class for a library package

I'm currently working on a maintenance for an undocumented code and have stumbled upon a weird error when running a class in Spring Boot. The code raises an IllegalAccessError on execution, but compiles properly.
It seems that the way the original team tried to access the Kafka Streams' private methods was the "MacGyver way", but I'm unsure about its behaviour since IntelliJ imports the code properly and handles the inheritance as it should (if I declare the class as a local package it highlights saying that the AbstractStream and KTableImpl classes do not exist).
The project is managed with Gradle and it is a micro-service. The execution is made through a Spring Boot and the error raises on startup.
One possible solution was to use Java's Reflection library, but it does not seem the correct approach to solve this error. Maybe a boot setting is wrong?
package org.apache.kafka.streams.kstream.internals;
import org.apache.kafka.streams.kstream.KTable;
public class KTableSpy {
public static <K> String getName(AbstractStream<K> stream) {
return stream.name;
}
public static <K, V> void enableSendingOldValues(KTable<K, V> table) {
((KTableImpl<K, ?, V>) table).enableSendingOldValues();
}
}
This class should've worked properly and do not interfere on the service startup. Instead, we have the following error
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AVCompanyStateProjector': Invocation of init method failed; nested exception is java.lang.IllegalAccessError: tried to access method org.apache.kafka.streams.kstream.internals.KTableImpl.enableSendingOldValues()V from class org.apache.kafka.streams.kstream.internals.KTableSpy
Not sure what you try to accomplish, but AbstractStream and KTableImpl are from package org.apache.kafka.streams.kstream.internals and not part of public API (as the name indicates). This implies that methods (or even the whole class) can be added/removed without notice.
Maybe the visibility from enableSendingOldValues() was changes from public to package-private breaking your code.

Spring state machine annotation #WithStateMachine not working when created in wrong order

I'm using the spring state machine and want to add on functionality to transitions and state changes using #WithStateMachine, #OnTransition and #OnStateChanged to keep the class with #EnableStateMachine as simple as possible. I never got it to work though and after some trial and error I realised that the #WithStateMachine bean has to be created before the state machine but that dependency isn't automatically solved.
The interesting parts of my project basically boils down to the following:
package org.myorg.a
#Component
public class MyComponent {
#Autowired
StateMachine<States, Events> sm;
}
package org.myorg.b
#WithStateMachine
public class Listener {
#OnTransition
public void anyTransition() {}
}
MyComponent and therefore the state machine will be created before the Listener because Spring will look in package a before package b and in effect none of the methods in class Listener will be associated with the state machine and the anyTransition method will never be called.
The only two solutions to this that I can think of is
Rename the packages
Annotate the #EnableStateMachine class with #DependsOn and explicitly depend on all #WithStateMachine classes
In my opinion neither of those two solutions are good. Does anyone have a better way to solve this?
Hopefully this just got fixed in https://github.com/spring-projects/spring-statemachine/issues/232.

Scala+Android+Roboguice: ClassNotFoundException in loader

I'm evaluating Scala on Android by starting with the NotesList demo. I was able to replace the NotesLiveFolder.java file with its Scala equivalent without problem.
Next, I introduced Roboguice, creating a simple NotesListApplication.java that sets up the Guice modules, and successfully injected a resource into the NoteEditor.java activity.
Finally, I when I tried to replace NotesListApplication.java with its Scala equivalent, I get the following runtime error before the application finishes booting:
java.lang.ClassNotFoundException: com.example.android.notepad.NotesListApplication in loader dalvik.system.PathClassLoader[/data/app/com.example.android.notepad-1.apk]
I created a Google Code project containing the complete Eclipse project and source. The original functioning NotesListApplication.java is:
package com.example.android.notepad;
import java.util.List;
import roboguice.application.RoboApplication;
import com.google.inject.Module;
public class NotesListApplication extends RoboApplication {
private Module module = new BindEverything();
public void setModule(Module module) {
this.module = module;
}
#Override
protected void addApplicationModules(List<Module> modules) {
modules.add(module);
}
}
and the Scala equivalent that causes the error is:
package com.example.android.notepad
import roboguice.application.RoboApplication
class NotesListApplication extends RoboApplication {
val module : Module = new BindEverything()
override protected def addApplicationModules(modules:java.util.List[Module] ) {
modules.add(module)
}
}
I'm building in Eclipse with the ScalaIDE plugin. I'm not running any treeshaker/proguard/etc.
The disassembly shows the Scala classes as expected:
Class descriptor : 'Lcom/example/android/notepad/NotesLiveFolder;'
...
Class descriptor : 'Lcom/example/android/notepad/NotesListApplication;'
Any ideas what could cause this?
Upgrade to 2.0-SNAPSHOT of RoboGuice and then you dont have to use RoboApplication and it all binds automatically. For more how to bind check out the slides from Mike Burtons presentations about RoboGuice at AnDevCon 2 and check out the 2.0 section on the wiki.
Like I posted on the mailing list maybe check out the apk with dedexer and see if the class was actually removed e.g. by Proguard or renamed so it cant be found as a next step.

java.lang.IllegalAccessError: tried to access method

I am getting an exception and I can't find the reason of it.
The exception I get is :
java.lang.IllegalAccessError: tried to access method Connected.getData(Ljava/lang/String;)Ljava/sql/ResultSet; from class B
The method is public.
public class B
{
public void myMethod()
{
Connected conn = new Connected(); // create a connected class in order to connect to The DB
ResultSet rs = null; // create a result set to get the query result
rs = conn.getData(sql); // do sql query
}
}
public class Connected
{
public ResultSet getData(String sql)
{
ResultSet rs = null;
try
{
prepareConnection();
stmt = conn.createStatement();
stmt.execute(sql);
rs = stmt.getResultSet();
}
catch (SQLException E)
{
System.out.println("Content.getData Error");
E.printStackTrace();
}
return rs;
}
i am using apache tomcat 5.5.12
and JAVA 1.6
This happens when accessing a package scoped method of a class that is in the same package but is in a different jar and classloader.
This was my source, but the link is now broken. Following is full text from google cache:
Packages (as in package access) are scoped per ClassLoader.
You state that the parent ClassLoader loads the interface and the child
ClassLoader loads the implementation. This won't work because of the
ClassLoader-specific nature of package scoping. The interface isn't visible to
the implementation class because, even though it's the same package name,
they're in different ClassLoaders.
I only skimmed the posts in this thread, but I think you've already discovered
that this will work if you declare the interface to be public. It would also
work to have both interface and implementation loaded by the same ClassLoader.
Really, if you expect arbitrary folks to implement the interface (which you
apparently do if the implementation is being loaded by a different
ClassLoader), then you should make the interface public.
The ClassLoader-scoping of package scope (which applies to accessing package
methods, variables, etc.) is similar to the general ClassLoader-scoping of
class names. For example, I can define two classes, both named com.foo.Bar,
with entirely different implementation code if I define them in separate
ClassLoaders.
Joel
You are almost certainly using a different version of the class at runtime to the one you expect. In particular, the runtime class would be different to the one you've compiled against (else this would have caused a compile-time error) - has that method ever been private? Do you have old versions of the classes/jars on your system anywhere?
As the javadocs for IllegalAccessError state,
Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
I'd definitely look at your classpath and check whether it holds any surprises.
This happened to me when I had a class in one jar trying to access a private method in a class from another jar. I simply changed the private method to public, recompiled and deployed, and it worked ok afterwards.
I was getting this error on a Spring Boot application where a #RestController ApplicationInfoResource had a nested class ApplicationInfo.
It seems the Spring Boot Dev Tools was using a different class loader.
The exception I was getting
2017-05-01 17:47:39.588 WARN 1516 --- [nio-8080-exec-9]
.m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused
by Handler execution:
org.springframework.web.util.NestedServletException: Handler dispatch
failed; nested exception is java.lang.IllegalAccessError: tried to
access class com.gt.web.rest.ApplicationInfo from class
com.gt.web.rest.ApplicationInfoResource$$EnhancerBySpringCGLIB$$59ce500c
Solution
I moved the nested class ApplicationInfo to a separate .java file and got rid of the problem.
If getData is protected then try making it public. The problem could exist in JAVA 1.6 and be absent in 1.5x
I got this for your problem. Illegal access error
I was getting same error because of configuration issue in intellij.
As shown in screenshot.
Main and test module was pointing to two different JDK. (Press F12 on the intellij project to open module settings)
Also all my dto's were using #lombok.Builder which I changed it to #Data.
From Android perspective:
Method not available in api version
I was getting this Issue primarily because i was using some thing that is not available/deprecated in that Android version
Wrong way:
Notification.Builder nBuilder = new Notification.Builder(mContext);
nBuilder.addAction(new Notification.Action(android.R.drawable.ic_menu_view,"PAUSE",pendingIntent));
Right way:
Notification.Builder nBuilder = new Notification.Builder(mContext);
nBuilder.addAction(android.R.drawable.ic_media_pause,"PAUSE",pendingIntent);
here Notification.Action is not available prior to API 20 and my min version was API 16
Just an addition to the solved answer:
This COULD be a problem with Android Studio's Instant Run feature, for example, if you realized you forgot to add the line of code: finish() to your activity after opening another one, and you already re-opened the activity you shouldn't have reopened (which the finish() solved), then you add finish() and Instant Run occurs, then the app will crash since the logic has been broken.
TL:DR;
This is not necessarily a code problem, just an Instant Run problem
In my case the problem was that a method was defined in some Interface A as default, while its sub-class overrode it as private. Then when the method was called, the java Runtime realized it was calling a private method.
I am still puzzled as to why the compiler didn't complain about the private override..
public interface A {
default void doStuff() {
// doing stuff
}
}
public class B {
private void doStuff() {
// do other stuff instead
}
}
public static final main(String... args) {
A someB = new B();
someB.doStuff();
}
In my case I was getting this error running my app in wildfly with the .ear deployed from eclipse. Because it was deployed from eclipse, the deployment folder did not contain an .ear file, but a folder representing it, and inside of it all the jars that would have been contained in the .ear file; like if the ear was unzipped.
So I had in on jar:
class MySuperClass {
protected void mySuperMethod {}
}
And in another jar:
class MyExtendingClass extends MySuperClass {
class MyChildrenClass {
public void doSomething{
mySuperMethod();
}
}
}
The solution for this was adding a new method to MyExtendingClass:
class MyExtendingClass extends MySuperClass {
class MyChildrenClass {
public void doSomething{
mySuperMethod();
}
}
#Override
protected void mySuperMethod() {
super.mySuperMethod();
}
}
I was getting similar exception but at class level
e.g. Caused by: java.lang.IllegalAccessError: tried to access class ....
I fixed this by making my class public.

NoClassDefFoundError

I have an issue where NoClasDefFoundError is being thrown. It puzzles me since I am using interfaces, and no class definition should be available. I have read through some posts which point to Classpath, but I don't believe that to be the issue here (although I may be wrong). I am using NetBeans 6.9.1 IDE.
I have created a sample setup to reproduce the issue. Four projects: Interfaces, Objects, Locator and Consumer. Below you will find the implementations.
At runtime consumer coplains about missing SomeObject implementation, which it should not be aware of since it is accepting interface.
Exception in thread "main"
java.lang.NoClassDefFoundError:
objects/SomeObject
What am I missing?
package interfaces;
public interface ISomeInterface { }
package objects;
import interfaces.ISomeInterface;
public class SomeObject implements ISomeInterface{ }
package locator;
import interfaces.ISomeInterface;
import objects.SomeObject;
public class Locator { public static ISomeInterface LocateImplementation() { return new SomeObject(); }}
package consumer;
import interfaces.ISomeInterface;
import locator.Locator;
public class Main { public static void main(String[] args) { ISomeInterface object = Locator.LocateImplementation(); }}
You can get a NoClassDefFoundError exception with interfaces just as you can with classes. Consider the "Class" in the name of the exception to be the .class file that is generated from compiling a class or interface, not a Java class.
This is saying that the class/interface objects.SomeObject isn't visible on your classpath. Check the location of that .class file and ensure that it's on your classpath - if you're positive it's there, give us some screen shots or something that might help to debug the problem.
Think of NoClassDefFoundError as a runtime linkage problem. JRE loaded one class (or an interface) and it references another class (or an interface), but that referenced class isn't found.
The only way this can happen if you have packaging/classpath issues such that your runtime environment doesn't reflect how things are at build time.
If you are launching this from IDE, make sure that you aren't ignoring any errors and launching anyway. Some classes will not be generated that way.
Usually I run into these problems not when a class is missing, but when there is an error in the static initializers.
Try running your code in a debugger, and set the exception breakpoint to break when any exception is thrown, whether caught or not. I bet you have an uncaught exception in the static initializer for some reason.
In the locateImplementation() method you are returning "new SomeObject()",
JVM needs to have its definition when called. I think it is missing.
You should check if your SomeObject class is in class path because -
Well the JVM will be running the below code -
ISomeInterface object = Locator.LocateImplementation();
and when it does that it will call Locator.LocateImplementation(). This code internally tries to instantiate your SomeObject class which it does not find in the classpath.
So your below understanding
It puzzles me since I am using
interfaces, and no class definition
should be available.
Is not really valid.
Any Interface must be declared inside class
public class Calbacks {
public interface IBaseFragmentInterface {
void NotifyMainActivity();
}
}

Categories

Resources