compare objects

P

Petterson Mikael

Hi,

I am using a class, from an api, called JavaClass. I contains data about
the class. I will extract the following data:

String className
Field [] field
Method [] method

I will store the data in the following objects:

DataClass
DataField
DataMethod

This is how I think of writing the code for the DataClass ( see below
Listing 1).

Then each DataClass can contain zero or more DataField's and DataMethod's

My task is to compare the data of DataClass objects for two different api:s

clazz1 = (DataClass)vector1.getDataClass();
clazz2 = (DataClass)vector2.getDataClass();
if(clazz1.equals(clazz2)){
//Data is the same

}else{

//Data is not the same for class ......
}


How can I make sure that the DataClass's ( and depending objects
DataField and DataMethod ) data is compared?

Cheers,

//mikael


Listing 1
=========
import java.util.ArrayList;


public class DataClass {


private String className = null;
private ArrayList fields = null;
private ArrayList methods = null;

public DataClass(){

}

public void setClassName(String className){
this.className = className;

}

public String getClassName(){
return className;
}

public void setField(String field){
DataField df = new DataField();
df.setFieldName(field);
methods.add(df);
}

public void setMethod(String method){
DataMethod dm = new DataMethod();
dm.setMethodName(method);
methods.add(dm);
}


}

Listing 2
=========

public class DataField {

private String fieldName = null;

public void setField(String fieldName){
this.fieldName = fieldName;

}



}
 
R

Roedy Green

DataClass

this is a name worthy of inclusion in my essay on unmaintainable code.

It is about as vague as it gets, and it further confuses with the
ambiguity of whether you mean class in the sense of category or class
in the sense of Java classes.

Your question would be ever so much clearer with more concrete names.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
P

Petterson Mikael

Roedy said:
this is a name worthy of inclusion in my essay on unmaintainable code.

It is about as vague as it gets, and it further confuses with the
ambiguity of whether you mean class in the sense of category or class
in the sense of Java classes.

Your question would be ever so much clearer with more concrete names.

Hi,

I know that my code was a mess. I was in a hurry before a meeting ;-)

Anyway my app's purpose is to find out if I must update my simulated
framework if the real framework has changed ( interfaces).
My app loads two jar files ( real framework and simulated framwork) and
use bcel to make an static analysis of the classes ( bcel created
'JavaClass' to describe the class).
In order to determine if the classes are equal i create a 'DataClass'
object with the following information:
className
Field [] fields (bcel 'Field' not java.lang.reflect.Field)
Method [] methods (bcel 'Method' not java.lang.reflect.Method)

I retrieve the information from JavaClass and put it in my 'DataClass'.

This is the code I use ( hopefully more readable) to create a Vector for
each framework ( Listing 1). This is where I store the data describing
my classes in the api (Listing 2). Then I compare each 'DataClass'
contained in the Vector.

My intention is to compare the 'data' of the 'DataClass'. But since I
get that all DataClass objects are NOT equal I guess I compare
references. How can I do this.

All hints are vey much welcome.

cheers,

//mikael

Listing 1
==========
Note: ClassSet is a 'Set' of JavaClass' objects.

public static Vector makeDataObjects(ClassSet classes){
Vector api = new Vector();
JavaClass [] clazzes = classes.toArray();
for (int i = 0; i < clazzes.length; i++) {
JavaClass clazz = clazzes;
DataClass dc = new DataClass();
dc.setClassName(clazz.getClassName());
Field [] fields = clazz.getFields();
for(int j = 0; j < fields.length; j++){
dc.setField(fields[j].toString());
}
Method [] methods = clazz.getMethods();
for (int k = 0; k < methods.length; k++) {
dc.setMethod(methods[k].toString());
}

api.add(dc);

}
return api;
}


Listing 2
=========

mport java.util.ArrayList;


public class DataClass implements Comparable{


private String className = null;
private ArrayList fields = new ArrayList();
private ArrayList methods = new ArrayList();


public void setClassName(String className){
this.className = className;

}

public String getClassName(){
return className;
}

public void setField(String field){
fields.add(field);
}

public void setMethod(String method){
methods.add(method);
}

public int compareTo(Object o){

}

}

This is where I compare the content of my two api:s


Listing 3
=========
public static void compare(Vector api1, Vector api2) {
for (Iterator iter = api1.iterator(); iter.hasNext();) {
DataClass clazz1 = (DataClass) iter.next();
String className = (String) clazz1.getClassName();
DataClass clazz2 = (DataClass) toBeCompared(className, api2);
// compare object values
System.out.println("clazz1 is " + clazz1.toString());
System.out.println("clazz2 is " + clazz2.toString());
if (clazz1.equals(clazz2)) {
System.out.println("Objects are the SAME" + className);

} else {
System.out.println("Objects are NOT the same" + className);
}

}

}

private static DataClass toBeCompared(String className, Vector api){
DataClass dao = null;
for (Iterator iter = api.iterator(); iter.hasNext();) {
DataClass element = (DataClass) iter.next();
String otherClassName = element.getClassName();
if (className.equals(otherClassName)){
dao = element;

}
}
return dao;
}
 
I

iamfractal

Hej,

Something like:

class DataClass {
private List dataFields = new ArrayList();
private List dataMethods = new ArrayList();

public boolean equals(Object object) {
if (object instanceof DataClass) {
DataClass hisDataClass = (DataClass)object;
if (dataFields.equals(hisDataClass.dataFields)) {
return dataMethods.equals(hisDataClass.dataMethods) {
}
}
return false;
}
}

With similar, compound hashCode() method, and with DataField and
DataMethod employing prudent equasl() and hashCode() themselves. And
see
http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf
for a full equals().


..ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top