Skip to content

Commit

Permalink
Fixed Jalopy 'error' catching with unformatted fallback sourcecode ou…
Browse files Browse the repository at this point in the history
…tput. Fixed parameter namings in generated cpp methods.
  • Loading branch information
Nicolas Gramlich committed Mar 27, 2012
1 parent 740574e commit f711331
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 38 deletions.
45 changes: 42 additions & 3 deletions src/org/andengine/extension/scripting/generator/Generator.java
Expand Up @@ -211,9 +211,33 @@ private void generateInterfaceHeader(final Class<?> pClass, final GenCppClassFil
}

private void generateInterfaceMethods(final Class<?> pClass, final GenCppClassFileWriter pGenCppClassFileWriter) {

for(final Method method : pClass.getMethods()) {
if(!this.isGenMethodExcluded(method)) {
final String methodName = method.getName();
if(methodName .startsWith("get") || methodName.startsWith("is") || methodName.startsWith("has") || methodName.startsWith("set") || methodName.startsWith("on")) {
this.generateIncludes(method.getParameterTypes(), pGenCppClassFileWriter);
this.generateInterfaceMethod(pClass, method, pGenCppClassFileWriter);
} else {
// System.err.println("Skipping interface method: " + pClass.getSimpleName() + "." + methodName + "(...) !");
}
}
}
}


private void generateInterfaceMethod(final Class<?> pClass, final Method pMethod, final GenCppClassFileWriter pGenCppClassFileWriter) {
final String returnType = Util.getGenCppParameterTypeName(pMethod.getReturnType(), this.mGenCppClassSuffix);
final String genCppMethodHeaderParamatersAsString = Util.getGenCppMethodHeaderParamatersAsString(pMethod, this.mGenCppClassSuffix);
final String methodName = pMethod.getName();

pGenCppClassFileWriter.append(GenCppClassHeaderFileSegment.METHODS_PUBLIC, "virtual").space().append(returnType).space().append(methodName);
pGenCppClassFileWriter.append(GenCppClassHeaderFileSegment.METHODS_PUBLIC, "(");
if(genCppMethodHeaderParamatersAsString != null) {
pGenCppClassFileWriter.append(GenCppClassHeaderFileSegment.METHODS_PUBLIC, genCppMethodHeaderParamatersAsString);
}
pGenCppClassFileWriter.append(GenCppClassHeaderFileSegment.METHODS_PUBLIC, ")");
pGenCppClassFileWriter.append(GenCppClassHeaderFileSegment.METHODS_PUBLIC, " = 0;").end();
}

private void generateInterfaceFooter(final Class<?> pClass, final GenCppClassFileWriter pGenCppClassFileWriter) {
/* Generate native footer. */
{
Expand Down Expand Up @@ -425,7 +449,7 @@ private void generateClassMethods(final Class<?> pClass, final GenJavaClassFileW
this.generateParameterImportsAndIncludes(method, pGenJavaClassFileWriter, pGenCppClassFileWriter);
this.generateCallback(pClass, method, pGenJavaClassFileWriter, pGenCppClassFileWriter);
} else {
// System.err.println("Skipping method: " + pClass.getSimpleName() + "." + methodName + "(...) !");
// System.err.println("Skipping class method: " + pClass.getSimpleName() + "." + methodName + "(...) !");
}
}
}
Expand Down Expand Up @@ -631,6 +655,21 @@ private void generateParameterImportsAndIncludes(final AccessibleObject pAccessi
}
}

private void generateIncludes(final AccessibleObject pAccessibleObject, final GenCppClassFileWriter pGenCppClassFileWriter) {
if(pAccessibleObject instanceof Constructor<?>) {
final Constructor<?> constructor = (Constructor<?>)pAccessibleObject;

this.generateIncludes(constructor.getParameterTypes(), pGenCppClassFileWriter);
} else if(pAccessibleObject instanceof Method) {
final Method method = (Method)pAccessibleObject;
final Class<?>[] parameterTypes = method.getParameterTypes();

this.generateIncludes(parameterTypes, pGenCppClassFileWriter);
} else {
throw new IllegalArgumentException();
}
}

private void generateImports(final Class<?>[] pTypes, final GenJavaClassFileWriter pGenJavaClassFileWriter) {
for(final Class<?> type : pTypes) {
if(!Util.isPrimitiveParameter(type)) {
Expand Down
77 changes: 53 additions & 24 deletions src/org/andengine/extension/scripting/generator/util/Util.java
Expand Up @@ -242,7 +242,7 @@ public static String getGenCppMethodParamatersAsString(final Class<?>[] pParamet
for(int i = 0; i < pParameterTypes.length; i++) {
final Class<?> parameterType = pParameterTypes[i];
final String parameterTypeName = Util.getGenCppParameterTypeName(parameterType, pGenCppClassSuffix);
final String parameterName = pParameterNames[i];
final String parameterName = pParameterNames[i] + pGenCppClassSuffix;

if(i == 0) {
stringBuilder.append("");
Expand Down Expand Up @@ -324,34 +324,63 @@ public static String getJNIParameterTypeName(final Class<?> parameterType) {
return parameterTypeName;
}

public static String getGenCppParameterTypeName(final Class<?> parameterType, final String pGenCppClassSuffix) {
final String parameterTypeName;
if(parameterType == Boolean.TYPE) {
parameterTypeName = "jboolean";
} else if(parameterType == Byte.TYPE) {
parameterTypeName = "jbyte";
} else if(parameterType == Character.TYPE) {
parameterTypeName = "jchar";
} else if(parameterType == Short.TYPE) {
parameterTypeName = "jshort";
} else if(parameterType == Integer.TYPE) {
parameterTypeName = "jint";
} else if(parameterType == Long.TYPE) {
parameterTypeName = "jlong";
} else if(parameterType == Float.TYPE) {
parameterTypeName = "jfloat";
} else if(parameterType == Double.TYPE) {
parameterTypeName = "jdouble";
public static String getGenCppParameterTypeName(final Class<?> pParameterType, final String pGenCppClassSuffix) {
if(pParameterType.isArray()) {
final Class<?> componentType = pParameterType.getComponentType();
if(componentType == Boolean.TYPE) {
return "jbooleanArray";
} else if(componentType == Byte.TYPE) {
return "jbyteArray";
} else if(componentType == Character.TYPE) {
return "jcharArray";
} else if(componentType == Short.TYPE) {
return "jshortArray";
} else if(componentType == Integer.TYPE) {
return "jintArray";
} else if(componentType == Long.TYPE) {
return "jlongArray";
} else if(componentType == Float.TYPE) {
return "jfloatArray";
} else if(componentType == Double.TYPE) {
return "jdoubleArray";
} else if(componentType == Object.class) {
return "jobjectArray";
} else {
throw new IllegalArgumentException();
}
} else {
// TODO Add import, when name != simplename.
// parameterTypeName = parameterType.getName();
parameterTypeName = parameterType.getSimpleName() + pGenCppClassSuffix + "*";
if(pParameterType == Void.TYPE) {
return "void";
} else if(pParameterType == Boolean.TYPE) {
return "jboolean";
} else if(pParameterType == Byte.TYPE) {
return "jbyte";
} else if(pParameterType == Character.TYPE) {
return "jchar";
} else if(pParameterType == Short.TYPE) {
return "jshort";
} else if(pParameterType == Integer.TYPE) {
return "jint";
} else if(pParameterType == Long.TYPE) {
return "jlong";
} else if(pParameterType == Float.TYPE) {
return "jfloat";
} else if(pParameterType == Double.TYPE) {
return "jdouble";
} else if(pParameterType == Object.class) {
return "jobject";
} else {
// TODO Add import, when name != simplename.
// return parameterType.getName();
return pParameterType.getSimpleName() + pGenCppClassSuffix + "*";
}
}
return parameterTypeName;
}

public static boolean isPrimitiveParameter(final Class<?> parameterType) {
if(parameterType == Boolean.TYPE) {
if(parameterType == Void.TYPE) {
return true;
} else if(parameterType == Boolean.TYPE) {
return true;
} else if(parameterType == Byte.TYPE) {
return true;
Expand Down
@@ -0,0 +1,55 @@
package org.andengine.extension.scripting.generator.util.adt;

/**
* (c) Zynga 2012
*
* @author Nicolas Gramlich <ngramlich@zynga.com>
* @since 14:03:14 - 27.03.2012
*/
public class FormatterException extends Exception {
// ===========================================================
// Constants
// ===========================================================

private static final long serialVersionUID = -721067925569804178L;

// ===========================================================
// Fields
// ===========================================================

// ===========================================================
// Constructors
// ===========================================================

public FormatterException() {

}

public FormatterException(final String pMessage) {
super(pMessage);
}

public FormatterException(final Throwable pThrowable) {
super(pThrowable);
}

public FormatterException(final String pMessage, final Throwable pThrowable) {
super(pMessage, pThrowable);
}

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
Expand Up @@ -17,20 +17,20 @@ public enum JavaFormatter implements IFormatter {

JALOPY() {
@Override
public String format(final String pString) {
public String format(final String pString) throws FormatterException {
final Jalopy jalopy = new Jalopy();
jalopy.setInput(pString, "<inline>");

final StringBuffer output = new StringBuffer();
jalopy.setOutput(output);
try {
jalopy.format();
} catch (Throwable t) {
t.printStackTrace();
return pString;
}

return output.toString();
final boolean success = jalopy.format();

if(success) {
return output.toString();
} else {
throw new FormatterException("Could not format:\n###########################\n" + pString + "\n###########################");
}
}
};

Expand Down
Expand Up @@ -7,6 +7,8 @@
import java.util.SortedMap;
import java.util.TreeMap;

import org.andengine.extension.scripting.generator.util.adt.FormatterException;

/**
* (c) Zynga 2012
*
Expand Down Expand Up @@ -88,10 +90,16 @@ public void end() throws IOException {
}
}

final String source = stringBuilder.toString();
if(this.mFormatter == null) {
writer.write(stringBuilder.toString());
writer.write(source);
} else {
writer.write(this.mFormatter.format(stringBuilder.toString()));
try {
writer.write(this.mFormatter.format(source));
} catch (FormatterException e) {
e.printStackTrace();
writer.write(source);
}
}

writer.flush();
Expand Down
@@ -1,5 +1,7 @@
package org.andengine.extension.scripting.generator.util.adt.io;

import org.andengine.extension.scripting.generator.util.adt.FormatterException;

/**
* (c) Zynga 2012
*
Expand All @@ -15,5 +17,5 @@ public interface IFormatter {
// Methods
// ===========================================================

public abstract String format(final String pString);
public String format(final String pString) throws FormatterException;
}

0 comments on commit f711331

Please sign in to comment.