Facing java.lang.IllegalAccessError for a base abstract class when trying to inject its implementation in wildfly 10 - ejb-3.1

We are receiving the following exceptions in one of our code :
java.lang.IllegalAccessError: tried to access class base.BaseMessage from class message.beans.TerminalPowerCommandProducer$Proxy$_$$_WeldSubclass
Our class structure is as follows :
The Base Message creator class with default message properties
package messages.base
//... required imports
abstract class BaseMessage{ //some protected variables and methods }
the intermediate message class with extension to BaseMessage some additional properties for specific message types
package messages.base
//... required imports
public abstract class PowerMessage extends BaseMessage {//some more protected variables and Logger(using #Inject) and methods}
The actual implementation of the above abstract classes
package messages.beans
//... required imports
#Named
public class TerminalCommandMessage extends PowerMessage {// some more variables with injections and methods with abstract method implementation}
This class is now injected in some other classes :
package messages.beans
#Named
public class TerminalPowerCommandProducer {
#Inject
TerminalCommandMessage commandMessage
//some other code
}
We are receiving exception as reported above.
We are using WildFly version 10.1.0 Final with jdk 8
Is there an issue with the way we have consumed it?
Because if we mark the BaseMessage class as public it all works fine.

Related

Powermock of static class gives error: java.lang.NoClassDefFoundError: Could not initialize class XXX

Problem
Could not initialize class ...
...javax.xml.transform.FactoryFinder (in our case).
In the article, where we found the solution, it was the class SessionFactory.
Class Under Test
We wanted to write a test for a utils class with static members.
We got the error when trying to create a Mock of a class, which contained a new statement as an initialization of a static field.
public class ClassUnderTest{
private static JavaType javaType = new JavaType();
// ...
}
Test Class
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassUnderTest.class)
public class TestForClassUnderTest {
#Test
public void testCase() {
PowerMockito.mockStatic(ClassUnderTest.class);
Solution
The solution was adding another class level annotation to the test class:
#SuppressStaticInitializationFor("com.example.package.util.ClassUnderTest")
Note, that you have to give the package path and no .class at the end. Unlike #PrepareFor.
Thanks to this article: http://www.gitshah.com/2010/06/how-to-suppress-static-initializers.html
Test Class with Solution
//...
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassUnderTest.class)
#SuppressStaticInitializationFor("com.example.package.util.ClassUnderTest") // <-- this is it :)
public class TestForClassUnderTest {
#Test
public void testCase() {
PowerMockito.mockStatic(ClassUnderTest.class);
//...
}
}

Base Class vs Extended Classes

I'm a new learner, I'm practicing the base and child classes. My question is how do we decide which class should be instantiated, extended or the Baseclass?
Thanks in advance
package MavenProject2Package2;
import org.testng.annotations.Test;
import MavenProject2Package.JavaTesting;
public class JavaTesting2 extends JavaTesting
{
#Test
public void f1()
{
JavaTesting a1 = new JavaTesting();
System.out.println(a1.msg);
JavaTesting2 a2 = new JavaTesting2();
System.out.println(a2.msg);
}
}
Base class - it's a class which you should be extending from. - eg - superclass.
In superclass you may put some general fields and methods, which are used across your web app. For example, locators for header as well as footer items, because they are the same for all the pages (mostly).

Nancy/TinyIoC multiple concrete classes for single interface

We have two auth methods for different modules – UserAuthModule and ServiceAuthModule. We’ve created 2 base classes that modules derive from. We’ve interfaced the AuthProviders into IAuthProvider. Then we have a dependency in the constructors that should get the correct AuthProvider injected. However, we can’t find a way to tell Nancy/TinyIoC which concrete class to use. Here is the pseudo-code:
abstract class UserAuthModule : NancyModule
{
public UserAuthModule(IAuthProvider authProvider) // should get the UserAuthProvider concrete class
}
abstract class ServiceAuthModule : NancyModule
{
public ServiceAuthModule(IAuthProvider authProvider) // should get the ServiceAuthProvider concrete class
}
Here's an example of one of the concrete module's class declaration:
public class AccountModule : UserAuthModule
We then get stuck: how do we register 2 concrete classes for the IAuthProvider interface? We could name them, but can’t figure out how Nancy knows which class to inject when it does the constructor injection.
Inside our bootstrapper we have:
Container.Register<IAuthProvider, UserAuthProvider>(“UserAuth”);
Container.Register<IAuthProvider, ServiceAuthProvider>(“ServiceAuth”);
We could resolve the type from the container, but there's not container access within the Nancy module.
Is creating a unique interface for each based off of IAuthProvider out of the question?
interface IUserAuthProvider : IAuthProvider { }
interface IServiceAuthProvider : IAuthProvider { }
And then register:
Container.Register<IUserAuthProvider, UserAuthProvider>();
Container.Register<IServiceAuthProvider, ServiceAuthProvider>();
And then modify the constructors:
public UserAuthModule(IUserAuthProvider authProvider)
public ServiceAuthModule(IServiceAuthProvider authProvider)

How to initialize FileTree field in Gradle custom task?

I want to create an object which implements interface FileTree in Gradle.
From what I can find from documentation FileTreeAdapter class implements FileTree, but it is internal class.
How can I initialize my object filesToDelete? And how can I find which classes implements FileTree interface from the documentation?
public class DeleteDirTask extends DefaultTask {
#InputFiles #Optional
FileTree filesToDelete = files("/src/")
...
}
How can I initialize my object filesToDelete?
Use project.fileTree(). (project.files() is for FileCollections.)
And how can I find which classes implements FileTree interface from the documentation?
The implementation classes are internal, and you shouldn't have to worry about them.

CakePHP lazy loading fails with static access to class constants

In a CakePHP 2.2 app, I'm using class constants in a Model for some internal configuration. The following issue came up.
Short version:
Cake's lazy class loading will not be triggered by a static call to the Model class.
If the first access to a Model in a Controller is
MyModel::SOME_CONST // fails
the class will be unknown. If any instance of the class is used before, it's fine:
$this->MyModel->something();
MyModel::SOME_CONST // works
Not knowing about the details of the lazy loading implementation:
Question: Is this something that is impossible to fix? If so, why? How do I then best work around it in my App myself (wrap consts in a function)? Or is there a chance to improve the lazy loading so that it works with static access, too?
Long version with code:
In order to test the different cases, I made a small test App with 1 Model and 1 Controller:
Model/Post.php:
<?php
class Post extends AppModel {
public $useTable = false; // Don't bother with a DB
const FOO = "foo";
public $bar = "bar";
}
Controller/PostsController.php:
<?php
class PostsController extends AppController {
public function constant() {
debug(Post::FOO);
}
public function variable() {
debug($this->Post->bar);
}
public function variableFirst() {
debug($this->Post->bar);
debug(Post::FOO);
}
}
Accessing the three controller actions through the browser, the different cases can now be tested.
1) accessing the Model constant (at /posts/constant):
Error: Class 'AppModel' not found
2) accessing the Model variable (at /posts/variable):
'bar'
3) accessing the Model constant AFTER a variable (at /posts/variable):
'bar'
'foo'
lazyloading works with normal class calls as well as static calls IF you correctly approach it.
Correctly means, that you always have to App::uses() all used classes at the top of your file
for AppModel in a model file:
App::uses('AppModel', 'Model');
class Post extends AppModel {}
see the core files for details.

Resources