class A{ }
class B{
A a=null; // class B has A.
B(A a){ // this is aggregation. Object [COLOR=red]A[/COLOR] can have a life even after
// [COLOR=red]B[/COLOR] class's instance is set to null. ( of course here also you
// have to create [COLOR=red]B[/COLOR] class's instance first )
this.a = a;
}
public String getObject(A a){ // this is aggregation.
return a.toString();
}
}
============End Aggregation=============================
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class Test {
static BufferedWriter bw=null;
static StringBuffer sb=new StringBuffer();
private static ArrayList<String> array=new ArrayList<String>(); //testing
public static void main(String[] args) {
try {
bw = new BufferedWriter(new FileWriter((new Long(new Date().getTime()).toString())));
String path = "p1//InformationTechnologyEmployee.java";
method(path);
for(int i=0;i<array.size();i++){
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.write(sb.toString());
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method(String path) {
if (path != null || path.length() > 0) {
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String eachLineReadInFile = null;
ArrayList<String> arrayListOfInstanceVariable = new ArrayList<String>();
HashMap<String,String> hmClassNameWithClassPath = new HashMap<String,String>(); // [class name=key] & [class path=value]
while ((eachLineReadInFile = br.readLine()) != null) {
String previousLine = new String(eachLineReadInFile);
String newline = new String(eachLineReadInFile.replaceAll("package|return|import|this|[{]|[}]|[/]|
[*]|final", ""));
if (eachLineReadInFile.indexOf("import") > -1) {
String filteredClassName = eachLineReadInFile.substring(eachLineReadInFile.lastIndexOf(".") + 1, eachLineReadInFile.length() - 1);
String filteredClassPath = eachLineReadInFile.replaceAll("import|[;]","").replaceAll("[.]", "//").trim();
hmClassNameWithClassPath.put(filteredClassName, filteredClassPath);
}
if (!newline.equals(eachLineReadInFile))
continue;
System.out.println(previousLine);
if (previousLine.trim().replaceAll("[;]", "").length() != 0){
String instanceVariable = previousLine.trim().replaceAll("[;]|public|static|protected|private","");
arrayListOfInstanceVariable.add(instanceVariable.trim());
}
}
System.out.println(arrayListOfInstanceVariable);
String classs = path.substring(path.lastIndexOf("//")+2,path.lastIndexOf("."));
String classReference = Character.toLowerCase(classs.charAt(0))+classs.substring(1);
String createNewInstance = classs+" "+classReference+" = new "+classs+"();\r";
array.add(classs.toLowerCase());
sb.insert(0,createNewInstance);
for (int nextInstance = 0; nextInstance < arrayListOfInstanceVariable.size(); nextInstance++) {
String previousDataTypeOfInstanceVariable = (String) arrayListOfInstanceVariable.get(nextInstance);
String newDataTypeOfInstanceVariable = new String(previousDataTypeOfInstanceVariable.replaceAll(
"int|float|byte|long|String|boolean|double", ""));
String methodName = Character.toUpperCase(previousDataTypeOfInstanceVariable.split(" ")[1].charAt(0))+previousDataTypeOfInstanceVariable.split(" ")[1].substring(1);
if (newDataTypeOfInstanceVariable.equals(previousDataTypeOfInstanceVariable)) {
String className = newDataTypeOfInstanceVariable.split(" ")[0];
String passingReference = Character.toLowerCase(previousDataTypeOfInstanceVariable.split(" ")[0].charAt(0))+previousDataTypeOfInstanceVariable.split(" ")[0].substring(1);
sb.append(classReference+".set"+methodName+"("+passingReference+");\r");
if(className.indexOf("List")>-1){
System.out.println("className before "+className);
className = className.split("[<]|[>]")[1];
}
if(!hmClassNameWithClassPath.containsKey(className)){
String pathForSamePackageVariable=path.substring(0,path.lastIndexOf("//")+2)+className;
hmClassNameWithClassPath.put(className,pathForSamePackageVariable);
}
String instanceRef = (String) hmClassNameWithClassPath.get(className);
method(instanceRef + ".java");
}else{
if(previousDataTypeOfInstanceVariable.indexOf("String")>-1){
passArgument(classReference, createNewInstance, methodName,"(null)");
}else{
if(previousDataTypeOfInstanceVariable.indexOf("boolean")>-1){
passArgument(classReference, createNewInstance, methodName,"(false)");
}else{
passArgument(classReference, createNewInstance, methodName,"(0)");
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
return;
}
}
private static void passArgument(String classReference, String createNewInstance, String methodName, String argument) {
sb.insert(createNewInstance.length(),classReference+".set"+methodName+argument+";\r");
}
}