Skip to content

Commit

Permalink
Merge pull request #990 from AdamBJ/FixForIssue969
Browse files Browse the repository at this point in the history
Fix for issue 969 and issue 986
  • Loading branch information
vahdat-ab committed Feb 4, 2017
2 parents 12f6b32 + 378b5bc commit 9f6ff7d
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 21 deletions.
37 changes: 22 additions & 15 deletions UmpleToJava/UmpleTLTemplates/class_MethodDeclaration.ump
Expand Up @@ -27,6 +27,12 @@ class UmpleToJava {
String methodImplementationModifier = aMethod.getIsAbstract() ? " abstract" : "";
String methodName = aMethod.getName();
String methodType = aMethod.getType();
// Fix issue 969
if (!methodName.equals(uClass.getName()))
{
// If this is not a constructor, this method should return "void"
methodType = methodType.equals("") ? "void" : methodType;
}
String customBeforeInjectionCode = GeneratorHelper.toCode(uClass.getApplicableCodeInjectionsCustomMethod("before", aMethod.getName(), aMethod.getMethodParameters()));
String customAfterInjectionCode = GeneratorHelper.toCode(uClass.getApplicableCodeInjectionsCustomMethod("after", aMethod.getName(), aMethod.getMethodParameters()));
String customPreconditionCode = GeneratorHelper.toCode(uClass.getApplicableCodeInjections("before", aMethod.getName()+"Precondition"));
Expand All @@ -42,21 +48,6 @@ class UmpleToJava {
String isList="";
String finalParams = "";
String finalParamsWithoutTypes = "";
if(methodName.equals("main")&&methodType.equals("void")&&methodModifier.contains("public")&&methodModifier.contains("static"))
{
String exceptionHandlerPackage = "";
if(mainMainClass!=null)
{
exceptionHandlerPackage = mainMainClass.getPackageName()+"."+mainMainClass.getName()+".";
}
else
{
mainMainClass = uClass;
}
properMethodBody = " Thread.currentThread().setUncaughtExceptionHandler(new "+exceptionHandlerPackage+"UmpleExceptionHandler());\n"+
" Thread.setDefaultUncaughtExceptionHandler(new "+exceptionHandlerPackage+"UmpleExceptionHandler());\n"+properMethodBody;
uClass.setHasMainMethod(true);
}
StringBuilder parameters = new StringBuilder();
StringBuilder parametersWithoutTypes = new StringBuilder();
if (aMethod.hasMethodParameters())
Expand All @@ -75,6 +66,22 @@ class UmpleToJava {
finalParamsWithoutTypes = parametersWithoutTypes.toString().substring(0, parametersWithoutTypes.toString().length()-2);
}

if(methodName.equals("main")&&methodType.equals("void")&&methodModifier.contains("public")&&methodModifier.contains("static")&&paramType.equals("String")&&isList.equals(" [] ")&&paramName.equals("args"))
{
String exceptionHandlerPackage = "";
if(mainMainClass!=null)
{
exceptionHandlerPackage = mainMainClass.getPackageName()+"."+mainMainClass.getName()+".";
}
else
{
mainMainClass = uClass;
}
properMethodBody = " Thread.currentThread().setUncaughtExceptionHandler(new "+exceptionHandlerPackage+"UmpleExceptionHandler());\n"+
" Thread.setDefaultUncaughtExceptionHandler(new "+exceptionHandlerPackage+"UmpleExceptionHandler());\n"+properMethodBody;
uClass.setHasMainMethod(true);
}

if (aMethod.numberOfComments() > 0) { append(realSb, "\n\n {0}", Comment.format("Method Javadoc",aMethod.getComments())); }

append(realSb,"\n");
Expand Down
4 changes: 0 additions & 4 deletions cruise.umple/src/UmpleInternalParser_CodeClass.ump
Expand Up @@ -2719,10 +2719,6 @@ private Boolean checkIsDistributed(UmpleInterface uInterface)
if (methodToken.is("methodName"))
{
aMethod.setName(methodToken.getValue());
if (methodToken.getValue().equals("main"))
{
uElement.setHasMainMethod(true);
}
}
if (methodToken.is("parameterList"))
{
Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.junit.*;

import cruise.umple.compiler.UmpleModel;
import cruise.umple.compiler.java.JavaClassGenerator;
import cruise.umple.util.SampleFileWriter;

import java.io.File;
Expand Down Expand Up @@ -169,6 +170,16 @@ public void assertUmpleProxyFor(String umpleFile, String codeFile, String classN
}
}


@After
public void tearDown() {
/* Nullify mainMainClass. It's a static variable, if we don't do this the state will
* affect the next set of JUnit tests that use mainMainClass.
*/

JavaClassGenerator.mainMainClass = null;
}

@Test
public void TestDistributableDirectivesTest1()
{
Expand Down
Expand Up @@ -24,8 +24,105 @@ public class Mentor
public void delete()
{}

public static main(){
public static void main(String [] args){
Thread.currentThread().setUncaughtExceptionHandler(new UmpleExceptionHandler());
Thread.setDefaultUncaughtExceptionHandler(new UmpleExceptionHandler());

}

public static class UmpleExceptionHandler implements Thread.UncaughtExceptionHandler
{
public void uncaughtException(Thread t, Throwable e)
{
translate(e);
if(e.getCause()!=null)
{
translate(e.getCause());
}
e.printStackTrace();
}
public void translate(Throwable e)
{
java.util.List<StackTraceElement> result = new java.util.ArrayList<StackTraceElement>();
StackTraceElement[] elements = e.getStackTrace();
try
{
for(StackTraceElement element:elements)
{
String className = element.getClassName();
String methodName = element.getMethodName();
boolean methodFound = false;
int index = className.lastIndexOf('.')+1;
try {
java.lang.reflect.Method query = this.getClass().getMethod(className.substring(index)+"_"+methodName,new Class[]{});
UmpleSourceData sourceInformation = (UmpleSourceData)query.invoke(this,new Object[]{});
for(int i=0;i<sourceInformation.size();++i)
{
int distanceFromStart = element.getLineNumber()-sourceInformation.getJavaLine(i)-(("main".equals(methodName))?2:0);
if(distanceFromStart>=0&&distanceFromStart<=sourceInformation.getLength(i))
{
result.add(new StackTraceElement(element.getClassName(),element.getMethodName(),sourceInformation.getFileName(i),sourceInformation.getUmpleLine(i)+distanceFromStart));
methodFound = true;
break;
}
}
}
catch (Exception e2){}
if(!methodFound)
{
result.add(element);
}
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
e.setStackTrace(result.toArray(new StackTraceElement[0]));
}
//The following methods Map Java lines back to their original Umple file / line
public UmpleSourceData Mentor_main(){ return new UmpleSourceData().setFileNames("ClassTemplateTest_Generated.ump").setUmpleLines(5).setJavaLines(28).setLengths(3);}

}
public static class UmpleSourceData
{
String[] umpleFileNames;
Integer[] umpleLines;
Integer[] umpleJavaLines;
Integer[] umpleLengths;

public UmpleSourceData(){
}
public String getFileName(int i){
return umpleFileNames[i];
}
public Integer getUmpleLine(int i){
return umpleLines[i];
}
public Integer getJavaLine(int i){
return umpleJavaLines[i];
}
public Integer getLength(int i){
return umpleLengths[i];
}
public UmpleSourceData setFileNames(String... filenames){
umpleFileNames = filenames;
return this;
}
public UmpleSourceData setUmpleLines(Integer... umplelines){
umpleLines = umplelines;
return this;
}
public UmpleSourceData setJavaLines(Integer... javalines){
umpleJavaLines = javalines;
return this;
}
public UmpleSourceData setLengths(Integer... lengths){
umpleLengths = lengths;
return this;
}
public int size(){
return umpleFileNames.length;
}
}
}
Expand Up @@ -2,6 +2,6 @@ namespace example;

class Mentor
{
public static main()
public static main(String[] args)
{}
}
@@ -0,0 +1,31 @@
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE @UMPLE_VERSION@ modeling language!*/

package example;

public class Mentor
{

//------------------------
// MEMBER VARIABLES
//------------------------

//------------------------
// CONSTRUCTOR
//------------------------

public Mentor()
{}

//------------------------
// INTERFACE
//------------------------

public void delete()
{}

public void main(String [] args){

}

}
@@ -0,0 +1,7 @@
namespace example;

class Mentor
{
public main(String[] args)
{}
}
@@ -0,0 +1,31 @@
/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE ${last.version} modeling language!*/

package example;

public class Mentor
{

//------------------------
// MEMBER VARIABLES
//------------------------

//------------------------
// CONSTRUCTOR
//------------------------

public Mentor()
{}

//------------------------
// INTERFACE
//------------------------

public void delete()
{}

public static void main(){

}

}
@@ -0,0 +1,7 @@
namespace example;

class Mentor
{
public static void main()
{}
}

0 comments on commit 9f6ff7d

Please sign in to comment.