package pt.estgp.estgweb.services;
import com.eztech.util.FileUtils;
import com.eztech.util.JavaClassFinder;
import com.sun.javadoc.*;
import com.sun.tools.javadoc.Main;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.domain.DomainObject;
import pt.estgp.estgweb.domain.DomainSerializableObject;
import pt.estgp.estgweb.domain.typesgen.VariableType;
import pt.estgp.estgweb.utils.ClassUtils;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.List;
/**
* Created by jorgemachado on 21/06/17.
*/
public class ServiceFinder
{
public static List
<Class
<? extends IService
>> classes =
null;
public static List
<Class
<? extends DomainSerializableObject
>> domainClasses =
null;
void init
() {
JavaClassFinder classFinder =
new JavaClassFinder
();
classes = classFinder.
findAllMatchingTypes(IService.
class);
for (1.5.0/docs/api/java/lang/Class.html">Class serviceClass : classes
) {
1.5.0/docs/api/java/lang/System.html">System.
out.
println(serviceClass.
getName());
}
JavaClassFinder classFinder2 =
new JavaClassFinder
();
domainClasses = classFinder2.
findAllMatchingTypes(DomainSerializableObject.
class);
for (1.5.0/docs/api/java/lang/Class.html">Class serviceClass : classes
) {
1.5.0/docs/api/java/lang/System.html">System.
out.
println(serviceClass.
getName());
}
1.5.0/docs/api/java/lang/System.html">System.
out.
println("#####Domain Classes:");
for (1.5.0/docs/api/java/lang/Class.html">Class domainClass : domainClasses
) {
1.5.0/docs/api/java/lang/System.html">System.
out.
println(domainClass.
getName());
}
}
static JSONObject serviceProcessing =
null;
static 1.5.0/docs/api/java/lang/String.html">String packpath
;
/**
* @param packpathStr este xpto parametro
* @return nada
* @throws JSONException quando nao sei o que
*/
public JSONArray getIServiceClassesJson
(1.5.0/docs/api/java/lang/String.html">String packpathStr,
1.5.0/docs/api/java/lang/String.html">String srcRoot
) throws JSONException
{
packpath = packpathStr
;
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Generating SOA FILE");
if (classes ==
null)
init
();
JSONArray array =
new JSONArray
();
for (1.5.0/docs/api/java/lang/Class.html">Class serviceClass : ServiceFinder.
classes) {
1.5.0/docs/api/java/lang/String.html">String fileJava = srcRoot + serviceClass.
getName().
replaceAll("\\.",
"/") +
".java";
1.5.0/docs/api/java/lang/System.html">System.
out.
println(fileJava
);
1.5.0/docs/api/java/lang/System.html">System.
out.
println(serviceClass.
getName());
serviceProcessing =
new JSONObject
();
try{
Main.
execute("", Analyzer.
class.
getName(),
new 1.5.0/docs/api/java/lang/String.html">String[]{fileJava
});
array.
put(serviceProcessing
);
}catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e
)
{
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Classe falhou " + fileJava +
" Continuando");
}
}
return array
;
}
public JSONArray getDomainObjectClassesJson
(1.5.0/docs/api/java/lang/String.html">String srcRoot
) throws JSONException
{
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Generating Domain Objects FILE");
if (domainClasses ==
null)
init
();
JSONArray array =
new JSONArray
();
for (1.5.0/docs/api/java/lang/Class.html">Class domainClass : ServiceFinder.
domainClasses) {
1.5.0/docs/api/java/lang/String.html">String fileJava = srcRoot + domainClass.
getName().
replaceAll("\\.",
"/") +
".java";
1.5.0/docs/api/java/lang/System.html">System.
out.
println(fileJava
);
1.5.0/docs/api/java/lang/System.html">System.
out.
println(domainClass.
getName());
try{
if(domainClass.
getName() ==
null || domainClass.
getName().
trim().
length() == 0
)
1.5.0/docs/api/java/lang/System.html">System.
out.
print("");
if(domainClass.
getName().
indexOf("$") >= 0
)
continue;
VariableType variableType =
new VariableType
();
variableType.
setClassName(domainClass.
getName());
variableType.
setShortName(domainClass.
getSimpleName());
variableType.
setClassNameSuperClasses(ClassUtils.
getAllSuperClasses(domainClass
));
array.
put(variableType.
toJsonObject());
}catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e
)
{
e.
printStackTrace();
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Continuando");
}
}
return array
;
}
public static final 1.5.0/docs/api/java/lang/String.html">String SERVICES_SOA_FILE =
"/pt/estgp/estgweb/services/services.json";
public static final 1.5.0/docs/api/java/lang/String.html">String DOMAIN_OBJECTS_FILE =
"/pt/estgp/estgweb/domain/domainObjects.json";
public static void main
(1.5.0/docs/api/java/lang/String.html">String[] args
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException
{
1.5.0/docs/api/java/lang/String.html">String classesDir = args
[0
];
1.5.0/docs/api/java/lang/String.html">String srcDir = args
[1
];
//String classesDir = "/Volumes/Home/jorgemachado/Documents/workspace/baco/impl/build/ant/classes/";
//String srcDir = "/Volumes/Home/jorgemachado/Documents/workspace/baco/impl/src/java/";
Class
<ServiceFinder
> toMatch = ServiceFinder.
class;
1.5.0/docs/api/java/lang/String.html">String classPathRoot = FileUtils.
determineClassPathBase(toMatch
);
1.5.0/docs/api/java/lang/System.html">System.
setProperty(JavaClassFinder.
CUSTOM_CLASS_PATH_PROPERTY, classPathRoot
);
1.5.0/docs/api/java/lang/String.html">String soaOutput = classesDir + SERVICES_SOA_FILE
;
JSONArray servicesArray =
new ServiceFinder
().
getIServiceClassesJson(ServiceFinder.
class.
getPackage().
getName(), srcDir
);
1.5.0/docs/api/java/io/FileWriter.html">FileWriter fw =
new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(soaOutput
);
fw.
write(servicesArray.
toString());
fw.
flush();
fw.
close();
1.5.0/docs/api/java/lang/String.html">String classPathDomainRoot = FileUtils.
determineClassPathBase(DomainObject.
class);
1.5.0/docs/api/java/lang/System.html">System.
setProperty(JavaClassFinder.
CUSTOM_CLASS_PATH_PROPERTY, classPathDomainRoot
);
1.5.0/docs/api/java/lang/String.html">String domainOutput = classesDir + DOMAIN_OBJECTS_FILE
;
JSONArray domainObjectClasses =
new ServiceFinder
().
getDomainObjectClassesJson(srcDir
);
1.5.0/docs/api/java/io/FileWriter.html">FileWriter fw2 =
new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(domainOutput
);
fw2.
write(domainObjectClasses.
toString());
fw2.
flush();
fw2.
close();
}
public static class Analyzer
extends Doclet
{
public static boolean start
(RootDoc root
) {
for (ClassDoc classDoc : root.
classes())
{
try {
1.5.0/docs/api/java/lang/Class.html">Class serviceClass =
1.5.0/docs/api/java/lang/Class.html">Class.
forName(classDoc.
qualifiedName());
obtainMethodsDescriptionJson
(classDoc, serviceClass, serviceProcessing
);
} catch (JSONException e
) {
throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("error in class=" + classDoc.
qualifiedName() + e
);
} catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e
) {
throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("error in class=" + classDoc.
qualifiedName() + e
);
}
}
return false;
}
}
private static void obtainMethodsDescriptionJson
(ClassDoc classDoc,
1.5.0/docs/api/java/lang/Class.html">Class serviceClass, JSONObject serviceProcessing
) throws JSONException
{
serviceProcessing.
put("serviceRelativeName", serviceClass.
getName().
substring(packpath.
length() + 1
));
1.5.0/docs/api/java/lang/System.html">System.
out.
println("packpath:" + packpath
);
serviceProcessing.
put("serviceRelativeName", serviceClass.
getName().
substring(packpath.
length() + 1
));
1.5.0/docs/api/java/lang/System.html">System.
out.
println("serviceClass.getPackage:" + serviceClass.
getPackage().
getName());
1.5.0/docs/api/java/lang/String.html">String serviceModule = serviceClass.
getPackage().
getName().
substring(packpath.
length() + 1
);
1.5.0/docs/api/java/lang/String.html">String subServiceName = serviceClass.
getSimpleName();
serviceProcessing.
put("serviceModule", serviceModule
);
serviceProcessing.
put("subServiceName", subServiceName
);
obtainClassDescription
(classDoc, serviceClass, serviceProcessing
);
}
public static JSONObject obtainClassDescription
( 1.5.0/docs/api/java/lang/Class.html">Class serviceClass
) throws JSONException
{
JSONObject serviceProcessing =
new JSONObject
();
obtainClassDescription
(null,serviceClass,serviceProcessing
);
return serviceProcessing
;
}
public static enum PrimitiveType
{
1.5.0/docs/api/java/lang/Integer.html">Integer("int",java.
lang.
1.5.0/docs/api/java/lang/Integer.html">Integer.
class),
1.5.0/docs/api/java/lang/Double.html">Double("double",java.
lang.
1.5.0/docs/api/java/lang/Double.html">Double.
class),
1.5.0/docs/api/java/lang/Boolean.html">Boolean("boolean",java.
lang.
1.5.0/docs/api/java/lang/Boolean.html">Boolean.
class),
1.5.0/docs/api/java/lang/Float.html">Float("float",java.
lang.
1.5.0/docs/api/java/lang/Float.html">Float.
class),
// Byte("byte",java.lang.Byte.class),
// Char("char",java.lang.Character.class),
1.5.0/docs/api/java/lang/Short.html">Short("short",java.
lang.
1.5.0/docs/api/java/lang/Short.html">Short.
class),
1.5.0/docs/api/java/lang/Long.html">Long("long",java.
lang.
1.5.0/docs/api/java/lang/Long.html">Long.
class);
private 1.5.0/docs/api/java/lang/Class.html">Class classType
;
private 1.5.0/docs/api/java/lang/String.html">String primitiveName
;
private PrimitiveType
(1.5.0/docs/api/java/lang/String.html">String primitiveName,
1.5.0/docs/api/java/lang/Class.html">Class classType
) {
this.
classType = classType
;
this.
primitiveName = primitiveName
;
}
public static PrimitiveType parse
(1.5.0/docs/api/java/lang/String.html">String primitiveName
)
{
for(PrimitiveType type: PrimitiveType.
values())
{
if(type.
primitiveName.
equals(primitiveName
))
{
return type
;
}
}
return null;
}
public static boolean isPrimitiveType
(1.5.0/docs/api/java/lang/String.html">String primitiveName
)
{
PrimitiveType parsed = parse
(primitiveName
);
return parsed
!=
null;
}
public 5+0%2Fdocs%2Fapi+Object">Object createInstance
(1.5.0/docs/api/java/lang/String.html">String value
)
{
try {
classType.
newInstance();
1.5.0/docs/api/java/lang/reflect/Constructor.html">Constructor c = classType.
getConstructor(1.5.0/docs/api/java/lang/String.html">String.
class);
5+0%2Fdocs%2Fapi+Object">Object o = c.
newInstance(value
);
return o
;
} catch (1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException e
) {
e.
printStackTrace();
} catch (1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException e
) {
e.
printStackTrace();
} catch (1.5.0/docs/api/java/lang/NoSuchMethodException.html">NoSuchMethodException e
) {
e.
printStackTrace();
} catch (1.5.0/docs/api/java/lang/reflect/InvocationTargetException.html">InvocationTargetException e
) {
e.
printStackTrace();
}
return null;
}
}
private static void obtainClassDescription
(ClassDoc classDoc,
1.5.0/docs/api/java/lang/Class.html">Class serviceClass, JSONObject serviceProcessing
) throws JSONException
{
//System.out.println("ClassName:" + serviceClass.getName());
serviceProcessing.
put("className", serviceClass.
getName());
JSONArray methods =
new JSONArray
();
serviceProcessing.
put("methods", methods
);
if(classDoc ==
null)
{
for(1.5.0/docs/api/java/lang/reflect/Method.html">Method m : serviceClass.
getMethods())
{
if(1.5.0/docs/api/java/lang/reflect/Modifier.html">Modifier.
isPublic(m.
getModifiers()) && !1.5.0/docs/api/java/lang/reflect/Modifier.html">Modifier.
isStatic(m.
getModifiers()))
{
JSONObject method =
new JSONObject
();
methods.
put(method
);
method.
put("name",m.
getName());
if(!m.
getReturnType().
getName().
equals("void") ) //&& !PrimitiveType.isPrimitiveType(m.getReturnType().getName())
{
method.
put("returnTypeSuperClasses", ClassUtils.
getAllSuperClassesJsonArray(m.
getReturnType()));
}
method.
put("returnType",m.
getReturnType().
getName());
method.
put("simpleReturnType",m.
getReturnType().
getSimpleName());
JSONArray types =
new JSONArray
();
method.
put("arguments", types
);
for(1.5.0/docs/api/java/lang/Class.html">Class t : m.
getParameterTypes())
{
JSONObject parameter =
new JSONObject
();
types.
put(parameter
);
if(!t.
getName().
equals("void") ) //&& !PrimitiveType.isPrimitiveType(t.getName())
{
parameter.
put("classNameSuperClasses", ClassUtils.
getAllSuperClassesJsonArray(t
));
}
parameter.
put("name",t.
getSimpleName());
parameter.
put("simpleClassName",t.
getSimpleName());
parameter.
put("className",t.
getName());
}
}
}
}
else
{
for (MethodDoc methodDoc : classDoc.
methods())
{
if (methodDoc.
isPublic() && !methodDoc.
isStatic()) {
JSONObject method =
new JSONObject
();
methods.
put(method
);
method.
put("name", methodDoc.
name());
method.
put("siganture", methodDoc.
signature());
method.
put("simpleReturnType", methodDoc.
returnType().
typeName());
method.
put("returnType", methodDoc.
returnType().
toString());
try
{
if(!methodDoc.
returnType().
toString().
equals("void") ) //&& !PrimitiveType.isPrimitiveType(methodDoc.returnType().toString())
{
1.5.0/docs/api/java/lang/Class.html">Class returnTypeClass = getaClass
(methodDoc.
returnType().
toString());
method.
put("returnTypeSuperClasses", ClassUtils.
getAllSuperClassesJsonArray(returnTypeClass
));
}
}
catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e
)
{
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Class not used, fail: " + methodDoc.
returnType().
toString());
}
method.
put("document",methodDoc.
getRawCommentText());
JSONArray types =
new JSONArray
();
method.
put("arguments", types
);
if(methodDoc.
parameters() !=
null)
for (1.5.0/docs/api/org/omg/Dynamic/Parameter.html">Parameter t : methodDoc.
parameters())
{
JSONObject parameter =
new JSONObject
();
types.
put(parameter
);
if(!t.
type().
qualifiedTypeName().
equals("void") )//&& !PrimitiveType.isPrimitiveType(t.type().qualifiedTypeName()))
{
try {
1.5.0/docs/api/java/lang/Class.html">Class argTypeClass = getaClass
(t.
type().
qualifiedTypeName());
parameter.
put("classNameSuperClasses", ClassUtils.
getAllSuperClassesJsonArray(argTypeClass
));
} catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e
) {
1.5.0/docs/api/java/lang/System.html">System.
out.
println("Class not used, fail: " + t.
type().
qualifiedTypeName());
}
}
parameter.
put("name",t.
name());
parameter.
put("simpleClassName",t.
typeName());
parameter.
put("className",t.
type().
qualifiedTypeName());
}
}
//System.out.println(" " + methodDoc.returnType() + " " + methodDoc.name() + methodDoc.signature());
//System.out.println(methodDoc.getRawCommentText());
}
}
}
private static 1.5.0/docs/api/java/lang/Class.html">Class getaClass
(1.5.0/docs/api/java/lang/String.html">String t
) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException
{
PrimitiveType pType = PrimitiveType.
parse(t
);
if(pType
!=
null)
return pType.
classType;
return 1.5.0/docs/api/java/lang/Class.html">Class.
forName(t
);
}
/*
public static void main(String [] args) throws JSONException {
System.out.println(getAllSuperClasses(User.class));
System.out.println(DomainObject.class.isAssignableFrom(Argument.class));
JSONObject serviceProcessing = new JSONObject();
obtainClassDescription(null,pt.estgp.estgweb.services.assessments.ScheduleAssessmentService.class,serviceProcessing);
System.out.println(serviceProcessing.toString());
}
*/
}