Java Code Examples for org.eclipse.jdt.core.dom.TypeParameter#setName()

The following examples show how to use org.eclipse.jdt.core.dom.TypeParameter#setName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewCUProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void addTypeParameters(TypeDeclaration newDeclaration) {
	if (isParameterizedType(fTypeKind, fNode)) {
		String typeArgBaseName = getGenericTypeArgBaseName(ASTNodes.getSimpleNameIdentifier(fNode));
		int nTypeArgs = ((ParameterizedType) fNode.getParent().getParent()).typeArguments().size();
		String[] typeArgNames = new String[nTypeArgs];
		if (nTypeArgs == 1) {
			typeArgNames[0] = typeArgBaseName;
		} else {
			for (int i = 0; i < nTypeArgs; i++) {
				StringBuilder buf = new StringBuilder(typeArgBaseName);
				buf.append(i + 1);
				typeArgNames[i] = buf.toString();
			}
		}

		AST ast = newDeclaration.getAST();
		for (String typeArgName : typeArgNames) {
			TypeParameter typeArg = ast.newTypeParameter();
			typeArg.setName(ast.newSimpleName(typeArgName));
			newDeclaration.typeParameters().add(typeArg);
		}
	}

}
 
Example 2
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void addEnclosingInstanceTypeParameters(final ITypeBinding[] parameters, final AbstractTypeDeclaration declaration, final ASTRewrite rewrite) {
	Assert.isNotNull(parameters);
	Assert.isNotNull(declaration);
	Assert.isNotNull(rewrite);
	if (declaration instanceof TypeDeclaration) {
		final TypeDeclaration type= (TypeDeclaration) declaration;
		final List<TypeParameter> existing= type.typeParameters();
		final Set<String> names= new HashSet<String>();
		TypeParameter parameter= null;
		for (final Iterator<TypeParameter> iterator= existing.iterator(); iterator.hasNext();) {
			parameter= iterator.next();
			names.add(parameter.getName().getIdentifier());
		}
		final ListRewrite rewriter= rewrite.getListRewrite(type, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
		String name= null;
		for (int index= 0; index < parameters.length; index++) {
			name= parameters[index].getName();
			if (!names.contains(name)) {
				parameter= type.getAST().newTypeParameter();
				parameter.setName(type.getAST().newSimpleName(name));
				rewriter.insertLast(parameter, null);
			}
		}
	}
}
 
Example 3
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void addTypeParameters(CompilationUnitRewrite imRewrite, List<TypeParameter> list, ITypeBinding parent) {

		ITypeBinding enclosing= parent.getDeclaringClass();
		if (enclosing != null)
			addTypeParameters(imRewrite, list, enclosing);

		ITypeBinding[] typeParameters= parent.getTypeParameters();
		for (int i= 0; i < typeParameters.length; i++) {
			TypeParameter ntp= imRewrite.getAST().newTypeParameter();
			ntp.setName(imRewrite.getAST().newSimpleName(typeParameters[i].getName()));
			ITypeBinding[] bounds= typeParameters[i].getTypeBounds();
			for (int j= 0; j < bounds.length; j++)
				if (!"java.lang.Object".equals(bounds[j].getQualifiedName())) //$NON-NLS-1$
					ntp.typeBounds().add(imRewrite.getImportRewrite().addImport(bounds[j], imRewrite.getAST()));
			list.add(ntp);
		}
	}
 
Example 4
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Copies the constructor's parent type's type parameters, if any, as
 * method type parameters of the new static factory method. (Recall
 * that static methods can't refer to type arguments of the enclosing
 * class, since they have no instance to serve as a context.)<br>
 * Makes sure to copy the bounds from the owning type, to ensure that the
 * return type of the factory method satisfies the bounds of the type
 * being instantiated.<br>
 * E.g., for ctor Foo() in the type Foo<T extends Number>, be sure that
 * the factory method is declared as<br>
 * <code>static <T extends Number> Foo<T> createFoo()</code><br>
 * and not simply<br>
 * <code>static <T> Foo<T> createFoo()</code><br>
 * or the compiler will bark.
 * @param ast utility object needed to create ASTNode's for the new method
 * @param newMethod the method onto which to copy the type parameters
 */
private void copyTypeParameters(AST ast, MethodDeclaration newMethod) {
	ITypeBinding[] ctorOwnerTypeParms= fCtorBinding.getDeclaringClass().getTypeParameters();
	List<TypeParameter> factoryMethodTypeParms= newMethod.typeParameters();
	for(int i= 0; i < ctorOwnerTypeParms.length; i++) {
           TypeParameter newParm= ast.newTypeParameter();
           ITypeBinding[] parmTypeBounds= ctorOwnerTypeParms[i].getTypeBounds();
           List<Type> newParmBounds= newParm.typeBounds();

           newParm.setName(ast.newSimpleName(ctorOwnerTypeParms[i].getName()));
           for(int b=0; b < parmTypeBounds.length; b++) {
           	if (parmTypeBounds[b].isClass() && parmTypeBounds[b].getSuperclass() == null)
           		continue;

           	Type newBound= fImportRewriter.addImport(parmTypeBounds[b], ast);

               newParmBounds.add(newBound);
           }
		factoryMethodTypeParms.add(newParm);
	}
}
 
Example 5
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void createTypeParameters(ImportRewrite imports, ImportRewriteContext context, AST ast, IMethodBinding binding, MethodDeclaration decl) {
	ITypeBinding[] typeParams= binding.getTypeParameters();
	List<TypeParameter> typeParameters= decl.typeParameters();
	for (int i= 0; i < typeParams.length; i++) {
		ITypeBinding curr= typeParams[i];
		TypeParameter newTypeParam= ast.newTypeParameter();
		newTypeParam.setName(ast.newSimpleName(curr.getName()));
		ITypeBinding[] typeBounds= curr.getTypeBounds();
		if (typeBounds.length != 1 || !"java.lang.Object".equals(typeBounds[0].getQualifiedName())) {//$NON-NLS-1$
			List<Type> newTypeBounds= newTypeParam.typeBounds();
			for (int k= 0; k < typeBounds.length; k++) {
				newTypeBounds.add(imports.addImport(typeBounds[k], ast, context));
			}
		}
		typeParameters.add(newTypeParam);
	}
}
 
Example 6
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void updateConstructorReference(ITypeBinding[] parameters, ParameterizedType type, CompilationUnitRewrite targetRewrite, ICompilationUnit cu, TextEditGroup group) throws CoreException {
	final ListRewrite rewrite= targetRewrite.getASTRewrite().getListRewrite(type, ParameterizedType.TYPE_ARGUMENTS_PROPERTY);
	TypeParameter parameter= null;
	for (int index= type.typeArguments().size(); index < parameters.length; index++) {
		parameter= targetRewrite.getRoot().getAST().newTypeParameter();
		parameter.setName(targetRewrite.getRoot().getAST().newSimpleName(parameters[index].getName()));
		rewrite.insertLast(parameter, group);
	}
	if (type.getParent() instanceof ClassInstanceCreation)
		updateConstructorReference((ClassInstanceCreation) type.getParent(), targetRewrite, cu, group);
}
 
Example 7
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void copyTypeParameters(MethodDeclaration intermediary, CompilationUnitRewrite rew) {
	ITypeBinding[] typeParameters= fTargetMethodBinding.getTypeParameters();
	for (int i= 0; i < typeParameters.length; i++) {
		ITypeBinding current= typeParameters[i];

		TypeParameter parameter= rew.getAST().newTypeParameter();
		parameter.setName(rew.getAST().newSimpleName(current.getName()));
		ITypeBinding[] bounds= current.getTypeBounds();
		for (int j= 0; j < bounds.length; j++)
			if (!"java.lang.Object".equals(bounds[j].getQualifiedName())) //$NON-NLS-1$
				parameter.typeBounds().add(rew.getImportRewrite().addImport(bounds[j], rew.getAST()));

		intermediary.typeParameters().add(parameter);
	}
}
 
Example 8
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
	final AST ast= fAnonymousInnerClassNode.getAST();

	final TypeDeclaration newDeclaration= ast.newTypeDeclaration();
	newDeclaration.setInterface(false);
	newDeclaration.setJavadoc(null);
	newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
	newDeclaration.setName(ast.newSimpleName(fClassName));

	TypeParameter parameter= null;
	for (int index= 0; index < typeParameters.length; index++) {
		parameter= ast.newTypeParameter();
		parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
		newDeclaration.typeParameters().add(parameter);
	}
	setSuperType(newDeclaration);

	IJavaProject project= fCu.getJavaProject();

	IVariableBinding[] bindings= getUsedLocalVariables();
	ArrayList<String> fieldNames= new ArrayList<String>();
	for (int i= 0; i < bindings.length; i++) {
		String name= StubUtility.getBaseName(bindings[i], project);
		String[] fieldNameProposals= StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
		fieldNames.add(fieldNameProposals[0]);


		if (fLinkedProposalModel != null) {
			LinkedProposalPositionGroup positionGroup= fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
			for (int k= 0; k < fieldNameProposals.length; k++) {
				positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
			}
		}
	}
	String[] allFieldNames= fieldNames.toArray(new String[fieldNames.size()]);

	List<BodyDeclaration> newBodyDeclarations= newDeclaration.bodyDeclarations();

	createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);

	MethodDeclaration newConstructorDecl= createNewConstructor(rewrite, bindings, allFieldNames);
	if (newConstructorDecl != null) {
		newBodyDeclarations.add(newConstructorDecl);
	}

	updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);

	if (doAddComments()) {
		String[] parameterNames= new String[typeParameters.length];
		for (int index= 0; index < parameterNames.length; index++) {
			parameterNames[index]= typeParameters[index].getName();
		}
		String string= CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
			newDeclaration.setJavadoc(javadoc);
		}
	}
	if (fLinkedProposalModel != null) {
		addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
		ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
	}

	return newDeclaration;
}