List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration setReturnType2
public void setReturnType2(Type type)
From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.AbstractStateMachineTestStrategy.java
License:Open Source License
/** * Creates abstract methods which return true * @param javadoc/*from w ww . j av a 2 s . c o m*/ * @param methodName * @param ast * @return an AST method declaration */ protected MethodDeclaration createAbstractAssertMethod(Javadoc javadoc, String methodName, AST ast) { MethodDeclaration method = ast.newMethodDeclaration(); method.setJavadoc(javadoc); method.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); method.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.ABSTRACT_KEYWORD)); method.setName(ast.newSimpleName(methodName)); //escape all spaces in state name /** * add to method generic arguments Object... arguments */ SingleVariableDeclaration param = ast.newSingleVariableDeclaration(); param.setName(ast.newSimpleName("arguments")); param.setType(ast.newSimpleType(ast.newName("Object"))); param.setStructuralProperty(SingleVariableDeclaration.VARARGS_PROPERTY, true); method.setReturnType2(ast.newPrimitiveType(PrimitiveType.BOOLEAN)); return method; }
From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.impl.PathWithUncertaintyTestStrategy.java
License:Open Source License
/** * Generates a class to be used in executing the test plan. * The class is abstract because at this point it is unclear how to assert that a certain state has been reached. * Thus, the assertStateReached will be abstract methods. * @param stateGraph//from w w w . j a v a 2s .co m */ public Document generateTestPlan(StateMachineStateGraph stateGraph) { Document doc = new Document( "public abstract class TestPlanForStateMachine" + stateGraph.getStateMachineName() + " { \n"); //from here we use the cumbersome and extremely detailed Eclipse recommended DOM/AST library ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.recordModifications(); AST ast = cu.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); //create method which will contain one test plan (might be cloned and branched for each if-else in the state machine diagram) MethodDeclaration testPlanMethodDeclaration = ast.newMethodDeclaration(); testPlanMethodDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); testPlanMethodDeclaration.setName(ast.newSimpleName("testPlan")); testPlanMethodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); //return true if successful or false otherwise //create method body Block testPlanMethodBody = ast.newBlock(); testPlanMethodDeclaration.setBody(testPlanMethodBody); //create recursively the test plan by parsing the state graph starting with initial state try { generatePlanForState(stateGraph.getInitialState(), rewriter, testPlanMethodDeclaration, new HashSet<StateMachineStateTransition>()); } catch (NoSuchStateException e) { e.printStackTrace(); } ListRewrite listRewrite = rewriter.getListRewrite(cu, CompilationUnit.TYPES_PROPERTY); //add all generated abstract methods for (Map.Entry<String, MethodDeclaration> entry : generatedAbstractMethods.entrySet()) { listRewrite.insertLast(entry.getValue(), null); } if (generatedPlans.isEmpty()) { notifyUser("No test plans containing uncertainty states could have been generated. " + "\n Please ensure selected state machine has at least one state with at least one uncertainty" + "\n Please ensure there is at least one InitialState, one FinalState, and one path between Initial and Final states which passes through" + "at least one state with at least one uncertainty"); } int index = 1; //add generated plan methods for (Map.Entry<String, MethodDeclaration> entry : generatedPlans.entrySet()) { //rename to PLAN_METHOD_LEADING + plan index from PLAN_METHOD_LEADING + UUID MethodDeclaration method = entry.getValue(); method.setName(ast.newSimpleName(PLAN_METHOD_LEADING + index++)); listRewrite.insertLast(method, null); } //add final } listRewrite.insertLast(rewriter.createStringPlaceholder("}\n", ASTNode.EMPTY_STATEMENT), null); TextEdit edits = rewriter.rewriteAST(doc, null); try { UndoEdit undo = edits.apply(doc); } catch (MalformedTreeException | BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(doc.get()); return doc; }
From source file:ac.at.tuwien.dsg.uml.statemachine.export.transformation.engines.impl.TransitionCorrectnessTestStrategy.java
License:Open Source License
/** * Generates a class to be used in executing the test plan. * The class is abstract because at this point it is unclear how to assert that a certain state has been reached. * Thus, the assertStateReached will be abstract methods. * @param stateGraph/*from w ww.ja va 2 s . com*/ */ public Document generateTestPlan(StateMachineStateGraph stateGraph) { Document doc = new Document( "public abstract class TestPlanForStateMachine" + stateGraph.getStateMachineName() + " { \n"); //from here we use the cumbersome and extremely detailed Eclipse recommended DOM/AST library ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.recordModifications(); AST ast = cu.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); //create method which will contain one test plan (might be cloned and branched for each if-else in the state machine diagram) MethodDeclaration testPlanMethodDeclaration = ast.newMethodDeclaration(); testPlanMethodDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); testPlanMethodDeclaration.setName(ast.newSimpleName("testPlan")); testPlanMethodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); //return true if successful or false otherwise //create method body Block testPlanMethodBody = ast.newBlock(); testPlanMethodDeclaration.setBody(testPlanMethodBody); //create recursively the test plan by parsing the state graph starting with initial state try { generatePlanForState(stateGraph.getInitialState(), rewriter, testPlanMethodDeclaration, new HashSet<StateMachineStateTransition>()); } catch (NoSuchStateException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } ListRewrite listRewrite = rewriter.getListRewrite(cu, CompilationUnit.TYPES_PROPERTY); //add all generated abstract methods for (Map.Entry<String, MethodDeclaration> entry : generatedAbstractMethods.entrySet()) { listRewrite.insertLast(entry.getValue(), null); } int index = 1; //add generated plan methods if (generatedPlans.isEmpty()) { notifyUser("No test plans could have been generated. " + "\n Please ensure selected state machine has at least one complete path from initial to final state." + "\n Please ensure there is at least one InitialState, one FinalState, and one path between Initial and Final states"); } for (Map.Entry<String, MethodDeclaration> entry : generatedPlans.entrySet()) { //rename to PLAN_METHOD_LEADING + plan index from PLAN_METHOD_LEADING + UUID MethodDeclaration method = entry.getValue(); method.setName(ast.newSimpleName(PLAN_METHOD_LEADING + index++)); listRewrite.insertLast(method, null); } //add final } listRewrite.insertLast(rewriter.createStringPlaceholder("}\n", ASTNode.EMPTY_STATEMENT), null); TextEdit edits = rewriter.rewriteAST(doc, null); try { UndoEdit undo = edits.apply(doc); } catch (MalformedTreeException | BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(doc.get()); return doc; }
From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java
License:Apache License
private static void merge(CompilationUnit unit, String pkgName, String typeName, String auth, String dbName, List<String> tableCreators) { unit.recordModifications();//w ww .jav a2 s . c om AST ast = unit.getAST(); TypeDeclaration type = (TypeDeclaration) unit.types().get(0); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName("cn.ieclipse.aorm.Session")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.content.UriMatcher")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.database.sqlite.SQLiteDatabase")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.database.sqlite.SQLiteOpenHelper")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("java.net.Uri")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.content.ContentValue")); unit.imports().add(id); VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("AUTH")); StringLiteral sl = ast.newStringLiteral(); sl.setLiteralValue(auth); vdf.setInitializer(sl); FieldDeclaration fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))); fd.setType(ast.newSimpleType(ast.newSimpleName("String"))); int i = 0; type.bodyDeclarations().add(i++, fd); // URI = Uri.parse("content://" + AUTH); vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("URI")); MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("Uri")); mi.setName(ast.newSimpleName("parse")); InfixExpression fix = ast.newInfixExpression(); fix.setOperator(InfixExpression.Operator.PLUS); sl = ast.newStringLiteral(); sl.setLiteralValue("content://"); fix.setLeftOperand(sl); fix.setRightOperand(ast.newSimpleName("AUTH")); mi.arguments().add(fix); vdf.setInitializer(mi); fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))); fd.setType(ast.newSimpleType(ast.newSimpleName("Uri"))); type.bodyDeclarations().add(i++, fd); // private mOpenHelper; vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("mOpenHelper")); fd = ast.newFieldDeclaration(vdf); fd.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); fd.setType(ast.newSimpleType(ast.newName("SQLiteOpenHelper"))); type.bodyDeclarations().add(i++, fd); // private static session; vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("session")); fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PRIVATE | Modifier.STATIC))); fd.setType(ast.newSimpleType(ast.newName("Session"))); type.bodyDeclarations().add(i++, fd); // public static Session getSession(){ // return session; // } MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC))); md.setReturnType2(ast.newSimpleType(ast.newName("Session"))); md.setName(ast.newSimpleName("getSession")); Block methodBlock = ast.newBlock(); ReturnStatement returnStmt = ast.newReturnStatement(); returnStmt.setExpression(ast.newSimpleName("session")); methodBlock.statements().add(returnStmt); md.setBody(methodBlock); type.bodyDeclarations().add(i, md); // modify onCreate rewriteOnCreate(unit, dbName, tableCreators); }
From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java
License:Apache License
private static void genInnerSQLiteOpenHelper(AnonymousClassDeclaration acd, AST ast, List<String> tableCreators) { MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC))); md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); md.setName(ast.newSimpleName("onCreate")); SingleVariableDeclaration svd = ast.newSingleVariableDeclaration(); svd.setName(ast.newSimpleName("db")); svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase"))); md.parameters().add(svd);/*from w ww .ja va 2 s . co m*/ Block innerBlock = ast.newBlock(); md.setBody(innerBlock); VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("sql")); StringLiteral sl = ast.newStringLiteral(); sl.setLiteralValue(""); vdf.setInitializer(sl); VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf); vds.setType(ast.newSimpleType(ast.newSimpleName("String"))); innerBlock.statements().add(vds); for (String creator : tableCreators) { String[] lines = creator.split(SourceAnalysis.LF); for (String line : lines) { Assignment a = ast.newAssignment(); a.setOperator(Assignment.Operator.PLUS_ASSIGN); a.setLeftHandSide(ast.newSimpleName("sql")); StringLiteral temp = ast.newStringLiteral(); temp.setLiteralValue(line); a.setRightHandSide(temp); innerBlock.statements().add(ast.newExpressionStatement(a)); } MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("execSQL")); mi.setExpression(ast.newSimpleName("db")); mi.arguments().add(ast.newSimpleName("sql")); innerBlock.statements().add(ast.newExpressionStatement(mi)); } acd.bodyDeclarations().add(md); // onUpgrade md = ast.newMethodDeclaration(); md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC))); md.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); md.setName(ast.newSimpleName("onUpgrade")); svd = ast.newSingleVariableDeclaration(); svd.setName(ast.newSimpleName("db")); svd.setType(ast.newSimpleType(ast.newSimpleName("SQLiteDatabase"))); md.parameters().add(svd); svd = ast.newSingleVariableDeclaration(); svd.setName(ast.newSimpleName("oldVersion")); svd.setType(ast.newPrimitiveType(PrimitiveType.INT)); md.parameters().add(svd); svd = ast.newSingleVariableDeclaration(); svd.setName(ast.newSimpleName("newVersion")); svd.setType(ast.newPrimitiveType(PrimitiveType.INT)); md.parameters().add(svd); innerBlock = ast.newBlock(); md.setBody(innerBlock); acd.bodyDeclarations().add(md); }
From source file:com.crispico.flower.codesync.tests.java.JavaTestsOnSourceCode.java
License:Open Source License
@SuppressWarnings("unchecked") public void testMethod_Adders() { javaCompilationUnit = JavaSyncUtils.loadJavaFile(folder.getFile("ClassA.java"), ASTParser.newParser(AST.JLS3)); javaCompilationUnit.recordModifications(); javaClass = JavaSyncUtils.getMasterClass(javaCompilationUnit); SingleVariableDeclaration variableDeclaration = null; MethodDeclaration methodDeclaration = null; AST ast = javaClass.getAST();/*from w w w. j a va2 s .c om*/ methodDeclaration = ast.newMethodDeclaration();// a() javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod1")); methodDeclaration.setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "void")); methodDeclaration.setBody(ast.newBlock()); methodDeclaration = ast.newMethodDeclaration();// a():int javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod2")); methodDeclaration.setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "int")); methodDeclaration.setBody(ast.newBlock()); methodDeclaration = ast.newMethodDeclaration();// a(par1:String):int javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod3")); methodDeclaration.setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "int")); methodDeclaration.setBody(ast.newBlock()); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setName(ast.newSimpleName("par1")); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "String")); methodDeclaration.parameters().add(variableDeclaration); methodDeclaration = ast.newMethodDeclaration();// a(par1:int, par2 :ClassB) :int javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod4")); methodDeclaration.setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "int")); methodDeclaration.setBody(ast.newBlock()); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setName(ast.newSimpleName("par1")); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "int")); methodDeclaration.parameters().add(variableDeclaration); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setName(ast.newSimpleName("par2")); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "ClassB")); methodDeclaration.parameters().add(variableDeclaration); methodDeclaration = ast.newMethodDeclaration(); javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod6")); methodDeclaration.setBody(ast.newBlock()); JavaSyncUtils.updateVisibilityFromModelToJavaClass(methodDeclaration, VisibilityKind.PUBLIC_LITERAL); methodDeclaration = ast.newMethodDeclaration(); javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod7")); methodDeclaration.setBody(ast.newBlock()); JavaSyncUtils.updateVisibilityFromModelToJavaClass(methodDeclaration, VisibilityKind.PROTECTED_LITERAL); methodDeclaration = ast.newMethodDeclaration(); javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod8")); methodDeclaration.setBody(ast.newBlock()); JavaSyncUtils.updateVisibilityFromModelToJavaClass(methodDeclaration, VisibilityKind.PRIVATE_LITERAL); methodDeclaration = ast.newMethodDeclaration(); javaClass.bodyDeclarations().add(methodDeclaration); methodDeclaration.setName(ast.newSimpleName("addedMethod9")); methodDeclaration.setBody(ast.newBlock()); JavaSyncUtils.updateVisibilityFromModelToJavaClass(methodDeclaration, VisibilityKind.PACKAGE_LITERAL); synchronizeAndCompareMasterClass(); }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaOperationModelAdapter.java
License:Open Source License
@Override public void setValueFeatureValue(Object element, Object feature, Object value) { if (CodeSyncPackage.eINSTANCE.getCodeSyncElement_Name().equals(feature)) { MethodDeclaration method = getMethodDeclaration(element); String name = (String) value; int index = name.indexOf("("); if (index == -1) { index = name.length();/*from w ww. ja v a 2 s . c o m*/ } name = name.substring(0, index); method.setName(method.getAST().newSimpleName(name)); } if (AstCacheCodePackage.eINSTANCE.getTypedElement_Type().equals(feature)) { MethodDeclaration method = getMethodDeclaration(element); Type type = getTypeFromString(method.getAST(), (String) value); method.setReturnType2(type); } super.setValueFeatureValue(element, feature, value); }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaMethod.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//www. j a va 2s .co m protected void setASTFeatureValue(EStructuralFeature feature, MethodDeclaration astElement, Object value) throws CodeSyncException { if (astElement == null) throw new IllegalArgumentException("astElement null "); AST ast = astElement.getAST(); switch (feature.getFeatureID()) { case UMLPackage.NAMED_ELEMENT__NAME: if (value == null) throw new IllegalArgumentException("setting name to null value "); astElement.setName(ast.newSimpleName((String) value)); break; case UMLPackage.OPERATION__TYPE: parentForwardJavaClass_OwnedMethods.parentForwardJavaType.parentForwardJavaSrcDir_Files .createImportDeclarationIfNeeded((Type) value); String modelReturnType = value == null ? null : ((Type) value).getName(); if (!astElement.isConstructor()) { TypeDeclaration parent = (TypeDeclaration) astElement.getParent(); if (value == null && astElement.getName().getIdentifier().equals(parent.getName().getIdentifier())) { // transform this method to constructor astElement.setConstructor(true); } else //if null value => return void type astElement.setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, modelReturnType, true)); } else if (value != null) { // transforming from constructor to ordinary method astElement.setConstructor(false); astElement.setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, modelReturnType, true)); } break; case UMLPackage.BEHAVIORAL_FEATURE__OWNED_PARAMETER: astElement.parameters().clear(); for (Parameter par : (List<Parameter>) value) if (par.getDirection().equals(ParameterDirectionKind.IN_LITERAL)) { parentForwardJavaClass_OwnedMethods.parentForwardJavaType.parentForwardJavaSrcDir_Files .createImportDeclarationIfNeeded(par.getType()); SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration(); String paramName = par.getName(); String paramType = par.getType() == null ? null : par.getType().getName(); if (paramName == null) throw new IllegalArgumentException("Parameter name is null: " + par); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, paramType, true)); try { variableDeclaration.setName(ast.newSimpleName(paramName)); } catch (IllegalArgumentException e) { throw new CodeSyncException("Invalid Parameter Name: \"" + paramName + "\" on java operation: " + astElement.getName() + "()", e); } astElement.parameters().add(variableDeclaration); } break; case UMLPackage.BEHAVIORAL_FEATURE__IS_ABSTRACT: JavaSyncUtils.updateModifierFromModelToJavaClass(astElement, (Boolean) value, JavaSyncUtils.MODIFIER_ABSTRACT); break; case UMLPackage.ELEMENT__EANNOTATIONS: List<EAnnotation> annotations = (List<EAnnotation>) value; for (EAnnotation annot : annotations) { // search for a template method annotation if (annot.getSource().equals("TemplateMethod") && annot.getDetails().containsKey("id")) { String bodyContent = JetTemplateFactory.INSTANCE .invokeOperationJetTemplate(annot.getEModelElement(), annot.getDetails().get("id")); if (annot.getDetails().containsKey("comment")) { // if it must contain also the template comment String commentLine = JetTemplateFactory.INSTANCE.invokeOperationJetTemplate( annot.getEModelElement(), annot.getDetails().get("comment")); JavaSyncUtils.generateBodyWithCommentForMethod(astElement, bodyContent, commentLine); } else { // add only content to method JavaSyncUtils.generateBodyForMethod(astElement, bodyContent); } // remove annotation; it doesn't need to be synchronized annotations.remove(annot); break; } } break; default: super.setASTFeatureValue(feature, astElement, value); } }
From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java
License:Open Source License
/** * Add a method stub, the body of which throws an assertion error, to a type. *//*w w w.j a va 2s.co m*/ @SuppressWarnings("unchecked") private void generateMethodStub(AST ast, ITypeBinding scope, List<BodyDeclaration> scopeBody, IMethodBinding method, Type returnType) { MethodDeclaration decl = ast.newMethodDeclaration(); decl.setName(ast.newSimpleName(method.getName())); // Return type decl.setReturnType2(returnType); // Generic type for (ITypeBinding typeParamBinding : method.getTypeParameters()) { TypeParameter typeParam = ast.newTypeParameter(); typeParam.setName(ast.newSimpleName(typeParamBinding.getName())); for (ITypeBinding typeBound : typeParamBinding.getTypeBounds()) { typeParam.typeBounds().add(createType(ast, scope, typeBound)); } decl.typeParameters().add(typeParam); } // Parameters int paramCount = 0; for (ITypeBinding paramBinding : method.getParameterTypes()) { SingleVariableDeclaration var = ast.newSingleVariableDeclaration(); // Binding doesn't track original parameter name; generate new parameter names. String paramName = "arg" + (paramCount++); var.setName(ast.newSimpleName(paramName)); var.setType(createType(ast, scope, paramBinding)); decl.parameters().add(var); } // Modifiers int modifiers = method.getModifiers(); // Always make the new method public. Even if this method overrides a // protected method, it might also need to implement an interface. decl.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); if (Modifier.isStrictfp(modifiers)) { decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STRICTFP_KEYWORD)); } if (Modifier.isSynchronized(modifiers)) { decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.SYNCHRONIZED_KEYWORD)); } // Body Block block = ast.newBlock(); decl.setBody(block); addAssertionError(block); // Add to type scopeBody.add(decl); generatedMethods.add(decl); }
From source file:com.google.devtools.j2cpp.translate.DestructorGenerator.java
License:Open Source License
private MethodDeclaration buildFinalizeMethod(AST ast, ITypeBinding declaringClass, List<IVariableBinding> fields) { ITypeBinding voidType = Types.mapTypeName("void"); GeneratedMethodBinding binding = new GeneratedMethodBinding(destructorName, Modifier.PUBLIC, voidType, declaringClass, false, false, true); MethodDeclaration method = ast.newMethodDeclaration(); Types.addBinding(method, binding); method.setName(ast.newSimpleName(destructorName)); Types.addBinding(method.getName(), binding); @SuppressWarnings("unchecked") List<Modifier> modifiers = method.modifiers(); modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); method.setBody(ast.newBlock());/* w w w. j a v a2s . c om*/ addReleaseStatements(method, fields); Type returnType = ast.newPrimitiveType(PrimitiveType.VOID); Types.addBinding(returnType, ast.resolveWellKnownType("void")); method.setReturnType2(returnType); return method; }