Accessing caller class attribute - java

I would like to know if it is possible from a child class to get caller attributes. Follow a sample.
TestClass1.class
package test;
public class TestClass1 {
private String myTestClass1Var = "xxxxx";
public static void main(String[] args) {
new TestClass1().class2();
}
private void class2() {
new TestClass2().getClass1Attributes();
}
}
TestClass2.class
package test;
public class TestClass2 {
public void getClass1Attributes() {
//is something like this possible?
getCaller().myTestClass1Var
}
}
Thanks

Related

Powermock and Mockito. Avoid static initialization for a class while mocking and stubing the same class

Suppose I have a class named Util with static fields:
public class Util {
public static field = Param.getValue("param1");
}
and the class Param look like this:
public class Param {
public static field = SomeClass.getValue("someValue");
}
I want to mock and stubb Param.getValue("param1") inside Util, but at the same time I want suppress static initialization for Param class. How can I achieve this?
This is my first attempt but it's not working
#RunWith(PowerMockRunner.class)
#PrepareForTest({Param.class})
#SuppressStaticInitializationFor("py.com.company.Param")
public class Test {
#Test
public void testSomeMethod() {
PowerMockito.mockStatic(Param.class);
when(Param.getValue("value1")).thenReturn("someValue1");
}
}
This is working for me. I get no output, and SomeClass#getValue if no #SuppressStaticInitializationFor:
#RunWith(PowerMockRunner.class)
#SuppressStaticInitializationFor({"so35047166.Param"})
#PrepareForTest({Param.class})
public class UtilTest {
#Before
public void setUp() throws Exception {
PowerMockito.mockStatic(Param.class);
}
#Test
public void testFoo() throws Exception {
final Util util = new Util();
assertEquals("Util#foo", util.foo());
assertEquals(null, Util.field);
}
}
with:
// all in package so35047166;
public class Util {
public static String field = Param.getValue("param1");
public String foo() {
return "Util#foo";
}
}
public class Param {
public static String field = SomeClass.getValue("someValue");
public static String getValue(final String in) {
System.out.println("Param#getValue");
return "Param#getValue";
}
}
public class SomeClass {
public static String getValue(final String in) {
System.out.println("SomeClass#getValue");
return "SomeClass#getValue";
}
}

How do you pass a reference to type 'class'?

I have a method that registers classes for serialization. I want to call this from a control class, something like:
public static void main(String[] args) {
Registrar.RegisterClass(Control.SomeRequest);
Registrar.RegisterClass(Control.SomeResponse);
Sender testServer = new Sender();
Receiver testClient = new Receiver();
testServer.StartServer();
testClient.StartClient();
}
public static class SomeRequest {
public String text;
}
public static class SomeResponse {
public String text;
}
---------------------------------------------
public class Registrar {
static Kryo kryo = client.getKryo();
public static void RegisterClass(??? cls){
kryo.register(cls.class);
}
}
Alternatively, I could pass in Control.SomeRequest.class just as easy, though I'm not sure how to achieve either of these.
The class name of "classes" is Class
public static void RegisterClass(Class cls){
kryo.register(cls);
}

Utilizing Public Final Classes Java

I would just like a clear example of how to instantiate a public final class in Java. I have to use a method from a class like this for a project, and have no idea how to instantiate it in the first place. Very difficult to find a clear example and explanation of the proper syntax. Thanks for the help.
public class Test {
public static void main(String[] args) {
Project pro = new Project();
pro.getName();
}
}
final class Project{
public String getName(){return "";}
}
===============================
A final class can be created like a normal class, Only thing is it can not be extended
This is an example
public class del {
public static void main(String args[])
{
x x1=new x();
System.out.println(x1.u());
}
}
final class x
{
public String u()
{
return "hi";
}
}
As you can see,x is a final class and have a method u which returns a string.
I am instatiating x in class del and calling its method u.
The output is hi
For more info click on final
final class Test{
public void callMe(){
System.out.println("In callMe method.");
}
}
public class TestingFinalClass{
public static void main(String[] args){
Test t1 = new Test();
t1.callMe();
}
}
Output : In callMe method.
final in java is applied to variable,method,class
final variable : the variable can not be signed with another value.
final method : the method cannot not be overridden.
final class : the class cannot extended.
The best example is String class in java. public final class String you can access the methods of String class as normal.
Some links
final keyword
String class
public class Test {
public static void main(String[] args) {
StdRandom stdRandom = StdRandom.getInstance(); /* this will retun an instance of the class, if needed you can use it */
int result =StdRandom.uniform(1);
System.out.println(result);
}
}
final class StdRandom{
private static StdRandom stdRandom = new StdRandom();
private StdRandom(){
}
public static StdRandom getInstance(){
return stdRandom;
}
public static int uniform(int N){
// Implement your logic here
return N;
}
}

How to write an abstract main class

I need to write some importers. They need all the same initialization. So I try to write an abstract class, which does all the initialization and also has the main method, so that all sub-classes just need to implement run() to do their specific import work:
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter();
importer.run();
}
}
public class ConcreteClass() {
public void run() {
// Do some importing
}
}
Of course it fails to create an instance of this abstract class (new AbstractImporter()).
Does anybody has any idea how to solve that? TIA!
Obviously you need a concrete class - anonymous or otherwise.
Better to move the main method to another class and instantiate the appropriate concrete class based on data (either your domain specific or a constant) and then run it. This way each implementation can be independent of other implementations.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteImporter1 extends AbstractImporter {
public void run() {
//do something
}
}
public class ImporterMain() {
public static void main(String[] args) {
AbstractImporter importer = createImporter(args[1]);
importer.run();
}
private static AbstractImporter createImporter(String type) {
if (type.equals("1")) {
return new ConcreteImporter1();
}
....
}
}
new AbstracterImporter() {
public void run() {
// ...
}
};
I apologize for current lack of formatting, currently on a mobile device.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
public static void main(String[] args) {
AbstractImporter importer = new AbstractImporter(){
public void run() {
System.out.println("Anonymous implementation");
}
};
importer.run();
}
}
You cannot create an instance of an abstract class.
public abstract class AbstractImporter {
public AbstractImporter() {
// Initialization
}
public abstract void run();
}
public class ConcreteClass extends AbstractImporter{
public void run(){
//Implementation
}
public static void main(String args[]){
AbstractImporter ai = new ConcreteClass();
ai.run();
}
}

Class inside class

public class FirstClass
{
private FirstClass()
{
new Thread(new Thread()).start();
}
public static void checkSomething(FirstClass clas)
{
//doing something
}
private class Thread implements Runnable
{
#Override
public void run() {
checkSomething(????);
}
}
public static void main(String[] args)
{
new FirstClass();
}
}
my question is, what to write in ???? to get class FirstClass, i cannot write "this", coz i would get Thread.
You can write FirstClass.this to access the enclosing class instance.

Categories