protected void copyReturnType(final ASTRewrite rewrite, final ICompilationUnit unit, final MethodDeclaration oldMethod, final MethodDeclaration newMethod, final TypeVariableMaplet[] mapping) throws JavaModelException { Type newReturnType= null; if (mapping.length > 0) newReturnType= createPlaceholderForType(oldMethod.getReturnType2(), unit, mapping, rewrite); else newReturnType= createPlaceholderForType(oldMethod.getReturnType2(), unit, rewrite); newMethod.setReturnType2(newReturnType); }
protected void copyReturnType(final ASTRewrite rewrite, final ICompilationUnit unit, final MethodDeclaration oldMethod, final MethodDeclaration newMethod, final TypeVariableMaplet[] mapping) throws JavaModelException { Type newReturnType= null; if (mapping.length > 0) newReturnType= createPlaceholderForType(oldMethod.getReturnType2(), unit, mapping, rewrite); else newReturnType= createPlaceholderForType(oldMethod.getReturnType2(), unit, rewrite); newMethod.setReturnType2(newReturnType); }
protected void copyReturnType(final ASTRewrite rewrite, final ICompilationUnit unit, final MethodDeclaration oldMethod, final MethodDeclaration newMethod, final TypeVariableMaplet[] mapping) throws JavaModelException { Type newReturnType= null; if (mapping.length > 0) newReturnType= createPlaceholderForType(oldMethod.getReturnType2(), unit, mapping, rewrite); else newReturnType= createPlaceholderForType(oldMethod.getReturnType2(), unit, rewrite); newMethod.setReturnType2(newReturnType); }
@Override public MethodSource<O> setReturnType(final String typeName) { String stub = "public class Stub { public " + typeName + " method() {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2(); returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType); method.setReturnType2(returnType); return this; }
/** * Sets the return type of the factory method, including any necessary type * arguments. E.g., for constructor <code>Foo()</code> in <code>Foo<T></code>, * the factory method defines a method type parameter <code><T></code> and * returns a <code>Foo<T></code>. * @param newMethod the method whose return type is to be set * @param retTypeName the simple name of the return type (without type parameters) * @param ctorOwnerTypeParameters the formal type parameters of the type that the * factory method instantiates (whose constructor is being encapsulated) * @param ast utility object used to create AST nodes */ private void setMethodReturnType(MethodDeclaration newMethod, String retTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) { if (ctorOwnerTypeParameters.length == 0) newMethod.setReturnType2(ast.newSimpleType(ast.newSimpleName(retTypeName))); else { Type baseType= ast.newSimpleType(ast.newSimpleName(retTypeName)); ParameterizedType newRetType= ast.newParameterizedType(baseType); List<Type> newRetTypeArgs= newRetType.typeArguments(); for(int i= 0; i < ctorOwnerTypeParameters.length; i++) { Type retTypeArg= ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName()); newRetTypeArgs.add(retTypeArg); } newMethod.setReturnType2(newRetType); } }
/** * Sets the return type of the factory method, including any necessary type * arguments. E.g., for constructor <code>Foo()</code> in <code>Foo<T></code>, * the factory method defines a method type parameter <code><T></code> and * returns a <code>Foo<T></code>. * @param newMethod the method whose return type is to be set * @param retTypeName the simple name of the return type (without type parameters) * @param ctorOwnerTypeParameters the formal type parameters of the type that the * factory method instantiates (whose constructor is being encapsulated) * @param ast utility object used to create AST nodes */ private void setMethodReturnType(MethodDeclaration newMethod, String retTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) { if (ctorOwnerTypeParameters.length == 0) newMethod.setReturnType2(ast.newSimpleType(ast.newSimpleName(retTypeName))); else { Type baseType= ast.newSimpleType(ast.newSimpleName(retTypeName)); ParameterizedType newRetType= ast.newParameterizedType(baseType); List<Type> newRetTypeArgs= newRetType.typeArguments(); for(int i= 0; i < ctorOwnerTypeParameters.length; i++) { Type retTypeArg= ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName()); newRetTypeArgs.add(retTypeArg); } newMethod.setReturnType2(newRetType); } }
/** * Sets the return type of the factory method, including any necessary type * arguments. E.g., for constructor <code>Foo()</code> in <code>Foo<T></code>, * the factory method defines a method type parameter <code><T></code> and * returns a <code>Foo<T></code>. * @param newMethod the method whose return type is to be set * @param retTypeName the simple name of the return type (without type parameters) * @param ctorOwnerTypeParameters the formal type parameters of the type that the * factory method instantiates (whose constructor is being encapsulated) * @param ast utility object used to create AST nodes */ private void setMethodReturnType(MethodDeclaration newMethod, String retTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) { if (ctorOwnerTypeParameters.length == 0) newMethod.setReturnType2(ast.newSimpleType(ast.newSimpleName(retTypeName))); else { Type baseType= ast.newSimpleType(ast.newSimpleName(retTypeName)); ParameterizedType newRetType= ast.newParameterizedType(baseType); List/*<Type>*/ newRetTypeArgs= newRetType.typeArguments(); for(int i= 0; i < ctorOwnerTypeParameters.length; i++) { Type retTypeArg= ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName()); newRetTypeArgs.add(retTypeArg); } newMethod.setReturnType2(newRetType); } }
/** * 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; }
/** * 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); }
/** * 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); }
@SuppressWarnings("unchecked") private MethodDeclaration createReturnLatestVersionService(final Map.Entry<String, VersionedService> latestEntry, final AST ast, final ConnectorType connectorType) { final String version = makeVersion(latestEntry.getValue().getVersion().toString()); final String serviceType = packageName(connectorType.getPackageName(), version, latestEntry.getValue().getType().getName()); final MethodDeclaration md = ast.newMethodDeclaration(); md.setName(ast.newSimpleName(asPropertyName(latestEntry.getKey()))); md.setReturnType2(ast.newSimpleType(ast.newName(serviceType))); md.modifiers().add(ast.newModifier(ModifierKeyword.DEFAULT_KEYWORD)); final Block block = ast.newBlock(); md.setBody(block); final ReturnStatement ret = ast.newReturnStatement(); block.statements().add(ret); final MethodInvocation versionMethod = ast.newMethodInvocation(); versionMethod.setName(ast.newSimpleName(version.toLowerCase())); final MethodInvocation serviceMethod = ast.newMethodInvocation(); serviceMethod.setName(ast.newSimpleName(asPropertyName(latestEntry.getKey()))); serviceMethod.setExpression(versionMethod); ret.setExpression(serviceMethod); return md; }
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; }
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 MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field = ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type = field.getType(); MethodDeclaration result = ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fGetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter); result.setReturnType2(returnType); Block block = ast.newBlock(); result.setBody(block); String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter); if (body != null) { body = body.substring(0, body.lastIndexOf(lineDelimiter)); ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK); block.statements().add(getterNode); } else { ReturnStatement rs = ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fField.getElementName())); block.statements().add(rs); } if (fGenerateJavadoc) { String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter); if (string != null) { Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }
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 }
@Override public MethodSource<O> setReturnType(final String typeName) { String simpleName = Types.toSimpleName(typeName); O origin = getOrigin(); if (!hasTypeVariable(typeName) && !Strings.areEqual(typeName, simpleName) && origin.requiresImport(typeName)) { origin.addImport(typeName); } for (String genericType : Types.splitGenerics(typeName)) { if (!hasTypeVariable(genericType) && origin.requiresImport(genericType)) { origin.addImport(genericType); } } String stub = "public class Stub { public " + simpleName + " method() {} }"; JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); List<MethodSource<JavaClassSource>> methods = temp.getMethods(); org.eclipse.jdt.core.dom.Type returnType = ((MethodDeclaration) methods.get(0).getInternal()).getReturnType2(); returnType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(method.getAST(), returnType); method.setReturnType2(returnType); return this; }
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 MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException { FieldDeclaration field= (FieldDeclaration)ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class); Type type= field.getType(); MethodDeclaration result= ast.newMethodDeclaration(); result.setName(ast.newSimpleName(fGetterName)); result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers())); result.setReturnType2((Type)rewriter.createCopyTarget(type)); Block block= ast.newBlock(); result.setBody(block); ReturnStatement rs= ast.newReturnStatement(); rs.setExpression(ast.newSimpleName(fField.getElementName())); block.statements().add(rs); if (fGenerateJavadoc) { String string= CodeGeneration.getGetterComment( fField.getCompilationUnit() , getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), NamingConventions.removePrefixAndSuffixForFieldName(fField.getJavaProject(), fField.getElementName(), fField.getFlags()), lineDelimiter); if (string != null) { Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC); result.setJavadoc(javadoc); } } return result; }