Example usage for org.eclipse.jdt.core.dom AST newQualifiedType

List of usage examples for org.eclipse.jdt.core.dom AST newQualifiedType

Introduction

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

Prototype

public QualifiedType newQualifiedType(Type qualifier, SimpleName name) 

Source Link

Document

Creates and returns a new unparented qualified type node with the given qualifier type and name.

Usage

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Converts the provided qualified type name to a new {@link Type} object.
 *
 * @param ast the {@link AST}//from   w w  w.  ja v  a  2 s  . c  o m
 * @param qualifiedName the qualified type name
 * @return a new {@link Type} object
 */
public static Type toType(final AST ast, String qualifiedName) {
    final String[] names = qualifiedName.split("\\.");
    if (names.length == 0) {
        return null;
    }
    final SimpleType simpleType = ast.newSimpleType(ast.newSimpleName(names[0]));
    if (names.length == 1) {
        return simpleType;
    }
    Type result = simpleType;
    for (int i = 1; i < names.length; i++) {
        result = ast.newQualifiedType(result, ast.newSimpleName(names[i]));
    }
    return result;
}

From source file:org.decojer.cavaj.utils.Expressions.java

License:Open Source License

/**
 * New type./*from  ww  w.  j a va 2 s.c o  m*/
 *
 * @param t
 *            type
 * @param context
 *            context
 * @return AST type
 */
@SuppressWarnings("deprecation")
public static Type newType(@Nonnull final T t, @Nonnull final Element context) {
    final AST ast = context.getCu().getAst();
    // handle array first because annot(array()) is special
    if (t.isArray()) {
        if (ast.apiLevel() <= AST.JLS4) {
            final T componentT = t.getComponentT();
            assert componentT != null;
            return ast.newArrayType(newType(componentT, context));
        }
        final T elementT = t.getElementT();
        assert elementT != null;
        for (T checkT = t; checkT != null && checkT.isArray(); checkT = checkT.getComponentT()) {
            if (checkT.isAnnotated()) {
                final ArrayType arrayType = ast.newArrayType(newType(elementT, context));
                final List<Dimension> dimensions = arrayType.dimensions();
                dimensions.clear();
                for (T componentT = t; componentT != null
                        && componentT.isArray(); componentT = componentT.getComponentT()) {
                    final Dimension dimension = ast.newDimension();
                    final List<IExtendedModifier> annotations = dimension.annotations();
                    assert annotations != null;
                    if (componentT.isAnnotated()) {
                        Annotations.decompileAnnotations(componentT, annotations, context);
                    }
                    dimensions.add(dimension);
                }
                return arrayType;
            }
        }
        return ast.newArrayType(newType(elementT, context), t.getDimensions());
    }
    if (t.isAnnotated()) {
        Type type = newType(t.getRawT(), context);
        if (ast.apiLevel() <= AST.JLS4) {
            log.warn("Cannot decompile type annotations for type '" + t + "' in Eclipse AST JLS4!");
            return type;
        }
        // parameterized type is not directly annotateable in Eclipse; but DecoJer thinks the
        // whole type is meant, not just the generic type, hence translate here
        AnnotatableType annotatableType = (AnnotatableType) (type instanceof ParameterizedType
                ? ((ParameterizedType) type).getType()
                : type);
        qualified: if (annotatableType instanceof SimpleType) {
            // direct annotation of qualified types adds the annotations as first element,
            // strange Java spec doesn't allow "@A package.Name" but wants "package.@A Name"
            final Name typeName = ((SimpleType) annotatableType).getName();
            if (!(typeName instanceof QualifiedName)) {
                break qualified;
            }
            final Name qualifier = ((QualifiedName) typeName).getQualifier();
            final SimpleName name = ((QualifiedName) typeName).getName();
            // cannot delete mandory childs, copy them
            annotatableType = ast.newNameQualifiedType((Name) ASTNode.copySubtree(ast, qualifier),
                    (SimpleName) ASTNode.copySubtree(ast, name));
            if (type instanceof ParameterizedType) {
                ((ParameterizedType) type).setType(annotatableType);
            } else {
                type = annotatableType;
            }
        }
        final List<IExtendedModifier> annotations = annotatableType.annotations();
        assert annotations != null;
        Annotations.decompileAnnotations(t, annotations, context);
        return type;
    }
    // doesn't work, now with Dimension (see above): if (t.isArray()) { return
    // ast.newArrayType(newType(t.getComponentT(), contextT)); }
    if (t.isParameterized()) {
        final T genericT = t.getGenericT();
        assert genericT != null;
        final ParameterizedType parameterizedType = ast.newParameterizedType(newType(genericT, context));
        for (final T typeArg : t.getTypeArgs()) {
            assert typeArg != null;
            parameterizedType.typeArguments().add(newType(typeArg, context));
        }
        return parameterizedType;
    }
    if (t.isWildcard()) {
        final T boundT = t.getBoundT();
        if (boundT == null) {
            return ast.newWildcardType();
        }
        if (t.isSubclassOf()) {
            final WildcardType wildcardType = ast.newWildcardType();
            // default...newWildcardType.setUpperBound(true);
            wildcardType.setBound(newType(boundT, context));
            return wildcardType;
        }
        final WildcardType wildcardType = ast.newWildcardType();
        wildcardType.setUpperBound(false);
        wildcardType.setBound(newType(boundT, context));
        return wildcardType;
    }
    if (t.isMulti()) {
        log.warn("Convert type for multi-type '" + t + "'!");
        // prefer boolean for multi-type with 0 or 1, synchronous to newLiteral()!
        // prefer byte before char if no explicit char type given
    }
    if (t.is(T.BOOLEAN)) {
        return ast.newPrimitiveType(PrimitiveType.BOOLEAN);
    }
    if (t.is(T.BYTE)) {
        return ast.newPrimitiveType(PrimitiveType.BYTE);
    }
    if (t.is(T.CHAR)) {
        return ast.newPrimitiveType(PrimitiveType.CHAR);
    }
    if (t.is(T.SHORT)) {
        return ast.newPrimitiveType(PrimitiveType.SHORT);
    }
    if (t.is(T.INT)) {
        return ast.newPrimitiveType(PrimitiveType.INT);
    }
    if (t.is(T.FLOAT)) {
        return ast.newPrimitiveType(PrimitiveType.FLOAT);
    }
    if (t.is(T.LONG)) {
        return ast.newPrimitiveType(PrimitiveType.LONG);
    }
    if (t.is(T.DOUBLE)) {
        return ast.newPrimitiveType(PrimitiveType.DOUBLE);
    }
    if (t.is(T.VOID)) {
        return ast.newPrimitiveType(PrimitiveType.VOID);
    }
    if (t.isQualified()) {
        final T qualifierT = t.getQualifierT();
        assert qualifierT != null : "cannot be null for qualified";
        // could be ParamT etc., not decompileable with name as target;
        // restrict qualifications to really necessary enclosings:
        // t = Outer.Inner.InnerInner, t = Outer.Inner ==> Inner
        final T contextT = context.getT();
        assert contextT != null;
        if (contextT.getFullName().startsWith(qualifierT.getFullName())) {
            // TODO full name has too much info yet (like annotations)
            return ast.newSimpleType(newSimpleName(t.getSimpleIdentifier(), ast));
        }
        return ast.newQualifiedType(newType(qualifierT, context), newSimpleName(t.getSimpleIdentifier(), ast));
    }
    // else fall through...
    return ast.newSimpleType(newTypeName(t, context));
}

From source file:org.evosuite.testcarver.codegen.CodeGenerator.java

License:Open Source License

private Type createAstType(final String type, final AST ast) {
    if (type.startsWith("[")) // does type specify an array?
    {/*  w w w . j  a  va  2s. c o m*/
        return this.createAstArrayType(type, ast);
    } else {
        final String[] fragments = type.split("\\.");
        if (fragments.length == 1) {
            return ast.newSimpleType(ast.newSimpleName(fragments[0]));
        }

        final String[] pkgArray = new String[fragments.length - 1];
        System.arraycopy(fragments, 0, pkgArray, 0, pkgArray.length);
        final SimpleType pkgType = ast.newSimpleType(ast.newName(pkgArray));

        return ast.newQualifiedType(pkgType, ast.newSimpleName(fragments[fragments.length - 1]));
    }
}

From source file:org.evosuite.testcarver.codegen.JUnitCodeGenerator.java

License:Open Source License

private Type createAstType(final String type, final AST ast) {
    if (type.startsWith("[")) // does type specify an array?
    {//from   www .  j a v  a2  s  .c  om
        return this.createAstArrayType(type, ast);
    } else {
        final String[] fragments = type.split("\\.");
        if (fragments.length == 1) {
            if (fragments[0].contains("$")) {
                return ast.newSimpleType(ast.newName(fragments[0].replace('$', '.')));
            } else {
                return ast.newSimpleType(ast.newSimpleName(fragments[0]));
            }
        }

        if (type.startsWith("java.lang")) {
            return ast.newSimpleType(ast.newSimpleName(fragments[2]));
        }

        final String[] pkgArray = new String[fragments.length - 1];
        System.arraycopy(fragments, 0, pkgArray, 0, pkgArray.length);
        final SimpleType pkgType = ast.newSimpleType(ast.newName(pkgArray));

        final String clazzName = fragments[fragments.length - 1];
        if (clazzName.contains("$")) {
            final String[] clazzSplit = clazzName.split("\\$");

            final String[] newPkgType = new String[pkgArray.length + clazzSplit.length - 1];
            System.arraycopy(pkgArray, 0, newPkgType, 0, pkgArray.length);
            System.arraycopy(clazzSplit, 0, newPkgType, pkgArray.length, clazzSplit.length - 1);

            if (clazzName.endsWith("[]")) {
                final QualifiedType t = ast.newQualifiedType(ast.newSimpleType(ast.newName(newPkgType)),
                        ast.newSimpleName(clazzSplit[clazzSplit.length - 1].replace("[]", "")));
                return ast.newArrayType(t);
            } else {
                return ast.newQualifiedType(ast.newSimpleType(ast.newName(newPkgType)),
                        ast.newSimpleName(clazzSplit[clazzSplit.length - 1]));
            }
        } else {
            final String[] clazzSplit = clazzName.split("\\$");

            final String[] newPkgType = new String[pkgArray.length + clazzSplit.length - 1];
            System.arraycopy(pkgArray, 0, newPkgType, 0, pkgArray.length);
            System.arraycopy(clazzSplit, 0, newPkgType, pkgArray.length, clazzSplit.length - 1);

            if (clazzName.endsWith("[]")) {
                final QualifiedType t = ast.newQualifiedType(ast.newSimpleType(ast.newName(newPkgType)),
                        ast.newSimpleName(clazzSplit[clazzSplit.length - 1].replace("[]", "")));
                return ast.newArrayType(t);
            } else {
                return ast.newQualifiedType(ast.newSimpleType(ast.newName(newPkgType)),
                        ast.newSimpleName(clazzSplit[clazzSplit.length - 1]));
            }
        }
    }
}