Skip to content

Instantly share code, notes, and snippets.

@aino-prashant
Last active May 23, 2018 04:31
Show Gist options
  • Save aino-prashant/bc6465ecea78929a72281b74f3f92a07 to your computer and use it in GitHub Desktop.
Save aino-prashant/bc6465ecea78929a72281b74f3f92a07 to your computer and use it in GitHub Desktop.
Static block of writer class
private void writeStaticBlock() {
sourceWriter.println("static{");
JMethod[] methods = pojoMarkerClass.getMethods();
JMethod[] overridableMethods = pojoMarkerClass.getOverridableMethods();
HashSet<JMethod> finalMethods = new HashSet<JMethod>();
finalMethods.addAll(Arrays.asList(methods));
finalMethods.addAll(Arrays.asList(overridableMethods));
for (JMethod method : finalMethods) {
if (!method.isAnnotationPresent(Worker.class) && isPojoMarkerMethod(method)) {
if (method.getName().startsWith("get") && method.getParameters().length == 0) {
String propertyName = getPropertyName(method);
JType methodReturnJType = method.getReturnType();
JClassType methodReturnJClassType = typeOracle.findType(methodReturnJType.getErasedType()
.getQualifiedSourceName());
if (method.isAnnotationPresent(UseProvider.class)) {
String contextId = method.getAnnotation(UseProvider.class).propertyKey();
Class<?> contextSource = method.getAnnotation(UseProvider.class).providerClazz();
sourceWriter.println("ArrayList<TypeKey> arrProviderTypeKeys = new ArrayList<TypeKey>();");
sourceWriter
.println("TypeKey propertyKeyTypeKey = new TypeKey(TypedAs.FIXED, String.class.getCanonicalName());");
sourceWriter
.println("TypeKey propertyValTypeKey = new TypeKey(TypedAs.FIXED, String.class.getCanonicalName());");
sourceWriter
.println("arrProviderTypeKeys.add(propertyKeyTypeKey);arrProviderTypeKeys.add(propertyValTypeKey);");
sourceWriter.println("typeMap.put(\"" + propertyName + "\", new ProviderTypeKey(\"" + contextId
+ "\",AppOpsCommonGinjector.INSTANCE.get" + contextSource.getSimpleName()
+ "(),TypedAs.VARIANT,\"" + methodReturnJType.getErasedType().getQualifiedSourceName()
+ "\",arrProviderTypeKeys));");
} else if (isPrimitive(methodReturnJType)) {
sourceWriter.println("typeMap.put(\"" + propertyName + "\", TypeKeyFactory.getTypeKey(\""
+ method.getReturnType().getErasedType().getQualifiedSourceName() + "\"));");
} else if (isCollection(methodReturnJClassType)) {
String typeSourceName = "";
if (method.isAnnotationPresent(UseTypeSource.class)) {
typeSourceName = method.getAnnotation(UseTypeSource.class).sourceName();
}
addCollectionTypeKey(methodReturnJType, methodReturnJClassType, propertyName, typeSourceName);
} else {
if (methodReturnJClassType.isInterface() != null || methodReturnJClassType.isAbstract()) {
sourceWriter.println("typeMap.put(\"" + propertyName + "\", new TypeKey(TypedAs.VARIANT,\""
+ methodReturnJType.getErasedType().getQualifiedSourceName() + "\"));");
}
/*
* This part is commented due to type key issue in referrer registry.
*/
else if (!isPrimitive(methodReturnJClassType) || methodReturnJClassType.isFinal()) {
sourceWriter.println("typeMap.put(\"" + propertyName + "\", new TypeKey(TypedAs.FIXED,\""
+ methodReturnJType.getErasedType().getQualifiedSourceName() + "\"));");
} // /
else {
sourceWriter.println("typeMap.put(\"" + propertyName + "\", new TypeKey(TypedAs.VARIANT,\""
+ methodReturnJType.getErasedType().getQualifiedSourceName() + "\"));");
}
}
}
}
}
sourceWriter.println("String pojoMarkerKey = \"\";");
if (pojoMarkerClass.isAnnotationPresent(Typename.class)
&& !pojoMarkerClass.getAnnotation(Typename.class).typename().isEmpty()) {
sourceWriter.println(" pojoMarkerKey = \"" + pojoMarkerClass.getAnnotation(Typename.class).typename()
+ "\";");
sourceWriter
.println("PojoMarkerImplInstantiatorRegistry.get().registerInstantiator( pojoMarkerKey , new PojoMarkerImplInstantiator<"
+ pojoMarkerClass.getName() + ">() {");
sourceWriter.println("@Override");
sourceWriter.println("public " + pojoMarkerClass.getName() + " createInstance() {");
sourceWriter.println("return GWT.create(" + pojoMarkerClass.getName() + ".class) ; ");
sourceWriter.println("}");
sourceWriter.println("});");
}
sourceWriter.println(" pojoMarkerKey = \"" + TypeUtil.getTypeKey(pojoMarkerClass.getQualifiedSourceName())
+ "\";");
sourceWriter
.println("PojoMarkerImplInstantiatorRegistry.get().registerInstantiator( pojoMarkerKey , new PojoMarkerImplInstantiator<"
+ pojoMarkerClass.getName() + ">() {");
sourceWriter.println("@Override");
sourceWriter.println("public " + pojoMarkerClass.getName() + " createInstance() {");
sourceWriter.println("return GWT.create(" + pojoMarkerClass.getName() + ".class) ; ");
sourceWriter.println("}");
sourceWriter.println("});");
sourceWriter.println("}");
}
private void addCollectionTypeKey(JType methodReturnType, JClassType jClassType, String propertyName,
String typeSourceName) {
String buffer = new String("");
if (!typeSourceName.isEmpty()) {
if (KnownCollectionTypeFactory.INSTANCE.isKnownType(jClassType.getQualifiedSourceName())) {
buffer = "new CustomSourceTypeKey(TypedAs.FIXED, \""
+ jClassType.getErasedType().getQualifiedSourceName() + "\",\"" + typeSourceName + "\",";
} else {
buffer = "new CustomSourceTypeKey(TypedAs.VARIANT, \"" + jClassType.getQualifiedSourceName() + "\",\""
+ typeSourceName + "\",";
}
} else {
if (KnownCollectionTypeFactory.INSTANCE.isKnownType(jClassType.getQualifiedSourceName())) {
buffer = "new CollectionTypeKey(TypedAs.FIXED, \""
+ jClassType.getErasedType().getQualifiedSourceName() + "\",";
} else {
buffer = "new CollectionTypeKey(TypedAs.VARIANT, \"" + jClassType.getQualifiedSourceName() + "\",";
}
}
JParameterizedType jParameterizedType = methodReturnType.isParameterized();
if (jParameterizedType != null) {
buffer += "new ArrayList<TypeKey>(Arrays.asList(";
for (JClassType arg : jParameterizedType.getTypeArgs()) {
if (arg.getQualifiedSourceName().equals("?") || arg.getQualifiedSourceName().equals("T")
|| arg.getQualifiedSourceName().contains("extends")) {
buffer += "new TypeKey(TypedAs.VARIANT,\"" + arg.getQualifiedSourceName() + "\")";
} else {
buffer += getParameterizeTypeKey(arg) + ",";
}
}
if (buffer.endsWith(","))
buffer = buffer.substring(0, buffer.length() - 1);
buffer += "))";
} else {
buffer += "new ArrayList<TypeKey>(Arrays.asList(";
buffer += "new TypeKey(TypedAs.VARIANT,null)";
buffer += "))";
}
buffer += ")";
sourceWriter.println("typeMap.put(\"" + propertyName + "\"," + buffer + ");");
}
@aino-prashant
Copy link
Author

please check line 28,how we'll create statement from following

sourceWriter.println("typeMap.put(\"" + propertyName + "\", new ProviderTypeKey(\"" + contextId
								+ "\", AppOpsCommonGinjector.INSTANCE.get" + contextSource.getSimpleName()
								+ "(),TypedAs.VARIANT,\"" + methodReturnJType.getErasedType().getQualifiedSourceName()
								+ "\",arrProviderTypeKeys));");

because here method name is getting after append.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment