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

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

Introduction

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

Prototype

public void setName(SimpleName typeName) 

Source Link

Document

Sets the name of the type declared in this type declaration to the given name.

Usage

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 av  a2s .co 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:java5totext.input.JDTVisitor.java

License:Open Source License

private void endVisitATD(org.eclipse.jdt.core.dom.AbstractTypeDeclaration node,
        AbstractTypeDeclaration element) {
    element.setName(node.getName().getIdentifier());
    for (Iterator<?> i = node.bodyDeclarations().iterator(); i.hasNext();) {
        BodyDeclaration itElement = (BodyDeclaration) this.binding.get(i.next());
        if (itElement != null)
            element.getBodyDeclarations().add(itElement);
    }/*from ww  w .j  ava  2  s  .  c o  m*/

    // process for package direct types
    if (node.isPackageMemberTypeDeclaration()) {
        String mainClassName = this.currentJavaFilePath
                .substring(this.currentJavaFilePath.lastIndexOf(java.io.File.separator) + 1);
        mainClassName = mainClassName.substring(0, mainClassName.length() - ".java".length());
        //   process for main type of the .java file
        if (mainClassName.equalsIgnoreCase(node.getName().getIdentifier())) {
            // memorize this AST node as the main
            setRootTypeOrEnum(element);
            for (Iterator<ImportDeclaration> i = this.imports.iterator(); i.hasNext();) {
                ImportDeclaration importDecl = i.next();
                element.getImports().add(importDecl);
            }
        }

        if (this.currentPackage == null) { // Type without package 
            this.currentPackage = (PackageDeclaration) this.globalBindings.getTarget(this.DEFAULT_PKG_ID);
            if (this.currentPackage == null) {
                this.currentPackage = this.factory.createPackageDeclaration();
                this.currentPackage.setName(this.DEFAULT_PKG_ID);
                if (this.currentJavaFilePath != null)
                    this.globalBindings.addTarget(this.DEFAULT_PKG_ID, this.currentPackage);
            }
        }
        this.currentPackage.getOwnedElements().add(element);
        // calculate qualified name
        String qualifiedName = this.currentPackage.getQualifiedName() + "." + element.getName();
        element.setQualifiedName(qualifiedName);

    }
}

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

License:Open Source License

private static void decompileType(@Nonnull final T t, @Nonnull final CU cu) {
    if (checkTypeIgnore(t, cu)) {
        return;/*from  www  .  jav a2 s.  co  m*/
    }
    final AST ast = cu.getAst();

    // AF.STRICTFP is no valid inner modifier for bytecode, strictfp modifier at class generates
    // strictfp modifier for all method in class -> check here and oppress then in methods
    boolean strictFp = false;
    for (final Element declaration : t.getDeclarations()) {
        if (!(declaration instanceof M)) {
            continue;
        }
        if (!((M) declaration).getAf(AF.STRICTFP)) {
            break;
        }
        strictFp = true;
    }
    if (t.getAstNode() == null) {
        AbstractTypeDeclaration typeDeclaration = null;

        // annotation type declaration
        if (t.getAf(AF.ANNOTATION)) {
            final T superT = t.getSuperT();
            if (superT == null || !superT.isObject()) {
                log.warn("Classfile with AccessFlag.ANNOTATION has no super class Object but has '" + superT
                        + "'!");
            }
            if (t.getInterfaceTs().length != 1 || !t.getInterfaceTs()[0].is(Annotation.class)) {
                log.warn("Classfile with AccessFlag.ANNOTATION has no interface '" + Annotation.class.getName()
                        + "' but has '" + t.getInterfaceTs()[0] + "'!");
            }
            typeDeclaration = ast.newAnnotationTypeDeclaration();
        }
        // enum declaration
        if (t.isEnum() && !t.getCu().check(DFlag.IGNORE_ENUM)) {
            if (typeDeclaration != null) {
                log.warn("Enum declaration cannot be an annotation type declaration! Ignoring.");
            } else {
                final T superT = t.getSuperT();
                if (superT == null || !superT.isParameterized() || !superT.is(Enum.class)) {
                    log.warn("Enum type '" + t + "' has no super class '" + Enum.class.getName() + "' but has '"
                            + superT + "'!");
                }
                typeDeclaration = ast.newEnumDeclaration();
                // enums cannot extend other classes than Enum.class, but can have interfaces
                if (t.getInterfaceTs() != null) {
                    for (final T interfaceT : t.getInterfaceTs()) {
                        assert interfaceT != null;
                        ((EnumDeclaration) typeDeclaration).superInterfaceTypes().add(newType(interfaceT, t));
                    }
                }
            }
        }
        // no annotation type declaration or enum declaration => normal class or interface type
        // declaration
        if (typeDeclaration == null) {
            typeDeclaration = ast.newTypeDeclaration();
            decompileTypeParams(t.getTypeParams(), ((TypeDeclaration) typeDeclaration).typeParameters(), t);
            final T superT = t.getSuperT();
            if (superT != null && !superT.isObject()) {
                ((TypeDeclaration) typeDeclaration).setSuperclassType(newType(superT, t));
            }
            for (final T interfaceT : t.getInterfaceTs()) {
                assert interfaceT != null;
                ((TypeDeclaration) typeDeclaration).superInterfaceTypes().add(newType(interfaceT, t));
            }
        }
        t.setAstNode(typeDeclaration);

        final List<IExtendedModifier> modifiers = typeDeclaration.modifiers();
        assert modifiers != null;

        // add annotation modifiers before other modifiers, order preserved in source code
        // generation through eclipse.jdt
        if (t.getAs() != null) {
            Annotations.decompileAnnotations(t.getAs(), modifiers, t);
        }

        // decompile remaining modifier flags
        if (t.getAf(AF.PUBLIC)) {
            modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
        }
        // for inner classes
        if (t.getAf(AF.PRIVATE)) {
            modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
        }
        // for inner classes
        if (t.getAf(AF.PROTECTED)) {
            modifiers.add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD));
        }
        // for inner classes
        if (t.isStatic() && !t.isInterface()) {
            modifiers.add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
        }
        if (t.getAf(AF.FINAL) && !(typeDeclaration instanceof EnumDeclaration)) {
            // enum declaration is final by default
            modifiers.add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
        }
        if (t.isInterface()) {
            if (typeDeclaration instanceof TypeDeclaration) {
                ((TypeDeclaration) typeDeclaration).setInterface(true);
            }
        } else if (!t.getAf(AF.SUPER) && !t.isDalvik()) {
            // modern invokesuper syntax, is always set in current JVM, but not in Dalvik or
            // inner classes info flags
            log.warn("Modern invokesuper syntax flag not set in type '" + t + "'!");
        }
        if (t.getAf(AF.ABSTRACT) && !(typeDeclaration instanceof AnnotationTypeDeclaration)
                && !(typeDeclaration instanceof EnumDeclaration) && !(typeDeclaration instanceof TypeDeclaration
                        && ((TypeDeclaration) typeDeclaration).isInterface())) {
            modifiers.add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD));
        }
        if (strictFp) {
            modifiers.add(ast.newModifier(ModifierKeyword.STRICTFP_KEYWORD));
        }

        final String simpleName = t.getSimpleName();
        typeDeclaration.setName(newSimpleName(simpleName.length() > 0 ? simpleName : t.getPName(), ast));

        // decompile synthetic Javadoc-comment if no annotation set
        if (t.isSynthetic()) {
            final Javadoc javadoc = ast.newJavadoc();
            final TagElement tagElement = ast.newTagElement();
            tagElement.setTagName("is synthetic");
            javadoc.tags().add(tagElement);
            typeDeclaration.setJavadoc(javadoc);
        }
        if (t.getAf(AF.DEPRECATED) && !Annotations.isDeprecatedAnnotation(t.getAs())) {
            final Javadoc javadoc = ast.newJavadoc();
            final TagElement tagElement = ast.newTagElement();
            tagElement.setTagName("@deprecated");
            javadoc.tags().add(tagElement);
            typeDeclaration.setJavadoc(javadoc);
        }
    }
    for (final Element bd : t.getDeclarations()) {
        if (bd instanceof F) {
            decompileField((F) bd, cu);
        }
        if (bd instanceof M) {
            decompileMethod((M) bd, cu, strictFp);
        }
    }
}

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

License:Open Source License

/**
 * Fill informations in the MoDisco {@code AbstractTypeDeclaration} node
 * from the JDT {@code AbstractTypeDeclaration} node.
 * // www .j  av  a2s .  c  om
 * @param node
 *            the JDT {@code AbstractTypeDeclaration} node
 * @param element
 *            the MoDisco {@code AbstractTypeDeclaration} node
 */
private void endVisitATD(final org.eclipse.jdt.core.dom.AbstractTypeDeclaration node,
        final AbstractTypeDeclaration element) {
    element.setName(node.getName().getIdentifier());
    for (Iterator<?> i = node.bodyDeclarations().iterator(); i.hasNext();) {
        BodyDeclaration itElement = (BodyDeclaration) this.binding.get(i.next());
        if (itElement != null) {
            element.getBodyDeclarations().add(itElement);
        }
    }

    // process for package direct types
    if (node.isPackageMemberTypeDeclaration()) {
        String ext = null;
        if (this.cuNode instanceof ICompilationUnit) {
            ext = ".java"; //$NON-NLS-1$
        } else if (this.cuNode instanceof IClassFile) {
            ext = ".class"; //$NON-NLS-1$
        }
        if (ext == null) {
            // memorize this AST node as the main
            setRootTypeOrEnum(element);
        } else {
            String mainClassName = this.currentFilePath
                    .substring(this.currentFilePath.lastIndexOf(java.io.File.separator) + 1);
            mainClassName = mainClassName.substring(0, mainClassName.length() - ext.length());
            // process for main type of the .java file
            if (mainClassName.equalsIgnoreCase(node.getName().getIdentifier())) {
                // memorize this AST node as the main
                setRootTypeOrEnum(element);
            }
        }

        if (this.currentPackage == null) { // Type without package
            this.currentPackage = (Package) getGlobalBindings().getTarget(JDTVisitor.DEFAULT_PKG_ID);
            if (this.currentPackage == null) {
                this.currentPackage = this.factory.createPackage();
                this.currentPackage.setName(JDTVisitor.DEFAULT_PKG_ID);
                if (this.currentFilePath != null) {
                    this.currentPackage.setModel(this.jdtModel);
                }
                getGlobalBindings().addTarget(JDTVisitor.DEFAULT_PKG_ID, this.currentPackage);
            }
        }
        this.currentPackage.getOwnedElements().add(element);
        // calculate qualified name
        // String qualifiedName = this.currentPackage.getQualifiedName() +
        // "." + element.getName();
    }
    if (this.isINCREMENTALDISCOVERING) {
        List<EObject> toBeRemoved = new ArrayList<EObject>();
        Iterator<BodyDeclaration> bodyDeclarations = element.getBodyDeclarations().iterator();
        while (bodyDeclarations.hasNext()) {
            BodyDeclaration bodyDeclaration = bodyDeclarations.next();
            if (bodyDeclaration.isProxy()) {
                toBeRemoved.add(bodyDeclaration);
            }
        }
        if (element instanceof EnumDeclaration) {
            Iterator<EnumConstantDeclaration> enums = ((EnumDeclaration) element).getEnumConstants().iterator();
            while (enums.hasNext()) {
                EnumConstantDeclaration enumDecl = enums.next();
                if (enumDecl.isProxy()) {
                    toBeRemoved.add(enumDecl);
                }
            }
        }
        // To remove proxy Field element that have been replaced by an other
        // one
        // This is useful in the case of the incremental algorithm
        Iterator<EObject> toBeRemovedIter = toBeRemoved.iterator();
        while (toBeRemovedIter.hasNext()) {
            EObject eObject = toBeRemovedIter.next();
            if (eObject instanceof FieldDeclaration) {
                FieldDeclaration fieldDeclaration = (FieldDeclaration) eObject;
                if (fieldDeclaration.getFragments().size() > 0) {
                    String message = "Proxy field declaration should not contains any fragments (size=" //$NON-NLS-1$
                            + fieldDeclaration.getFragments().size() + "): " //$NON-NLS-1$
                            + JDTVisitorUtils.getQualifiedName(node) + "."; //$NON-NLS-1$
                    try {
                        message += (fieldDeclaration.getFragments().get(0)).getName();
                    } catch (Exception e) {
                        message += "???"; //$NON-NLS-1$
                    }
                    RuntimeException exception = new RuntimeException(message);
                    MoDiscoLogger.logError(exception, JavaActivator.getDefault());
                    throw exception;
                }
                deepRemove(eObject);
                element.getBodyDeclarations().remove(eObject);
                if (element instanceof EnumDeclaration) {
                    ((EnumDeclaration) element).getEnumConstants().remove(eObject);
                }
            }
        }
    }
}

From source file:org.flowerplatform.codesync.code.java.adapter.JavaTypeDeclarationModelAdapter.java

License:Open Source License

@Override
public void setValueFeatureValue(Object element, Object feature, final Object value) {
    if (CoreConstants.NAME.equals(feature)) {
        AbstractTypeDeclaration type = getAbstractTypeDeclaration(element);
        String name = (String) value;
        type.setName(type.getAST().newSimpleName(name));
    } else if (CodeSyncCodeJavaConstants.SUPER_CLASS.equals(feature)) {
        if (element instanceof TypeDeclaration) {
            String superClass = value.toString();
            TypeDeclaration cls = (TypeDeclaration) element;
            AST ast = cls.getAST();//  www  .  jav a 2  s. co m
            Type type = null;
            if (superClass != null) {
                type = getTypeFromString(ast, superClass);
            }
            cls.setSuperclassType(type);
        }
    }
    super.setValueFeatureValue(element, feature, value);
}