Example usage for org.eclipse.jdt.core.dom Type getAST

List of usage examples for org.eclipse.jdt.core.dom Type getAST

Introduction

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

Prototype

public final AST getAST() 

Source Link

Document

Returns this node's AST.

Usage

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaGeneralization.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override// w  w  w.jav  a  2s . co  m
protected void setASTFeatureValue(EStructuralFeature feature, Type astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();
    switch (feature.getFeatureID()) {
    case UMLPackage.GENERALIZATION__GENERAL:
        parentForwardJavaInterface_OwnedGeneralizations.parentForwardJavaType.parentForwardJavaSrcDir_Files
                .createImportDeclarationIfNeeded((org.eclipse.uml2.uml.Type) value);
        String newInterfaceName = ((Interface) value).getName();
        TypeDeclaration parentClass = JavaSyncUtils.getMasterClass((CompilationUnit) currentCompialtionUnit);
        parentClass.superInterfaceTypes().set(parentClass.superInterfaceTypes().indexOf(astElement),
                JavaSyncUtils.getJavaTypeFromString(ast, newInterfaceName, false));
        break;
    default:
        throw new IllegalArgumentException("Cannot get value for feature in ReversePLField:" + feature);
    }
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaRealization.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from ww w.jav  a 2 s .  c om*/
protected void setASTFeatureValue(EStructuralFeature feature, Type astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();
    switch (feature.getFeatureID()) {
    case UMLPackage.INTERFACE_REALIZATION__CONTRACT:
        parentForwardJavaClass_OwnedRealizations.parentForwardJavaType.parentForwardJavaSrcDir_Files
                .createImportDeclarationIfNeeded((org.eclipse.uml2.uml.Type) value);
        String newInterfaceName = ((Interface) value).getName();
        TypeDeclaration parentClass = JavaSyncUtils.getMasterClass((CompilationUnit) currentCompialtionUnit);
        parentClass.superInterfaceTypes().set(parentClass.superInterfaceTypes().indexOf(astElement),
                JavaSyncUtils.getJavaTypeFromString(ast, newInterfaceName, false));
        break;
    default:
        throw new IllegalArgumentException("Cannot get value for feature in ReversePLField:" + feature);
    }
}

From source file:com.google.devtools.j2objc.translate.JavaToIOSTypeConverter.java

License:Open Source License

private void convertType(Type original) {
    if (original == null) {
        return;//from   w w  w  . j  a  va  2 s .  c o m
    }
    ITypeBinding binding = Types.getTypeBinding(original);
    ITypeBinding newBinding = Types.mapType(binding);
    if (binding != newBinding) {
        ASTUtil.setProperty(original, ASTFactory.newType(original.getAST(), newBinding));
    }
}

From source file:com.google.gdt.eclipse.designer.builders.GwtBuilder.java

License:Open Source License

/**
 * @return the {@link Type} that extends {@link Object}, even if given is primitive.
 *///from  w  w w .ja  va2 s .  co m
private static Type getObjectType(Type type) {
    if (type.isPrimitiveType()) {
        String identifier = ((PrimitiveType) type).getPrimitiveTypeCode().toString();
        if (identifier.equals("int")) {
            identifier = "Integer";
        } else {
            identifier = StringUtils.capitalize(identifier);
        }
        SimpleName typeName = type.getAST().newSimpleName(identifier);
        return type.getAST().newSimpleType(typeName);
    } else {
        return type;
    }
}

From source file:de.ovgu.cide.export.physical.ahead.MethodObjectHelper.java

License:Open Source License

/**
 * //from www  .j a  va2  s  .  co  m
 * @param methodObjectClass
 * @param type
 *            original ASTNode is sufficient (is copied internally)
 * @param name
 * @param colors
 *            null if not used
 */
private static void addField(TypeDeclaration methodObjectClass, Type type, String name, Set<IFeature> colors) {
    AST ast = type.getAST();
    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    fragment.setName(ast.newSimpleName(name));
    FieldDeclaration field = ast.newFieldDeclaration(fragment);
    field.setType((Type) ASTNode.copySubtree(ast, type));
    Modifier modifier = ast.newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
    field.modifiers().add(modifier);
    methodObjectClass.bodyDeclarations().add(field);
}

From source file:net.sf.eclipsecs.ui.quickfixes.misc.ArrayTypeStyleQuickfix.java

License:Open Source License

/**
 * {@inheritDoc}//from   w ww . ja  va2s  . c om
 */
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {

    return new ASTVisitor() {

        public boolean visit(VariableDeclarationStatement node) {

            if (containsPosition(node, markerStartOffset)) {

                if (isCStyle(node.fragments())) {

                    int dimensions = 0;

                    List fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        if (decl.getExtraDimensions() > dimensions) {
                            dimensions = decl.getExtraDimensions();

                        }
                        decl.setExtraDimensions(0);
                    }

                    // wrap current type into ArrayType
                    ArrayType arrayType = createArrayType(node.getType(), dimensions);
                    node.setType(arrayType);

                } else if (isJavaStyle(node.getType())) {

                    int dimensions = ((ArrayType) node.getType()).getDimensions();

                    List fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        decl.setExtraDimensions(dimensions);
                    }

                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(),
                            ((ArrayType) node.getType()).getElementType());
                    node.setType(elementType);
                }
            }
            return true;
        }

        public boolean visit(SingleVariableDeclaration node) {

            if (containsPosition(node, markerStartOffset)) {
                if (isCStyle(node)) {
                    // wrap the existing type into an array type
                    node.setType(createArrayType(node.getType(), node.getExtraDimensions()));
                    node.setExtraDimensions(0);
                } else if (isJavaStyle(node.getType())) {

                    ArrayType arrayType = (ArrayType) node.getType();
                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(), arrayType.getElementType());

                    node.setType(elementType);
                    node.setExtraDimensions(arrayType.getDimensions());
                }
            }

            return true;
        }

        public boolean visit(FieldDeclaration node) {

            if (containsPosition(node, markerStartOffset)) {

                if (isCStyle(node.fragments())) {

                    int dimensions = 0;

                    List fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        if (decl.getExtraDimensions() > dimensions) {
                            dimensions = decl.getExtraDimensions();

                        }
                        decl.setExtraDimensions(0);
                    }

                    // wrap current type into ArrayType
                    ArrayType arrayType = createArrayType(node.getType(), dimensions);
                    node.setType(arrayType);
                } else if (isJavaStyle(node.getType())) {

                    int dimensions = ((ArrayType) node.getType()).getDimensions();

                    List fragments = node.fragments();
                    for (int i = 0, size = fragments.size(); i < size; i++) {
                        VariableDeclaration decl = (VariableDeclaration) fragments.get(i);
                        decl.setExtraDimensions(dimensions);
                    }

                    Type elementType = (Type) ASTNode.copySubtree(node.getAST(),
                            ((ArrayType) node.getType()).getElementType());
                    node.setType(elementType);
                }
            }
            return true;
        }

        private boolean isJavaStyle(Type type) {
            return type instanceof ArrayType;
        }

        private boolean isCStyle(VariableDeclaration decl) {
            return decl.getExtraDimensions() > 0;
        }

        private boolean isCStyle(List fragments) {

            Iterator it = fragments.iterator();
            while (it.hasNext()) {
                VariableDeclaration decl = (VariableDeclaration) it.next();
                if (isCStyle(decl)) {
                    return true;
                }
            }
            return false;
        }

        private ArrayType createArrayType(Type componentType, int dimensions) {
            Type type = (Type) ASTNode.copySubtree(componentType.getAST(), componentType);
            ArrayType arrayType = componentType.getAST().newArrayType(type, dimensions);

            return arrayType;
        }
    };
}

From source file:org.autorefactor.refactoring.rules.EnumMapRatherThanHashMapRefactoring.java

License:Open Source License

/**
 * Map parameter for HashMap constructor to EnumMap constructor. HashMap(Map) ->
 * EnumMap(EnumMap) <br/>//from   w  w  w .  ja v a 2 s .co m
 * other HashMap constructors -> EnumMap(Class) <br>
 *
 * @return correct parameter for EnumMap constructor
 */
private Expression resolveParameter(Type keyType, List<Expression> originalArgs) {
    if (!originalArgs.isEmpty() && instanceOf(originalArgs.get(0), "java.util.EnumMap")) {
        return ctx.getASTBuilder().copy(originalArgs.get(0));
    }
    TypeLiteral keyTypeLiteral = keyType.getAST().newTypeLiteral();
    keyTypeLiteral.setType(ctx.getASTBuilder().copy(keyType));
    return keyTypeLiteral;
}

From source file:org.eclipse.pde.api.tools.internal.util.Signatures.java

License:Open Source License

private static Type getType(ASTNode node) {
    switch (node.getNodeType()) {
    case ASTNode.SINGLE_VARIABLE_DECLARATION: {
        SingleVariableDeclaration param = (SingleVariableDeclaration) node;
        Type type = param.getType();
        int extraDim = param.getExtraDimensions();

        if (extraDim == 0) {
            return type;
        }//from ww w .  j a  v a 2  s.  co  m
        AST ast = type.getAST();
        type = (Type) ASTNode.copySubtree(ast, type);
        for (int i = 0; i < extraDim; i++) {
            type = ast.newArrayType(type);
        }
        return type;
    }
    default: {
        // ASTNode.METHOD_DECLARATION
        MethodDeclaration methodDeclaration = (MethodDeclaration) node;
        Type type = methodDeclaration.getReturnType2();
        int extraDim = methodDeclaration.getExtraDimensions();

        if (extraDim == 0) {
            return type;
        }
        AST ast = type.getAST();
        type = (Type) ASTNode.copySubtree(ast, type);
        for (int i = 0; i < extraDim; i++) {
            type = ast.newArrayType(type);
        }
        return type;
    }
    }
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstParser.java

License:Open Source License

/**
 * @return the {@link Type} for given qualified name.
 *//*  w  w w.  j  a  v  a2  s .  com*/
public Type parseQualifiedType(int position, String name) throws Exception {
    VariableDeclarationStatement statement = (VariableDeclarationStatement) parseStatement(position,
            name + " __parseName;");
    Type type = statement.getType();
    statement.setType(type.getAST().newPrimitiveType(PrimitiveType.VOID));
    return type;
}

From source file:org.spoofax.interpreter.adapter.ecj.ECJFactory.java

License:LGPL

private Type asType(IStrategoTerm term) {
    Type x = ((WrappedType) term).getWrappee();
    return x.getParent() == null && x.getAST() == ast ? x : (Type) ASTNode.copySubtree(ast, x);
}