Example usage for org.eclipse.jdt.core.dom TypeDeclaration setSuperclassType

List of usage examples for org.eclipse.jdt.core.dom TypeDeclaration setSuperclassType

Introduction

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

Prototype

public void setSuperclassType(Type superclassType) 

Source Link

Document

Sets or clears the superclass declared in this type declaration (added in JLS3 API).

Usage

From source file:ch.acanda.eclipse.pmd.java.resolution.basic.ExtendsObjectQuickFix.java

License:Open Source License

/**
 * Removes {@code extends Object} from the type declaration.
 *///w w  w .  j  a  va  2s.  c  o m
@Override
protected boolean apply(final TypeDeclaration node) {
    node.setSuperclassType(null);
    return true;
}

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaTypeModelAdapter.java

License:Open Source License

@Override
public void setValueFeatureValue(Object element, Object feature, final Object value) {
    if (CodeSyncPackage.eINSTANCE.getCodeSyncElement_Name().equals(feature)) {
        AbstractTypeDeclaration type = getAbstractTypeDeclaration(element);
        String name = (String) value;
        type.setName(type.getAST().newSimpleName(name));
    }//from w  w w  . j  a  v a  2 s.  c  o  m
    if (AstCacheCodePackage.eINSTANCE.getClass_SuperClasses().equals(feature)) {
        if (element instanceof TypeDeclaration) {
            List<String> superClasses = (List<String>) value;
            TypeDeclaration cls = (TypeDeclaration) element;
            AST ast = cls.getAST();
            Type type = null;
            if (superClasses != null && superClasses.size() > 0) {
                type = getTypeFromString(ast, superClasses.get(0));
            }
            cls.setSuperclassType(type);
        }
    }
    super.setValueFeatureValue(element, feature, value);
}

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  ww. j a v  a2  s . com
protected void setASTFeatureValue(EStructuralFeature feature, CompilationUnit astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();
    TypeDeclaration masterClass = JavaSyncUtils.getMasterClass(astElement);
    switch (feature.getFeatureID()) {
    case UMLPackage.CLASSIFIER__GENERALIZATION:
        List<Generalization> listGeneralization = (List<Generalization>) value;
        if (listGeneralization.size() > 1)
            throw new IllegalArgumentException("setting more than one superclass ");
        else if (listGeneralization.size() == 1) {
            if (listGeneralization.get(0).getGeneral() == null)
                throw new IllegalArgumentException("generalization does not have attached a superclass");
            // This is a little hack until we will support parameterizable types.
            // The algorithm automatically updates every feature which doesn't work here because it will try to create a simpleType from Class<T1,T2...> 
            String newSuperClass = listGeneralization.get(0).getGeneral().getName();
            String oldSuperClass = masterClass.getSuperclassType() == null ? null
                    : masterClass.getSuperclassType().toString();
            if (!newSuperClass.equals(oldSuperClass)) {
                try {
                    parentForwardJavaSrcDir_Files
                            .createImportDeclarationIfNeeded((Type) listGeneralization.get(0).getGeneral());
                } catch (IllegalPathException ex) {
                    ex.customizeIllegalPathException(listGeneralization.get(0));
                    throw ex;
                }
                masterClass.setSuperclassType(JavaSyncUtils.getJavaTypeFromString(ast, newSuperClass, false));
            }
        } else if (listGeneralization.isEmpty())
            masterClass.setSuperclassType(null);
        break;
    default:
        super.setASTFeatureValue(feature, astElement, value);
    }
}

From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java

License:Open Source License

/**
 * Convert the anonymous class into an inner class.  Fields are added for
 * final variables that are referenced, and a constructor is added.
 *
 * Note: endVisit is used for a depth-first traversal, to make it easier
 * to scan their containing nodes for references.
 *//* w  w w.java2  s . c o  m*/
@Override
@SuppressWarnings("unchecked")
public void endVisit(AnonymousClassDeclaration node) {
    ASTNode parent = node.getParent();
    ClassInstanceCreation newInvocation = null;
    EnumConstantDeclaration enumConstant = null;
    List<Expression> parentArguments;
    String newClassName;
    ITypeBinding innerType;
    boolean isStatic = staticParent(node);
    int modifiers = isStatic ? Modifier.STATIC : 0;
    if (parent instanceof ClassInstanceCreation) {
        newInvocation = (ClassInstanceCreation) parent;
        parentArguments = newInvocation.arguments();
        innerType = Types.getTypeBinding(newInvocation);
        newClassName = innerType.getName();
        innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType,
                modifiers);
    } else if (parent instanceof EnumConstantDeclaration) {
        enumConstant = (EnumConstantDeclaration) parent;
        parentArguments = enumConstant.arguments();
        innerType = Types.getTypeBinding(node);
        newClassName = Types.getTypeBinding(node).getName();
        innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType,
                modifiers);
    } else {
        throw new AssertionError("unknown anonymous class declaration parent: " + parent.getClass().getName());
    }

    // Create a type declaration for this anonymous class.
    AST ast = node.getAST();
    TypeDeclaration typeDecl = ast.newTypeDeclaration();
    if (isStatic) {
        typeDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    }
    Types.addBinding(typeDecl, innerType);
    typeDecl.setName(ast.newSimpleName(newClassName));
    Types.addBinding(typeDecl.getName(), innerType);
    typeDecl.setSourceRange(node.getStartPosition(), node.getLength());

    Type superType = Types.makeType(Types.mapType(innerType.getSuperclass()));
    typeDecl.setSuperclassType(superType);
    for (ITypeBinding interfaceType : innerType.getInterfaces()) {
        typeDecl.superInterfaceTypes().add(Types.makeType(Types.mapType(interfaceType)));
    }

    for (Object bodyDecl : node.bodyDeclarations()) {
        BodyDeclaration decl = (BodyDeclaration) bodyDecl;
        typeDecl.bodyDeclarations().add(NodeCopier.copySubtree(ast, decl));
    }
    typeDecl.accept(new InitializationNormalizer());

    // Fix up references to external types, if necessary.
    Set<IVariableBinding> methodVars = getMethodVars(node);
    final List<ReferenceDescription> references = findReferences(node, methodVars);
    final List<Expression> invocationArgs = parentArguments;
    if (!references.isEmpty() || !invocationArgs.isEmpty()) { // is there anything to fix-up?
        List<IVariableBinding> innerVars = getInnerVars(references);
        if (!innerVars.isEmpty() || !invocationArgs.isEmpty()) {
            GeneratedMethodBinding defaultConstructor = addInnerVars(typeDecl, innerVars, references,
                    parentArguments);
            Types.addBinding(parent, defaultConstructor);
            for (IVariableBinding var : innerVars) {
                if (!isConstant(var)) {
                    parentArguments.add(makeFieldRef(var, ast));
                }
            }
            assert defaultConstructor.getParameterTypes().length == parentArguments.size();
            typeDecl.accept(new ASTVisitor() {
                @Override
                public void endVisit(SimpleName node) {
                    IVariableBinding var = Types.getVariableBinding(node);
                    if (var != null) {
                        for (ReferenceDescription ref : references) {
                            if (var.isEqualTo(ref.binding)) {
                                if (ref.innerField != null) {
                                    setProperty(node, makeFieldRef(ref.innerField, node.getAST()));
                                } else {
                                    // In-line constant.
                                    Object o = var.getConstantValue();
                                    assert o != null;
                                    Expression literal = makeLiteral(o, var.getType().getQualifiedName(),
                                            node.getAST());
                                    setProperty(node, literal);
                                }
                                return;
                            }
                        }
                    }
                }
            });
        }
    }

    // If invocation, replace anonymous class invocation with the new constructor.
    if (newInvocation != null) {
        newInvocation.setAnonymousClassDeclaration(null);
        newInvocation.setType(Types.makeType(innerType));
        IMethodBinding oldBinding = Types.getMethodBinding(newInvocation);
        if (oldBinding == null) {
            oldBinding = newInvocation.resolveConstructorBinding();
        }
        if (oldBinding != null) {
            GeneratedMethodBinding invocationBinding = new GeneratedMethodBinding(oldBinding);
            invocationBinding.setDeclaringClass(innerType);
            Types.addBinding(newInvocation, invocationBinding);
        }
    } else {
        enumConstant.setAnonymousClassDeclaration(null);
    }

    // Add type declaration to enclosing type.
    ITypeBinding outerType = innerType.getDeclaringClass();
    if (outerType.isAnonymous()) {
        // Get outerType node.
        ASTNode n = parent.getParent();
        while (!(n instanceof AnonymousClassDeclaration) && !(n instanceof TypeDeclaration)) {
            n = n.getParent();
        }
        if (n instanceof AnonymousClassDeclaration) {
            AnonymousClassDeclaration outerDecl = (AnonymousClassDeclaration) n;
            outerDecl.bodyDeclarations().add(typeDecl);
        }
    } else {
        AbstractTypeDeclaration outerDecl = (AbstractTypeDeclaration) unit.findDeclaringNode(outerType);
        outerDecl.bodyDeclarations().add(typeDecl);
    }
    Symbols.scanAST(typeDecl);
    super.endVisit(node);
}

From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java

License:Open Source License

/**
 * Creates a type declaration for a new class with the specified parent
 * and interfaces types./*from w w w. j  av  a  2  s. c o m*/
 */
@SuppressWarnings("unchecked")
private TypeDeclaration createClass(AST ast, ITypeBinding scope, ITypeBinding superClass,
        List<ITypeBinding> interfaces) {
    TypeDeclaration decl = ast.newTypeDeclaration();
    if (superClass != null) {
        decl.setSuperclassType(createType(ast, scope, superClass));
    }
    for (ITypeBinding intrface : interfaces) {
        decl.superInterfaceTypes().add(createType(ast, scope, intrface));
    }
    decl.modifiers().add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD));
    decl.setName(ast.newSimpleName(generateClassName()));
    return decl;
}

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

License:Open Source License

@Override
public boolean visit(TypeDeclaration node) {
    ITypeBinding binding = Types.getTypeBinding(node);
    assert binding == Types.mapType(binding); // don't try to translate the
                                              // types being mapped
    Type superClass = node.getSuperclassType();
    if (!node.isInterface()) {
        if (superClass == null) {
            node.setSuperclassType(Types.makeType(Types.getNSObject()));
        } else {/*  w  w  w .ja  v a  2s .  c  om*/
            binding = Types.getTypeBinding(superClass);
            if (Types.hasIOSEquivalent(binding)) {
                ITypeBinding newBinding = Types.mapType(binding);
                node.setSuperclassType(Types.makeType(newBinding));
            }
        }
    }
    @SuppressWarnings("unchecked")
    List<Type> interfaces = node.superInterfaceTypes(); // safe by definition
    for (int i = 0; i < interfaces.size(); i++) {
        Type intrface = interfaces.get(i);
        binding = Types.getTypeBinding(intrface);
        if (Types.hasIOSEquivalent(binding)) {
            ITypeBinding newBinding = Types.mapType(binding);
            interfaces.set(i, Types.makeType(newBinding));
        }
    }
    return super.visit(node);
}

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

License:Open Source License

@Override
public void endVisit(TypeDeclaration node) {
    ITypeBinding binding = Types.getTypeBinding(node);
    if (binding != Types.mapType(binding)) {
        // don't try to translate the types being mapped
        return;//from  w  w  w.  j a v a  2s  .c  o  m
    }

    if (!node.isInterface()) {
        Type superClass = node.getSuperclassType();
        if (superClass == null) {
            node.setSuperclassType(ASTFactory.newType(node.getAST(), Types.getNSObject()));
        } else {
            convertType(superClass);
        }
    }
    List<Type> interfaces = ASTUtil.getSuperInterfaceTypes(node);
    for (int i = 0; i < interfaces.size(); i++) {
        convertType(interfaces.get(i));
    }
}

From source file:com.google.devtools.j2objc.types.ModifiedTypeBindingTest.java

License:Open Source License

public void testGetSuperclass() throws Exception {
    CompilationUnit unit = translateType("Foo", "public class Foo extends java.util.Observable {}");
    TypeDeclaration type = (TypeDeclaration) unit.types().get(0);
    type.setSuperclassType(null);
    ITypeBinding originalBinding = type.resolveBinding();
    ITypeBinding modifiedBinding = ModifiedTypeBinding.bind(type);
    assertNotNull(originalBinding.getSuperclass());
    assertNull(modifiedBinding.getSuperclass());
}

From source file:com.idega.eclipse.ejbwizards.BeanCreator.java

License:Open Source License

protected TypeDeclaration getTypeDeclaration(AST ast, String name, boolean isInterface, String superClass,
        String[] interfaces, Set imports) {
    TypeDeclaration classType = ast.newTypeDeclaration();
    classType.setInterface(isInterface);
    classType.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC));
    classType.setName(ast.newSimpleName(name));
    if (isInterface) {
        classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(superClass)));
    } else {/*from  w w w . j  a va 2  s . c  om*/
        classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(superClass)));
    }

    if (interfaces != null) {
        for (int i = 0; i < interfaces.length; i++) {
            if (!Signature.getSignatureSimpleName(interfaces[i]).equals(name)) {
                classType.superInterfaceTypes().add(
                        ast.newSimpleType(ast.newSimpleName(Signature.getSignatureSimpleName(interfaces[i]))));
                imports.add(getImportSignature(Signature.toString(interfaces[i])));
            }
        }
    }

    return classType;
}

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

License:Apache License

/**
 * Generate the inheritance for the Exception Class "extends". Important:
 * Java only supports single inheritance!
 * /*from w w w .  j  a v a  2s . c om*/
 * @param clazz
 *            the UML class
 * @param ast
 *            the JDT Java AST
 * @param td
 *            TypeDeclaration JDT
 */
private void generateClassInheritance(Classifier clazz, AST ast, TypeDeclaration td) {
    EList<Generalization> generalizations = clazz.getGeneralizations();
    if (generalizations != null) {
        if (!generalizations.isEmpty()) {
            if (generalizations.size() == 1) {
                // Java only supports one Generatlization
                for (Generalization generalization : generalizations) {
                    Classifier interfaceClassifier = generalization.getGeneral();
                    String fullQualifiedInterfaceName = interfaceClassifier.getQualifiedName();
                    Name name = jdtHelper.createFullQualifiedTypeAsName(ast, fullQualifiedInterfaceName,
                            sourceDirectoryPackageName);
                    SimpleType simpleType = ast.newSimpleType(name);
                    td.setSuperclassType(simpleType);
                }
            } else {
                throw new TransformerException(
                        "Java only supports single inheritance! Wrong modeling in class: "
                                + clazz.getQualifiedName());
            }
        } else {
            // Empty, we extend from java.lang.Exception or
            // java.lang.RuntimeException
            String exceptionToBeInherited = "Exception";
            if (!isCheckedException) {
                exceptionToBeInherited = "RuntimeException";
            }
            SimpleType simpleType = ast.newSimpleType(ast.newSimpleName(exceptionToBeInherited));
            td.setSuperclassType(simpleType);
        }
    }
}