Example usage for org.eclipse.jdt.core.dom TypeParameter setName

List of usage examples for org.eclipse.jdt.core.dom TypeParameter setName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom TypeParameter setName.

Prototype

public void setName(SimpleName typeName) 

Source Link

Document

Sets the name of the type variable of this type parameter to the given name.

Usage

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);//from   ww w .  j  a  va 2  s.c  o  m
    CompilationUnit cu = ast.newCompilationUnit();

    PackageDeclaration p1 = ast.newPackageDeclaration();
    p1.setName(ast.newSimpleName("foo"));
    cu.setPackage(p1);

    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: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. j  ava  2 s.c  o 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:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGenerator.java

License:Apache License

/**
 * Generate the Generics for this Interface.
 * //from   w w w  .j av  a 2 s  .c  om
 * @param clazz
 *            the UML class
 * @param ast
 *            the JDT Java AST
 * @param td
 *            TypeDeclaration JDT
 */
@SuppressWarnings("unchecked")
public void generateClassTemplateParams(Classifier clazz, AST ast, TypeDeclaration td) {
    TemplateSignature templateSignature = clazz.getOwnedTemplateSignature();
    if (templateSignature != null) {
        EList<TemplateParameter> templateParameters = templateSignature.getParameters();
        for (TemplateParameter templateParameter : templateParameters) {
            Classifier classifier = (Classifier) templateParameter.getOwnedParameteredElement();
            String typeName = classifier.getLabel();
            TypeParameter typeParameter = ast.newTypeParameter();
            typeParameter.setName(ast.newSimpleName(typeName));
            td.typeParameters().add(typeParameter);
        }
    }
}

From source file:de.crowdcode.kissmda.cartridges.simplejava.InterfaceGenerator.java

License:Apache License

/**
 * Generate the template parameter for the given method - Generic Method.
 * //from  w w w.  j  av a 2s . co m
 * @param ast
 *            AST tree JDT
 * @param operation
 *            UML2 Operation
 * @param md
 *            MethodDeclaration JDT
 */
@SuppressWarnings("unchecked")
public void generateMethodTemplateParams(AST ast, Operation operation, MethodDeclaration md) {
    TemplateSignature templateSignature = operation.getOwnedTemplateSignature();
    if (templateSignature != null) {
        EList<TemplateParameter> templateParameters = templateSignature.getParameters();
        for (TemplateParameter templateParameter : templateParameters) {
            Classifier classifier = (Classifier) templateParameter.getOwnedParameteredElement();
            String typeName = classifier.getLabel();
            TypeParameter typeParameter = ast.newTypeParameter();
            typeParameter.setName(ast.newSimpleName(typeName));
            md.typeParameters().add(typeParameter);
        }
    }
}

From source file:de.dentrassi.varlink.generator.JdtGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void createImplFactory(final AST ast, final TypeDeclaration td) {

    final TypeDeclaration ftd = ast.newTypeDeclaration();
    td.bodyDeclarations().add(ftd);//from w ww . ja  va2  s  . co  m
    ftd.setName(ast.newSimpleName("Factory"));
    make(ftd, PUBLIC_KEYWORD, STATIC_KEYWORD);

    ftd.superInterfaceTypes().add(ast.newSimpleType(ast.newName("de.dentrassi.varlink.spi.Factory")));

    /*
     *
     * @Override public <T> T create(final VarlinkImpl varlink, final Class<T>
     * clazz, final Connection connection)
     */

    final MethodDeclaration md = ast.newMethodDeclaration();
    ftd.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("create"));
    md.setReturnType2(ast.newSimpleType(ast.newName("T")));
    make(md, PUBLIC_KEYWORD);
    addSimpleAnnotation(md, "Override");

    final TypeParameter tp = ast.newTypeParameter();
    tp.setName(ast.newSimpleName("T"));
    md.typeParameters().add(tp);

    final ParameterizedType clazz = ast.newParameterizedType(ast.newSimpleType(ast.newName("Class")));

    clazz.typeArguments().add(ast.newSimpleType(ast.newName("T")));

    createParameter(md, "de.dentrassi.varlink.internal.VarlinkImpl", "varlink", FINAL_KEYWORD);
    createParameter(md, clazz, "clazz", FINAL_KEYWORD);
    createParameter(md, "de.dentrassi.varlink.spi.Connection", "connection", FINAL_KEYWORD);

    final Block body = ast.newBlock();
    md.setBody(body);

    // return clazz.cast(new Impl(varlink,connection));

    final ReturnStatement ret = ast.newReturnStatement();
    body.statements().add(ret);

    final MethodInvocation cast = ast.newMethodInvocation();
    cast.setName(ast.newSimpleName("cast"));
    cast.setExpression(ast.newSimpleName("clazz"));

    ret.setExpression(cast);

    final ClassInstanceCreation newImpl = ast.newClassInstanceCreation();
    newImpl.setType(ast.newSimpleType(ast.newName(td.getName().getIdentifier())));
    cast.arguments().add(newImpl);

    newImpl.arguments().add(ast.newSimpleName("connection"));
    newImpl.arguments().add(ast.newSimpleName("varlink"));
}

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.TypeParameter node) {
    TypeParameter element = (TypeParameter) this.binding.get(node);
    this.initializeNode(element, node);
    element.setName(node.getName().getIdentifier());
    for (Iterator<?> i = node.typeBounds().iterator(); i.hasNext();) {
        NamedElementRef itElement = (NamedElementRef) this.binding.get(i.next());
        if (itElement != null)
            element.getBounds().add(itElement);
    }//from ww  w .  j a  v  a 2s  .  c o m
    JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this);
}

From source file:nz.ac.massey.cs.care.refactoring.executers.IntroduceFactoryRefactoring.java

License:Open Source License

/**
 * 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>//from  w  w  w.  jav  a2 s.com
 * 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);
    }
}

From source file:org.decojer.cavaj.transformers.TrOutline.java

License:Open Source License

/**
 * Decompile Type Parameters./*from  www  .  j a v  a  2  s .c  o  m*/
 *
 * @param typeParams
 *            Type Parameters
 * @param typeParameters
 *            AST Type Parameters
 * @param contextT
 *            Type Declaration
 */
private static void decompileTypeParams(@Nonnull final T[] typeParams, final List<TypeParameter> typeParameters,
        @Nonnull final T contextT) {
    final AST ast = contextT.getCu().getAst();
    for (final T typeParam : typeParams) {
        final TypeParameter typeParameter = ast.newTypeParameter();
        typeParameter.setName(newSimpleName(typeParam.getName(), ast));
        final List<IExtendedModifier> modifiers = typeParameter.modifiers();
        assert modifiers != null;
        Annotations.decompileAnnotations(typeParam, modifiers, contextT);
        final T superT = typeParam.getSuperT();
        if (superT != null && !superT.isObject()) {
            typeParameter.typeBounds().add(newType(superT, contextT));
        }
        for (final T interfaceT : typeParam.getInterfaceTs()) {
            assert interfaceT != null;
            typeParameter.typeBounds().add(newType(interfaceT, contextT));
        }
        typeParameters.add(typeParameter);
    }
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public TypeParameter convert(org.eclipse.jdt.internal.compiler.ast.TypeParameter typeParameter) {
    final TypeParameter typeParameter2 = new TypeParameter(this.ast);
    final SimpleName simpleName = new SimpleName(this.ast);
    simpleName.internalSetIdentifier(new String(typeParameter.name));
    int start = typeParameter.sourceStart;
    int end = typeParameter.sourceEnd;
    simpleName.setSourceRange(start, end - start + 1);
    typeParameter2.setName(simpleName);
    final TypeReference superType = typeParameter.type;
    end = typeParameter.declarationSourceEnd;
    if (superType != null) {
        Type type = convertType(superType);
        typeParameter2.typeBounds().add(type);
        end = type.getStartPosition() + type.getLength() - 1;
    }/*from  ww  w.  ja v  a  2 s.  c o  m*/
    TypeReference[] bounds = typeParameter.bounds;
    if (bounds != null) {
        Type type = null;
        for (int index = 0, length = bounds.length; index < length; index++) {
            type = convertType(bounds[index]);
            typeParameter2.typeBounds().add(type);
            end = type.getStartPosition() + type.getLength() - 1;
        }
    }
    start = typeParameter.declarationSourceStart;
    end = retrieveClosingAngleBracketPosition(end);
    typeParameter2.setSourceRange(start, end - start + 1);
    if (this.resolveBindings) {
        recordName(simpleName, typeParameter);
        recordNodes(typeParameter2, typeParameter);
        typeParameter2.resolveBinding();
    }
    return typeParameter2;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.TypeParameter node) {
    TypeParameter element = (TypeParameter) this.binding.get(node);
    initializeNode(element, node);//from w  w w . j  a  v  a  2  s.  co m
    element.setName(node.getName().getIdentifier());

    for (Iterator<?> i = node.typeBounds().iterator(); i.hasNext();) {
        ASTNode itElement = this.binding.get(i.next());
        if (itElement != null) {
            element.getBounds().add(JDTVisitorUtils.completeTypeAccess(itElement, this));
        }
    }

    JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this);
}