how to use Java ASM from java code to detect method? - java

i am new to using ASM library.
1-i have to find number of class in code
2-which class called which method,(caller class)
3-each method called by which class
source code
public class Start {
public static void main(String[] args) {
Cls1 c1=new Cls1();
Cls2 c2=new Cls2();
c1.m2c1();
}
}
public class Cls1 {
int var1;
int var2;
String name;
boolean bool;
Cls1()
{
var1 = this.var1;
var1 = this.var1;
name = this.name;
bool = this.bool;
}
Cls2 cl2=new Cls2();
int z=cl2.m1c2(5,6);
public int m1c1(int x,int y)
{
return x+y;
}
public void m2c1()
{
System.out.println("From m2c1 z= "+z);
}
}
public class Cls2 {
public int m1c2(int x,int y)
{
return x+y;
}
}
i have no idea how i do this project ..

Related

How to change the value of private variable in java

I started to programm in Java since Yesterday, and I have the biggest question of my entire programmer life(since Yesterday).
For example, let's say I have a code like this:
public class itsAClass {
static private String A;
public static void main() {
A = "This should be changed";
}
public String something() {
return A;
}
}
I wanted to use the method something() in another Class to get the String Sentence of A, but I got only null.
How can I change the value of A, so that the another Class can get the Value "This should be changed"?
If you just want to bring this code to work you just can make something() static as well.
But this will be not the right way to approach this problem.
If you want to hold code in the main class you could do something like this:
public class AClass {
private String a;
public static void main() {
AClass myC = new AClass();
myC.setA("This should be changed");
// than use myC for your further access
}
public String something() {
return a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
If you want to access it by a external class without direct reference you can checkout the singleton pattern.
public class AClass {
private final static AClass INSTANCE = new AClass();
private String a;
public static void main() {
getSingleton().setA("This should be changed");
}
public String something() {
return a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public static AClass getSingleton() {
return INSTANCE;
}
}
This way you can access it via AClass.getSingleton() from any location of your code.
You have to call your main() function.
In another class:
itsAClass aClassObj = new itsAClass();
aClassObj.main();
// or rather itsAClass.main() as it is a static function
// now A's value changed
System.out.println(aClassObj.something());
the way to set the value of private variable is by setter and getter methods in class.
example below
public class Test {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}
you can call method main() in method something().
public class itsAClass{
static private String A;
public static void main() {
A = "This should be changed";
}
public String something() {
main();
return A;
}
public static void main(String[] args){
itsAClass a1 = new itsAClass();
System.out.println(a1.something());// prints This should be changed
}
}

Extending abstract classes

MyMath's constructor is supposed to call Homework's constructor, but super(); returns an error 'cannot find symbol'. It should not have any arguments.
Also, I am confused about how to call the method createAssignment using an arraylist, but I have to use it. Any advice?
Homework
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
public Homework(int pages, String hw) {
// initialise instance variables
pagesToRead = 0;
typeHomework = "none";
}
public abstract void createAssignment(int p);
public int getPages() {
return pagesToRead;
}
public void setPagesToRead(int p) {
pagesToRead = p;
}
public String getTypeHomework() {
return typeHomework;
}
public void setTypeHomework(String hw) {
typeHomework = hw;
}
}
MyMath
public class MyMath extends Homework {
private int pagesRead;
private String typeHomework;
public MyMath() {
super();
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
public String toString() {
return typeHomework + " - " + pagesRead;
}
}
public class testHomework {
public static void main(String[] args) {
ArrayList<Homework> list = new ArrayList<Homework>();
list.add(new MyMath(1));
list.add(new MyJava(1));
for (Homework s : list) {
s.createAssignment();
}
}
}
Compiler error:
Regarding the compiler error, you have to change the MyMath constractor to somthing like:
public MyMath() {
super(someInt, someString);
}
Or, you can add a non-arg constructor to the Homework class:
public Homework() {
this(someInt,someString);
}
You can learn about the super() keyword in the Javadocs tutoriel:
If a constructor does not explicitly invoke a superclass constructor,
the Java compiler automatically inserts a call to the no-argument
constructor of the superclass. If the super class does not have a
no-argument constructor, you will get a compile-time error. Object
does have such a constructor, so if Object is the only superclass,
there is no problem.
Code Suggestion:
As there is many other issues in your question, i modified all your classes like below:
Homework.java:
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
{
// initialise instance variables
pagesToRead = 0;
typeHomework = "none";
}
public Homework(int pages, String hw) {
this.pagesToRead = pages;
this.typeHomework = hw;
}
public abstract void createAssignment(int p);
public int getPages() {
return pagesToRead;
}
public void setPagesToRead(int p) {
pagesToRead = p;
}
public String getTypeHomework() {
return typeHomework;
}
public void setTypeHomework(String hw) {
typeHomework = hw;
}
}
MyMath.java
public class MyMath extends Homework {
private int pagesRead;
private String typeHomework;
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
public String toString() {
return typeHomework + " - " + pagesRead;
}
}
TestHomework.java:
class TestHomework {
public static void main(String[] args) {
ArrayList<Homework> list = new ArrayList<Homework>();
// will create a homework with type Math and one page to read
list.add(new MyMath(1,"Math"));
// Assuming MyJava is similar to MyMath
list.add(new MyJava(1,"Java"));
for (Homework s : list) {
if (s instanceof MyMath) {
// modify the number of pages to read for the Math homework
s.createAssignment(3);
} else if (s instanceof MyJava) {
// modify the number of pages to read for the Java homework
s.createAssignment(5);
} else {
s.createAssignment(7);
}
}
}
}

How can access a object in main class from another class in java?

I have a main class and two sub class in java language.how can access xx in class Y?please help me ,I need it in my project.
import class X,Y;
public static void main(String[] args) {
xx=new X;
example=new Y;
}
public class Y{
change xx.value;//how can change it?
}
If I understand your question, then you can do it with an accessor and mutator (or getter and setter if you prefer) and something like,
static class X {
public X(int value) {
this.value = value;
}
int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String toString() {
return Integer.toString(value);
}
}
static class Y {
public void example(X x) {
System.out.println("in example");
x.setValue(x.getValue() + 5);
}
}
public static void main(String[] args) {
X xx = new X(10);
System.out.printf("xx = %s%n", xx);
Y yy = new Y();
yy.example(xx);
System.out.printf("xx = %s%n", xx);
}
Output is
xx = 10
in example
xx = 15
Create a function inside Y class with a type X parameter, then you can call this function from your Main class and change xx value.
import class X,Y;
public static void main(String[] args)
xx=new X;
example=new Y;
xx=example.changeXValue(xx);
}
public class Y{
public X changeXValue(X xx){
//change xx here
return xx;
}
}
make xx become parameter of method of class Y, because JAVA use reference by default (for object), so that if you change xx in method of class Y, it also apply to method main
import class X,Y;
public static void main(String[] args) {
xx=new X;
example=new Y;
example.modify(xx)
}
public class Y{
public void modify(X xx){ //change xx here)
}

Can't send string to another class from if loop

I'm trying to pass a string from one class to another, but not succeeding. I realized during research and trial and error that I need to have "public static void main(String[] args) {}" to be able to use the if statement, but then getY() produces an error. What can I do differently?
public class Testing {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
The other class is
class B {
public static void main(String args[]) {
String x = Klasatest2.getZ();
System.out.println(x);
}
}
Error:
Klasatest2.java:14: illegal start of expression
public static String getZ()
^
Klasatest2.java:14: illegal start of expression
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
Klasatest2.java:14: ';' expected
public static String getZ() {
^
4 errors
For starters, you can't declare a method inside of a method,
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
public static String getZ() {
return z;
}
}
}
So you have to make sure that get getZ() method is declared OUTSIDE of main(string[] args)
Like this,
public class Test {
public static String z;
public static void main(String[] args) {
int x = 15;
if (x >= 10)
{
z = "Blabla";
}
}
public static String getZ() {
return z;
}
}
Also, you shouldn't have two main(String[] args) methods, as only one of them will be called unless for some reason you decide to call it yourself, which would be very strange.
So if you wanted the string to be set in class Test, you would need to call it's main method from your other class, possible like this.
Test.main(null);
Your application can only have one main(String args[]) method.
Try this:
public class Testing {
public static void main(String[] args) {
A a = new A("hy");
B b = new B(a.z);
}
public class A {
public String z;
public A (String z) {
this.z = z;
}
}
public class B {
public B (String y) {
System.out.println(y);
}
}
}

Java : Using parent class method to access child class variable

I have the following scenario :
public class A {
private int x = 5;
public void print()
{
System.out.println(x);
}
}
public class B extends A {
private int x = 10;
/*public void print()
{
System.out.println(x);
}*/
public static void main(String[] args) {
B b = new B();
b.print();
}
}
On executing the code, the output is : 5.
How to access the child class(B's) variable(x) via the parent class method?
Could this be done without overriding the print() method (i.e. uncommenting it in B)?
[This is important because on overriding we will have to rewrite the whole code for the print() method again]
EDITED
More Clarification :-
The motive of the question is to use the value of a child class private variable from its parent class method. This doesn't require changing the value of the parent class private variable in order to achieve the desired result.
The answers posted here, though, led me to my desired answer, which I have posted below.
(Thanks all for your time and help )
class A {
private int x = 5;
protected int getX() {
return x;
}
protected void setX(int x) {
this.x = x;
}
public void print() {
// getX() is used such that
// subclass overriding getX() can be reflected in print();
System.out.println(getX());
}
}
class B extends A {
public B() {
// setX(10); // perhaps set the X to 10 in constructor or in main
}
public static void main(String[] args) {
B b = new B();
b.setX(10);
b.print();
}
}
EDITED
Below is a general answer using abstract class and method to solve similar scenario:
abstract class SuperA {
protected abstract Object getObj();
public void print() {
System.out.println(getObj());
}
}
class A extends SuperA {
#Override
protected Object getObj() {
// Your implementation
return null; // return what you want
}
}
class B extends A {
#Override
protected Object getObj() {
// Your implementation
return null; // return what you want
}
public static void main(String[] args) {
B b = new B();
b.print();
}
}
After reading all the answers posted here, I got what I was looking for. The following is what I feel is the best answer for my question :
public class A {
private int x = 5;
protected int getX(){
return x;
}
public void print(){
System.out.println(getX());
}
}
public class B extends A {
private int x = 10;
protected int getX(){
return x;
}
public static void main(String[] args) {
B b = new B();
b.print();
}
}
Setting up a protected getter and overriding it is better than overriding the print() method itself, as there could be any other huge method in place of the print method which might need to access the value of the child class variable(s).
To solve your question you have to define the fields in the parent class A like protected (so it will be inherited on the child class) and set the field value x inside the constructor in the child class B. The print method is also inherited from A class so you can invoke it directly from parent class.
I hope this can help you.
public class A
{
// fields declaration
protected int x = 5;
public void print()
{
System.out.println(x);
}
}
public class B extends A
{
public B()
{
// set child x value. The field have been defined in the parent class
x = 10;
}
public static void main(String[] args)
{
A a = new A();
a.print(); // print 5
B b = new B();
b.print(); // print 10
}
}
You can always add it to the constructor:
public class B extends A {
//this line is unnecessary: private int x = 10;
/*public void print()
{
System.out.println(x);
}*/
public B()
{
x=10;
}
public static void main(String[] args) {
B b = new B();
b.print();
}
}
The reason it won't work as you try it is that default values only get evaluated once. So when it's default 5 in A, it stays 5 even though you used default 10 in B.
You should expose a getter for the value you want and override that in the child class.
Like so:
public class A {
private int x = 5;
public void print()
{
System.out.println(getX());
}
protected void setX(int x)
{
this.x = x;
}
protected int getX()
{
return x;
}
}
public class B extends A {
/*public void print()
{
System.out.println(x);
}*/
public B()
{
setX(10);
}
public static void main(String[] args) {
B b = new B();
b.print();
}
}

Categories

Resources