List of usage examples for org.eclipse.jdt.core.dom AST newSingleVariableDeclaration
public SingleVariableDeclaration newSingleVariableDeclaration()
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/*w w w . j a v a 2s .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:br.com.objectos.way.core.code.jdt.ASTTest.java
License:Apache License
@SuppressWarnings("unchecked") public void stackoverflow_answer() { AST ast = AST.newAST(AST.JLS8); CompilationUnit cu = ast.newCompilationUnit(); PackageDeclaration p1 = ast.newPackageDeclaration(); p1.setName(ast.newSimpleName("foo")); cu.setPackage(p1);/*from www .j a v a 2s . co m*/ ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] { "java", "util", "Set" })); cu.imports().add(id); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Foo")); TypeParameter tp = ast.newTypeParameter(); tp.setName(ast.newSimpleName("X")); td.typeParameters().add(tp); cu.types().add(td); MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); md.setName(ast.newSimpleName("bar")); SingleVariableDeclaration var = ast.newSingleVariableDeclaration(); var.setType(ast.newSimpleType(ast.newSimpleName("String"))); var.setName(ast.newSimpleName("a")); md.parameters().add(var); td.bodyDeclarations().add(md); Block block = ast.newBlock(); md.setBody(block); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("x")); ExpressionStatement e = ast.newExpressionStatement(mi); block.statements().add(e); System.out.println(cu); }
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 w w . ja v a2 s.c o 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_Modifiers() throws IOException { 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(); // changing visibilities JavaSyncUtils.updateVisibilityFromModelToJavaClass(getMethod(javaClass, "myMethod1"), VisibilityKind.PROTECTED_LITERAL); JavaSyncUtils.updateVisibilityFromModelToJavaClass(getMethod(javaClass, "myMethod2"), VisibilityKind.PRIVATE_LITERAL); JavaSyncUtils.updateVisibilityFromModelToJavaClass(getMethod(javaClass, "myMethod3"), VisibilityKind.PACKAGE_LITERAL); JavaSyncUtils.updateVisibilityFromModelToJavaClass(getMethod(javaClass, "myMethod4"), VisibilityKind.PUBLIC_LITERAL); // renaming// w w w .ja v a 2 s. c o m getMethod(javaClass, "myMethod5").setName(ast.newSimpleName("myMethod5_Renamed")); // circular setting return type getMethod(javaClass, "myMethod6").setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "int")); getMethod(javaClass, "myMethod7").setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "ClassB")); getMethod(javaClass, "myMethod8").setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "ClassA")); getMethod(javaClass, "myMethod9").setReturnType2(JavaSyncUtils.getJavaTypeFromString(ast, "void")); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setName(ast.newSimpleName("par1")); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "ClassA")); getMethod(javaClass, "myMethod10").parameters().add(variableDeclaration); variableDeclaration = ast.newSingleVariableDeclaration(); variableDeclaration.setName(ast.newSimpleName("par2")); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "ClassB")); getMethod(javaClass, "myMethod11").parameters().add(variableDeclaration); methodDeclaration = getMethod(javaClass, "myMethod12"); methodDeclaration.parameters().remove(methodDeclaration.parameters().get(0));// remove par1 methodDeclaration = getMethod(javaClass, "myMethod13"); methodDeclaration.parameters().remove(methodDeclaration.parameters().get(1));// remove par2 methodDeclaration = getMethod(javaClass, "myMethod15"); variableDeclaration = (SingleVariableDeclaration) methodDeclaration.parameters().get(0); variableDeclaration.setName(ast.newSimpleName("par1_Renamed")); variableDeclaration = (SingleVariableDeclaration) methodDeclaration.parameters().get(1); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "String")); variableDeclaration = (SingleVariableDeclaration) methodDeclaration.parameters().get(2); variableDeclaration.setName(ast.newSimpleName("par3_Renamed")); variableDeclaration.setType(JavaSyncUtils.getJavaTypeFromString(ast, "ClassB")); synchronizeAndCompareMasterClass(); }
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(); 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();// w w w. j a v a 2 s . co m }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaOperationModelAdapter.java
License:Open Source License
@Override public Object createChildOnContainmentFeature(Object element, Object feature, Object correspondingChild) { if (AstCacheCodePackage.eINSTANCE.getOperation_Parameters().equals(feature)) { MethodDeclaration method = (MethodDeclaration) element; AST ast = method.getAST(); SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration(); method.parameters().add(parameter); return parameter; }/*from w ww.j ava 2 s. c o m*/ return super.createChildOnContainmentFeature(element, feature, correspondingChild); }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaMethod.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w .j a v a 2s .c om 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.AnonymousClassConverter.java
License:Open Source License
private void addArguments(List<Expression> invocationArguments, AST ast, ITypeBinding clazz, MethodDeclaration constructor, GeneratedMethodBinding binding) { // Create a parameter list, based on the invocation arguments. @SuppressWarnings("unchecked") // safe by definition List<SingleVariableDeclaration> parameters = constructor.parameters(); int parameterOffset = binding.getParameterTypes().length; for (int i = 0; i < invocationArguments.size(); i++) { Expression arg = invocationArguments.get(i); ITypeBinding argType = Types.getTypeBinding(arg); SimpleName paramName = ast.newSimpleName("arg$" + i); GeneratedVariableBinding paramBinding = new GeneratedVariableBinding(paramName.getIdentifier(), 0, argType, false, true, clazz, null); Types.addBinding(paramName, paramBinding); SingleVariableDeclaration param = ast.newSingleVariableDeclaration(); param.setName(paramName);/* w ww .j a v a 2s . c om*/ param.setType(Types.makeType(argType)); Types.addBinding(param, paramBinding); parameters.add(param); binding.addParameter(i + parameterOffset, argType); } // Add super constructor call, forwarding the invocation arguments. SuperConstructorInvocation superInvocation = ast.newSuperConstructorInvocation(); @SuppressWarnings("unchecked") List<Expression> superArgs = superInvocation.arguments(); // safe by definition for (SingleVariableDeclaration param : parameters) { superArgs.add(NodeCopier.copySubtree(ast, param.getName())); } Types.addBinding(superInvocation, findSuperConstructorBinding(clazz.getSuperclass(), invocationArguments)); @SuppressWarnings("unchecked") List<Statement> statements = constructor.getBody().statements(); // safe by definition statements.add(superInvocation); }
From source file:com.google.devtools.j2cpp.translate.ClassConverter.java
License:Open Source License
@SuppressWarnings("unchecked") protected List<SingleVariableDeclaration> createConstructorArguments(List<IVariableBinding> innerFields, IMethodBinding constructor, AST ast, String prefix) { int nameOffset = constructor.getParameterTypes().length; List<SingleVariableDeclaration> args = Lists.newArrayList(); for (int i = 0; i < innerFields.size(); i++) { IVariableBinding field = innerFields.get(i); String argName = prefix + (i + nameOffset); SimpleName name = ast.newSimpleName(argName); GeneratedVariableBinding binding = new GeneratedVariableBinding(argName, Modifier.FINAL, field.getType(), false, true, constructor.getDeclaringClass(), constructor); Types.addBinding(name, binding); SingleVariableDeclaration newArg = ast.newSingleVariableDeclaration(); newArg.setName(name);/*from w ww . j ava2 s .co m*/ newArg.setType(Types.makeType(field.getType())); newArg.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD)); Types.addBinding(newArg, binding); args.add(newArg); } return args; }
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. *///from w ww. ja v a 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); }