cu.types().add(td); MethodDeclaration md = ast.newMethodDeclaration(); td.bodyDeclarations().add(md);
public MethodImpl(final O parent) { init(parent); method = ast.newMethodDeclaration(); method.setConstructor(false); }
public MethodImpl(final O parent) { init(parent); method = ast.newMethodDeclaration(); method.setConstructor(false); }
/** * Add constructor declaration. * * @param name simple class name * @return constructor builder */ public MethodBuilder addConstructor(String name) { MethodDeclaration constr = getAST().newMethodDeclaration(); constr.setName(getAST().newSimpleName(name)); constr.setConstructor(true); m_methods.add(constr); return new MethodBuilder(this, constr); }
@SuppressWarnings("unchecked") private void generateConstructorDefault(Classifier clazz, AST ast, TypeDeclaration td) { MethodDeclaration constructor = ast.newMethodDeclaration(); constructor.setConstructor(true); constructor.setName(ast.newSimpleName(clazz.getName())); constructor.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); Block block = ast.newBlock(); constructor.setBody(block); td.bodyDeclarations().add(constructor); }
/** * Add method declaration. * * @param name Method name * @param type Method type * @return method builder */ public MethodBuilder addMethod(String name, Type type) { MethodDeclaration meth = getAST().newMethodDeclaration(); meth.setName(getAST().newSimpleName(name)); meth.setConstructor(false); meth.setReturnType2(type); if (m_class instanceof AnonymousClassDeclaration) { ((AnonymousClassDeclaration)m_class).bodyDeclarations().add(meth); } else { m_methods.add(meth); } return new MethodBuilder(this, meth); }
public MethodDeclaration createGetter(String fieldName, Type fieldType) { String shiftedName = StringUtils.capitalize(fieldName); Type typeCopy = (Type) ASTNode.copySubtree(ast, fieldType); MethodDeclaration getter = ast.newMethodDeclaration(); getter.setName(ast.newSimpleName("get" + shiftedName)); getter.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); getter.setReturnType2(typeCopy); // body Block body = ast.newBlock(); getter.setBody(body); ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fieldName)); body.statements().add(rs); return getter; }
private CAstEntity createDefaultConstructor(ITypeBinding classBinding, WalkContext oldContext, ArrayList<ASTNode> inits, ASTNode positioningNode) { MethodDeclaration fakeCtor = ast.newMethodDeclaration(); fakeCtor.setConstructor(true); // fakeCtor.setName(ast.newSimpleName(className)); will crash on anonymous types... fakeCtor.setSourceRange(positioningNode.getStartPosition(), positioningNode.getLength()); fakeCtor.setBody(ast.newBlock()); return visit(fakeCtor, classBinding, oldContext, inits); }
/** * This method initializes all variables used in the process of generating <code>toString</code> * method. */ protected void initialize() { needMaxLenVariable= false; needCollectionToStringMethod= false; typesThatNeedArrayToStringMethod= new ArrayList<>(); checkNeedForHelperMethods(); toStringMethod= fAst.newMethodDeclaration(); toStringMethod.modifiers().addAll(ASTNodeFactory.newModifiers(fAst, Modifier.PUBLIC)); toStringMethod.setName(fAst.newSimpleName(METHODNAME_TO_STRING)); toStringMethod.setConstructor(false); toStringMethod.setReturnType2(fAst.newSimpleType(fAst.newName(TYPENAME_STRING))); Block body= fAst.newBlock(); toStringMethod.setBody(body); fMaxLenVariableName= createNameSuggestion(MAX_LEN_VARIABLE_NAME, NamingConventions.VK_LOCAL); }
@Test public void testCreateReturnType() { Map<String, String> javaTypes = createJavaTypes(); when(dataTypeUtils.getJavaTypes()).thenReturn(javaTypes); TypeDeclaration td = ast.newTypeDeclaration(); MethodDeclaration md = ast.newMethodDeclaration(); String umlTypeName = "Collection"; String umlQualifiedTypeName = "Validation Profile::OCL Library::Collection"; String sourceDirectoryPackageName = "Data"; jdtHelper.createReturnType(ast, td, md, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName); assertEquals("java.util.Collection", md.getReturnType2().toString()); }
@Test public void testCreateReturnTypeAsCollectionResultCollection() { Map<String, String> javaTypes = createJavaTypes(); when(dataTypeUtils.getJavaTypes()).thenReturn(javaTypes); TypeDeclaration td = ast.newTypeDeclaration(); MethodDeclaration md = ast.newMethodDeclaration(); String umlTypeName = "String"; String umlQualifiedTypeName = "Validation Profile::OCL Library::String"; String sourceDirectoryPackageName = "Data"; jdtHelper.createReturnTypeAsCollection(ast, td, md, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName, JdtHelper.JAVA_UTIL_COLLECTION); assertEquals("java.util.Collection<String>", md.getReturnType2() .toString()); }
private MethodDeclaration createGetEnclosingInstanceHelper() { String enclosingTypeName= fType.getDeclaringClass().getTypeDeclaration().getName(); MethodDeclaration helperMethod= fAst.newMethodDeclaration(); helperMethod.modifiers().addAll(ASTNodeFactory.newModifiers(fAst, Modifier.PRIVATE)); helperMethod.setName(fAst.newSimpleName(METHODNAME_GET_ENCLOSING_INSTANCE)); helperMethod.setConstructor(false); helperMethod.setReturnType2(fAst.newSimpleType(fAst.newSimpleName(enclosingTypeName))); Block body= fAst.newBlock(); helperMethod.setBody(body); ThisExpression thisExpression= fAst.newThisExpression(); thisExpression.setQualifier(fAst.newSimpleName(enclosingTypeName)); ReturnStatement endReturn= fAst.newReturnStatement(); endReturn.setExpression(thisExpression); body.statements().add(endReturn); return helperMethod; }
private MethodDeclaration createGetOuterHelper() { String outerTypeName= fType.getDeclaringClass().getTypeDeclaration().getName(); MethodDeclaration helperMethod= fAst.newMethodDeclaration(); helperMethod.modifiers().addAll(ASTNodeFactory.newModifiers(fAst, Modifier.PRIVATE)); helperMethod.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); helperMethod.setConstructor(false); helperMethod.setReturnType2(fAst.newSimpleType(fAst.newSimpleName(outerTypeName))); Block body= fAst.newBlock(); helperMethod.setBody(body); ThisExpression thisExpression= fAst.newThisExpression(); thisExpression.setQualifier(fAst.newSimpleName(outerTypeName)); ReturnStatement endReturn= fAst.newReturnStatement(); endReturn.setExpression(thisExpression); body.statements().add(endReturn); return helperMethod; }
@Test public void testCreateReturnTypeAsCollectionResultList() { Map<String, String> javaTypes = createJavaTypes(); when(dataTypeUtils.getJavaTypes()).thenReturn(javaTypes); TypeDeclaration td = ast.newTypeDeclaration(); MethodDeclaration md = ast.newMethodDeclaration(); String umlTypeName = "String"; String umlQualifiedTypeName = "Validation Profile::OCL Library::String"; String sourceDirectoryPackageName = "Data"; jdtHelper.createReturnTypeAsCollection(ast, td, md, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName, JdtHelper.JAVA_UTIL_LIST); assertEquals("java.util.List<String>", md.getReturnType2().toString()); }
@Test public void testCreateReturnTypeAsCollectionResultSet() { Map<String, String> javaTypes = createJavaTypes(); when(dataTypeUtils.getJavaTypes()).thenReturn(javaTypes); TypeDeclaration td = ast.newTypeDeclaration(); MethodDeclaration md = ast.newMethodDeclaration(); String umlTypeName = "String"; String umlQualifiedTypeName = "Validation Profile::OCL Library::String"; String sourceDirectoryPackageName = "Data"; jdtHelper.createReturnTypeAsCollection(ast, td, md, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName, JdtHelper.JAVA_UTIL_SET); assertEquals("java.util.Set<String>", md.getReturnType2().toString()); }
@Test public void testCreateReturnTypeAsCollectionResultSortedSet() { Map<String, String> javaTypes = createJavaTypes(); when(dataTypeUtils.getJavaTypes()).thenReturn(javaTypes); TypeDeclaration td = ast.newTypeDeclaration(); MethodDeclaration md = ast.newMethodDeclaration(); String umlTypeName = "String"; String umlQualifiedTypeName = "Validation Profile::OCL Library::String"; String sourceDirectoryPackageName = "Data"; jdtHelper.createReturnTypeAsCollection(ast, td, md, umlTypeName, umlQualifiedTypeName, sourceDirectoryPackageName, JdtHelper.JAVA_UTIL_SORTEDSET); assertEquals("java.util.SortedSet<String>", md.getReturnType2() .toString()); }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setExtraDimensions(0); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
private void addMethodStubForAbstractMethod(final IMethod sourceMethod, final CompilationUnit declaringCuNode, final AbstractTypeDeclaration typeToCreateStubIn, final ICompilationUnit newCu, final CompilationUnitRewrite rewriter, final Map adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException { final MethodDeclaration methodToCreateStubFor= ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, declaringCuNode); final AST ast= rewriter.getRoot().getAST(); final MethodDeclaration newMethod= ast.newMethodDeclaration(); newMethod.setBody(createMethodStub(methodToCreateStubFor, ast)); newMethod.setConstructor(false); newMethod.setExtraDimensions(methodToCreateStubFor.getExtraDimensions()); newMethod.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiersWithUpdatedVisibility(sourceMethod, JdtFlags.clearFlag(Modifier.NATIVE | Modifier.ABSTRACT, methodToCreateStubFor.getModifiers()), adjustments, new SubProgressMonitor(monitor, 1), false, status))); newMethod.setName(((SimpleName) ASTNode.copySubtree(ast, methodToCreateStubFor.getName()))); final TypeVariableMaplet[] mapping= TypeVariableUtil.composeMappings(TypeVariableUtil.subTypeToSuperType(getDeclaringType(), getDestinationType()), TypeVariableUtil.superTypeToInheritedType(getDestinationType(), ((IType) typeToCreateStubIn.resolveBinding().getJavaElement()))); copyReturnType(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping); copyParameters(rewriter.getASTRewrite(), getDeclaringType().getCompilationUnit(), methodToCreateStubFor, newMethod, mapping); copyThrownExceptions(methodToCreateStubFor, newMethod); newMethod.setJavadoc(createJavadocForStub(typeToCreateStubIn.getName().getIdentifier(), methodToCreateStubFor, newMethod, newCu, rewriter.getASTRewrite())); ImportRewriteUtil.addImports(rewriter, newMethod, new HashMap(), new HashMap(), false); rewriter.getASTRewrite().getListRewrite(typeToCreateStubIn, typeToCreateStubIn.getBodyDeclarationsProperty()).insertAt(newMethod, ASTNodes.getInsertionIndex(newMethod, typeToCreateStubIn.bodyDeclarations()), rewriter.createCategorizedGroupDescription(RefactoringCoreMessages.PullUpRefactoring_add_method_stub, SET_PULL_UP)); }