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

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

Introduction

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

Prototype

public final AST getAST() 

Source Link

Document

Returns this node's AST.

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(FieldDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }/* www  .  j  a v a  2  s . co m*/
    if (node.getAST().apiLevel() >= JLS3) {
        printModifiers(node.modifiers());
    }
    node.getType().accept(this);
    this.fBuffer.append(" ");//$NON-NLS-1$
    for (Iterator<VariableDeclarationFragment> it = node.fragments().iterator(); it.hasNext();) {
        VariableDeclarationFragment f = it.next();
        f.accept(this);
        if (it.hasNext()) {
            this.fBuffer.append(", ");//$NON-NLS-1$
        }
    }
    this.fBuffer.append(";");//$NON-NLS-1$
    return false;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(FieldDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }//ww w  .  jav  a 2  s .c om
    printIndent();
    if (node.getAST().apiLevel() >= AST.JLS3) {
        printModifiers(node.modifiers());
    }
    node.getType().accept(this);
    this.buffer.append(" ");//$NON-NLS-1$
    for (Iterator it = node.fragments().iterator(); it.hasNext();) {
        VariableDeclarationFragment f = (VariableDeclarationFragment) it.next();
        f.accept(this);
        if (it.hasNext()) {
            this.buffer.append(", ");//$NON-NLS-1$
        }
    }
    this.buffer.append(";\n");//$NON-NLS-1$
    return false;
}

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

License:Open Source License

@Override
public void setValueFeatureValue(Object element, Object feature, Object value) {
    if (CodeSyncPackage.eINSTANCE.getCodeSyncElement_Name().equals(feature)) {
        FieldDeclaration field = getFieldDeclaration(element);
        String name = (String) value;
        VariableDeclaration var = (VariableDeclaration) field.fragments().get(0);
        var.setName(field.getAST().newSimpleName(name));
    }//  w ww .  jav a 2s  .c o  m
    if (AstCacheCodePackage.eINSTANCE.getTypedElement_Type().equals(feature)) {
        FieldDeclaration field = getFieldDeclaration(element);
        Type type = getTypeFromString(field.getAST(), (String) value);
        field.setType(type);
    }
    if (AstCacheCodePackage.eINSTANCE.getAttribute_Initializer().equals(feature)) {
        VariableDeclaration var = (VariableDeclaration) getFieldDeclaration(element).fragments().get(0);
        var.setInitializer(getExpressionFromString(var.getAST(), (String) value));
    }
    super.setValueFeatureValue(element, feature, value);
}

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

License:Open Source License

@Override
protected void setASTFeatureValue(EStructuralFeature feature, FieldDeclaration astElement, Object value)
        throws CodeSyncException {
    if (astElement == null)
        throw new IllegalArgumentException("astElement null ");
    AST ast = astElement.getAST();
    switch (feature.getFeatureID()) {
    case UMLPackage.NAMED_ELEMENT__NAME:
        if (astElement.fragments().size() == 0)
            throw new IllegalArgumentException("bad assumption size always >0");
        if (value == null)
            throw new IllegalArgumentException("setting name to null value ");
        ((VariableDeclarationFragment) astElement.fragments().get(0))
                .setName(ast.newSimpleName((String) value));
        break;//from w  ww .  jav a 2s. c  om
    case UMLPackage.TYPED_ELEMENT__TYPE:
        String newType = value != null ? ((Type) value).getName() : null;
        if (newType == null)
            throw new IllegalArgumentException("Field has type null: " + astElement);
        parentForwardJavaClass_OwnedFields.parentForwardJavaType.parentForwardJavaSrcDir_Files
                .createImportDeclarationIfNeeded((Type) value);
        astElement.setType(JavaSyncUtils.getJavaTypeFromString(ast, newType, true));
        break;
    case UMLPackage.PROPERTY__DEFAULT_VALUE:
        String newDefaultValue = (LiteralString) value == null ? null : ((LiteralString) value).getValue();
        Expression e = null;
        if (newDefaultValue != null && !newDefaultValue.equals("")) {
            ASTParser expressionParser = ASTParser.newParser(AST.JLS3);
            expressionParser.setKind(ASTParser.K_EXPRESSION);
            expressionParser.setSource(newDefaultValue.toCharArray());
            e = (Expression) expressionParser.createAST(null);
            e = (Expression) ASTNode.copySubtree(ast, e);
        }
        ((VariableDeclarationFragment) astElement.fragments().get(0)).setInitializer(e);
        break;
    default:
        super.setASTFeatureValue(feature, astElement, value);
    }
}

From source file:io.spring.boot.development.eclipse.resolution.ConfigurationClassConstructorInjectionMarkerResolution.java

License:Open Source License

@SuppressWarnings("unchecked")
private SingleVariableDeclaration createArgument(CompilationUnit compilationUnit,
        AutowiredField autowiredField) {
    FieldDeclaration field = autowiredField.getField();
    AST ast = field.getAST();
    SingleVariableDeclaration argument = ast.newSingleVariableDeclaration();
    Annotation autowired = autowiredField.getAutowired();
    boolean optionalDependency = dependencyIsOptional(autowired);
    String name = autowiredField.getName();
    if (optionalDependency) {
        ParameterizedType argumentType = ast
                .newParameterizedType(ast.newSimpleType(ast.newSimpleName("ObjectProvider")));
        argumentType.typeArguments().add(ASTNode.copySubtree(ast, autowiredField.getField().getType()));
        argument.setType(argumentType);/*  w ww  . j a  v  a  2 s  . c o m*/
        addImportIfNecessary(compilationUnit, "org.springframework.beans.factory.ObjectProvider");
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        methodInvocation.setExpression(ast.newName(name));
        methodInvocation.setName(ast.newSimpleName("getIfAvailable"));
        name += "Provider";
    } else {
        argument.setType((Type) ASTNode.copySubtree(ast, autowiredField.getField().getType()));
    }
    argument.setName(ast.newSimpleName(name));
    return argument;
}

From source file:nl.han.ica.core.issue.solver.EncapsulateFieldSolver.java

@SuppressWarnings("unchecked")
private void refactorFieldDeclaration(FieldDeclaration fieldDeclaration) {
    SourceFile sourceFile = getSourceFileFromNode(fieldDeclaration);
    IDocument document = getSourceFileDocument(sourceFile);
    Delta delta = createDelta(sourceFile, document);

    ASTRewrite rewrite = ASTRewrite.create(fieldDeclaration.getAST());

    FieldDeclaration fieldDeclarationCopy = (FieldDeclaration) ASTNode.copySubtree(fieldDeclaration.getAST(),
            fieldDeclaration);/*  ww w.j a v a  2 s.c o m*/

    int annotationsSize = ASTUtil.getAnnotationsSize(
            ((VariableDeclarationFragment) fieldDeclaration.fragments().get(0)).resolveBinding());

    int modifiers = fieldDeclaration.getModifiers();

    if (Modifier.isPublic(modifiers)) {
        fieldDeclarationCopy.modifiers().remove(annotationsSize);
    }

    fieldDeclarationCopy.modifiers().addAll(annotationsSize,
            fieldDeclaration.getAST().newModifiers(Modifier.PRIVATE));
    getter = createGetter(fieldDeclaration.getAST(), fieldDeclarationCopy,
            solution.getParameters().get(PARAMETER_GETTER_NAME));
    setter = createSetter(fieldDeclaration.getAST(), fieldDeclarationCopy,
            solution.getParameters().get(PARAMETER_SETTER_NAME));

    ListRewrite listRewrite = rewrite.getListRewrite(ASTUtil.parent(TypeDeclaration.class, fieldDeclaration),
            TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
    listRewrite.insertLast(getter, null);
    listRewrite.insertLast(setter, null);

    rewrite.replace(fieldDeclaration, fieldDeclarationCopy, null);
    TextEdit textEdit = rewrite.rewriteAST(document, JavaCore.getOptions());

    try {
        textEdit.apply(document);
    } catch (MalformedTreeException | BadLocationException e) {
        log.fatal(e);
    }
    delta.setAfter(document.get());
}

From source file:org.codemucker.jmutate.ast.JAstFlattener.java

License:Open Source License

public boolean visit(FieldDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }//  w ww . j ava 2 s.  c o m
    printIndent();
    if (node.getAST().apiLevel() == JLS2) {
        printModifiers(node.getModifiers());
    }
    if (node.getAST().apiLevel() >= JLS3) {
        printModifiers(node.modifiers());
    }
    node.getType().accept(this);
    this.buffer.append(" ");//$NON-NLS-1$
    for (Iterator it = node.fragments().iterator(); it.hasNext();) {
        VariableDeclarationFragment f = (VariableDeclarationFragment) it.next();
        f.accept(this);
        if (it.hasNext()) {
            this.buffer.append(", ");//$NON-NLS-1$
        }
    }
    this.buffer.append(";\n");//$NON-NLS-1$
    return false;
}

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

License:Open Source License

@Override
public void setValueFeatureValue(Object element, Object feature, Object value) {
    if (CoreConstants.NAME.equals(feature)) {
        FieldDeclaration field = getFieldDeclaration(element);
        String name = (String) value;
        VariableDeclaration var = (VariableDeclaration) field.fragments().get(0);
        var.setName(field.getAST().newSimpleName(name));
    } else if (CodeSyncCodeJavaConstants.TYPED_ELEMENT_TYPE.equals(feature)) {
        FieldDeclaration field = getFieldDeclaration(element);
        Type type = getTypeFromString(field.getAST(), (String) value);
        field.setType(type);/*from w  w w.  ja  v  a2s .c  o  m*/
    } else if (CodeSyncCodeJavaConstants.ATTRIBUTE_INITIALIZER.equals(feature)) {
        VariableDeclaration var = (VariableDeclaration) getFieldDeclaration(element).fragments().get(0);
        var.setInitializer(getExpressionFromString(var.getAST(), (String) value));
    }
    super.setValueFeatureValue(element, feature, value);
}

From source file:org.jastemf.refactorings.RefactoringManager.java

License:BSD License

/**
 * <p>//from w ww  .  ja va  2 s. c o m
 * Execute adaptations of the <tt>ASTNode$State</tt> class JastAdd
 * generates. Such adaptations are required because generated AST classes
 * are moved throughout the integration process into different packages,
 * which results in shadowed <tt>ASTNode$State</tt> fields and methods.
 * </p>
 * The following adaptations are conducted:
 * <ul>
 * <li>Protected fields are made public. Currently these are
 * ASTNode$State.duringInterpretation, ASTNode$State.CircularValue.value,
 * ASTNode$State.CircularValue.visited</li>
 * <li>Additionally: See {@link BasicJDTASTVisitor}</li>
 * </ul>
 * 
 * @param context
 *            The current integration context.
 * @throws JastEMFException
 *             Thrown, iff an IO error occurs or an adaptation cannot be
 *             applied.
 */
public static void performASTNodeStateAdaptations(IIntegrationContext context) throws JastEMFException {
    final IFile compilationUnitFile = IOSupport.getFile(context.astfolder(), "ASTNode$State.java");
    final CompilationUnit compilationUnit = JDTSupport.loadCompilationUnit(compilationUnitFile);
    compilationUnit.recordModifications();
    ASTVisitor visitor = new BasicJDTASTVisitor(context) {
        @SuppressWarnings("unchecked")
        public boolean visit(FieldDeclaration decl) {
            super.visit(decl);
            AST ast = decl.getAST();
            // making protected fields duringInterpretation,value,visited
            // and all others (!) public

            TypeDeclaration typeDecl = (TypeDeclaration) decl.getParent();
            String ident = typeDecl.getName().getIdentifier();
            if (("ASTNode$State").equals(ident) || ("CircularValue").equals(ident)) {
                ListIterator<?> it = decl.modifiers().listIterator();
                boolean isPublicOrPrivate = false;
                while (it.hasNext()) {
                    Modifier modifier = (Modifier) it.next();

                    if (modifier.isPublic() || modifier.isPrivate()) {
                        isPublicOrPrivate = true;
                        break;
                    }
                    if (modifier.isProtected()) {
                        modifier.setKeyword(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
                        isPublicOrPrivate = true;
                        break;
                    }
                }
                if (!isPublicOrPrivate) {
                    Modifier newModifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
                    decl.modifiers().add(newModifier);
                }
            }
            return false;
        }

        @SuppressWarnings("unchecked")
        public boolean visit(TypeDeclaration decl) {
            if ("IdentityHashSet".equals(decl.getName().getIdentifier())) {
                Modifier modifier = JDTSupport.findModifier(decl, ModifierKeyword.PROTECTED_KEYWORD);
                if (modifier != null) {
                    modifier.setKeyword(ModifierKeyword.PUBLIC_KEYWORD);
                } else {
                    Modifier newModifier = decl.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD);
                    decl.modifiers().add(newModifier);
                }

            }
            return true;
        }
    };

    //compilationUnit.accept(visitor);

    JDTSupport.applyRewritesAndSave(compilationUnit, compilationUnitFile);
}

From source file:refactorer.Refactorer.java

License:Apache License

@SuppressWarnings("unchecked")
protected void processFieldDeclaration(FieldDeclaration fieldDeclaration) {
    for (VariableDeclarationFragment fragment : (List<VariableDeclarationFragment>) fieldDeclaration
            .fragments()) {//  www . j av  a2 s . co  m
        processExpression(fragment.getInitializer());

        ITypeBinding binding = fieldDeclaration.getType().resolveBinding();
        if (binding != null) {
            variables.add(binding, fragment.getName().getFullyQualifiedName());
        }
    }

    ITypeBinding type = fieldDeclaration.getType().resolveBinding();
    if (type != null) {
        String typeName = type.getQualifiedName();
        AST ast = fieldDeclaration.getAST();
        if ("javax.media.opengl.GL".equals(typeName)) {
            Name name = ast.newName("GL2");
            fieldDeclaration.setType(ast.newSimpleType(name));
            addImportIfRequired("javax.media.opengl.GL2");
        } else if ("javax.media.opengl.GLCapabilities".equals(typeName)) {
            Name name = ast.newName("GLCapabilitiesImmutable");
            fieldDeclaration.setType(ast.newSimpleType(name));
            addImportIfRequired("javax.media.opengl.GLCapabilitiesImmutable");
        } else if ("javax.media.opengl.GLCanvas".equals(typeName)) {
            addImportIfRequired("javax.media.opengl.awt.GLCanvas");
        }
    }

}