Example usage for org.eclipse.jdt.core.dom SimpleType NAME_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom SimpleType NAME_PROPERTY

Introduction

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

Prototype

ChildPropertyDescriptor NAME_PROPERTY

To view the source code for org.eclipse.jdt.core.dom SimpleType NAME_PROPERTY.

Click Source Link

Document

The "name" structural property of this node type (child type: Name ).

Usage

From source file:org.eclipse.che.jdt.dom.ASTNodes.java

License:Open Source License

/**
 * For {@link Name} or {@link org.eclipse.jdt.core.dom.Type} nodes, returns the topmost {@link org.eclipse.jdt.core.dom.Type} node
 * that shares the same type binding as the given node.
 *
 * @param node/* w  w  w  .j a v a  2  s .c  o  m*/
 *         an ASTNode
 * @return the normalized {@link org.eclipse.jdt.core.dom.Type} node or the original node
 */
public static ASTNode getNormalizedNode(ASTNode node) {
    ASTNode current = node;
    // normalize name
    if (QualifiedName.NAME_PROPERTY.equals(current.getLocationInParent())) {
        current = current.getParent();
    }
    // normalize type
    if (QualifiedType.NAME_PROPERTY.equals(current.getLocationInParent())
            || SimpleType.NAME_PROPERTY.equals(current.getLocationInParent())
            || NameQualifiedType.NAME_PROPERTY.equals(current.getLocationInParent())) {
        current = current.getParent();
    }
    // normalize parameterized types
    if (ParameterizedType.TYPE_PROPERTY.equals(current.getLocationInParent())) {
        current = current.getParent();
    }
    return current;
}

From source file:org.eclipse.objectteams.otdt.debug.ui.internal.actions.OTToggleBreakpointAdapter.java

License:Open Source License

/**
 * Returns the binary name for the {@link IType} derived from its {@link ITypeBinding}.
 * <br><br>//from  w w w . j  a  v a2s .co m
 * If the {@link ITypeBinding} cannot be derived this method falls back to calling
 * {@link #createQualifiedTypeName(IType)} to try and compose the type name.
 * @param type
 * @return the binary name for the given {@link IType}
 * @since 3.6
 */
String getQualifiedName(IType type) throws JavaModelException {
    IJavaProject project = type.getJavaProject();
    if (project != null && project.isOnClasspath(type) && needsBindings(type)) {
        CompilationUnit cuNode = parseCompilationUnit(type.getTypeRoot());
        ISourceRange nameRange = type.getNameRange();
        if (SourceRange.isAvailable(nameRange)) {
            ASTNode node = NodeFinder.perform(cuNode, nameRange);
            if (node instanceof SimpleName) {
                IBinding binding;
                if (node.getLocationInParent() == SimpleType.NAME_PROPERTY
                        && node.getParent().getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
                    binding = ((ClassInstanceCreation) node.getParent().getParent()).resolveTypeBinding();
                } else {
                    binding = ((SimpleName) node).resolveBinding();
                }
                if (binding instanceof ITypeBinding) {
                    String name = ((ITypeBinding) binding).getBinaryName();
                    if (name != null) {
                        return name;
                    }
                }
            }
        }
    }
    return createQualifiedTypeName(type);
}

From source file:org.eclipse.scout.sdk.saml.importer.internal.jdt.imports.OrganizeImportsHelper.java

License:Open Source License

public static ASTNode getNormalizedNode(ASTNode node) {
    ASTNode current = node;/*from   ww w.  j a va2  s  .  c  o m*/
    // normalize name
    if (QualifiedName.NAME_PROPERTY.equals(current.getLocationInParent())) {
        current = current.getParent();
    }
    // normalize type
    if (QualifiedType.NAME_PROPERTY.equals(current.getLocationInParent())
            || SimpleType.NAME_PROPERTY.equals(current.getLocationInParent())) {
        current = current.getParent();
    }
    // normalize parameterized types
    if (ParameterizedType.TYPE_PROPERTY.equals(current.getLocationInParent())) {
        current = current.getParent();
    }
    return current;
}

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

License:Open Source License

/**
 * Examples://w  ww. ja  va 2s.  c  om
 * 
 * <pre>
  * SWT.NONE = org.eclipse.swt.SWT.NONE
  * new JButton() = new javax.swing.JButton()
  * </pre>
 * 
 * @param theNode
 *          the {@link ASTNode} to get the source.
 * @param transformer
 *          the {@link Function} that can participate in node to source transformation by
 *          providing alternative source for some nodes. Can be <code>null</code>, in not
 *          additional transformation required. If transformer returns not <code>null</code>, we
 *          use it instead of its original source; if <code>null</code> - we continue with
 *          original source.
 * 
 * @return the source of {@link ASTNode} in "external form", i.e. with fully qualified types.
 */
@SuppressWarnings("restriction")
public String getExternalSource(final ASTNode theNode, final Function<ASTNode, String> transformer) {
    final StringBuffer buffer = new StringBuffer(getSource(theNode));
    // remember positions for all nodes
    final Map<ASTNode, Integer> nodePositions = Maps.newHashMap();
    theNode.accept(new ASTVisitor() {
        @Override
        public void postVisit(ASTNode _node) {
            nodePositions.put(_node, _node.getStartPosition());
        }
    });
    // replace "name" with "qualified name"
    theNode.accept(new org.eclipse.jdt.internal.corext.dom.GenericVisitor() {
        @Override
        protected boolean visitNode(ASTNode node) {
            if (transformer != null) {
                String source = transformer.apply(node);
                if (source != null) {
                    replace(node, source);
                    return false;
                }
            }
            return true;
        }

        @Override
        public void endVisit(SimpleName name) {
            if (!AstNodeUtils.isVariable(name)) {
                StructuralPropertyDescriptor location = name.getLocationInParent();
                if (location == SimpleType.NAME_PROPERTY || location == QualifiedName.QUALIFIER_PROPERTY
                        || location == ClassInstanceCreation.NAME_PROPERTY
                        || location == MethodInvocation.EXPRESSION_PROPERTY) {
                    String fullyQualifiedName = AstNodeUtils.getFullyQualifiedName(name, false);
                    replace(name, fullyQualifiedName);
                }
            }
        }

        /**
         * Replace given ASTNode with different source, with updating positions for other nodes.
         */
        private void replace(ASTNode node, String newSource) {
            int nodePosition = nodePositions.get(node);
            // update source
            {
                int sourceStart = nodePosition - theNode.getStartPosition();
                int sourceEnd = sourceStart + node.getLength();
                buffer.replace(sourceStart, sourceEnd, newSource);
            }
            // update positions for nodes
            int lengthDelta = newSource.length() - node.getLength();
            for (Map.Entry<ASTNode, Integer> entry : nodePositions.entrySet()) {
                Integer position = entry.getValue();
                if (position > nodePosition) {
                    entry.setValue(position + lengthDelta);
                }
            }
        }
    });
    // OK, we have updated source
    return buffer.toString();
}

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

License:Open Source License

/**
 * @return <code>true</code> if given {@link ASTNode} is single variable {@link SimpleName} or
 *         {@link FieldAccess} like "this.fieldName".
 *///from w w w .  j a  va 2 s .  c  o  m
public static boolean isVariable(ASTNode variable) {
    // FieldAccess
    if (variable instanceof FieldAccess) {
        FieldAccess fieldAccess = (FieldAccess) variable;
        return fieldAccess.getExpression() instanceof ThisExpression;
    }
    // SimpleName
    if (variable instanceof SimpleName) {
        StructuralPropertyDescriptor locationInParent = variable.getLocationInParent();
        if (locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SimpleType.NAME_PROPERTY
                || locationInParent == FieldAccess.NAME_PROPERTY
                || locationInParent == QualifiedName.NAME_PROPERTY
                || locationInParent == MethodDeclaration.NAME_PROPERTY
                || locationInParent == TypeDeclaration.NAME_PROPERTY) {
            return false;
        }
        // variable has binding
        return getVariableBinding(variable) != null;
    }
    // unknown ASTNode
    return false;
}

From source file:org.moe.natjgen.ClassEditor.java

License:Apache License

@SuppressWarnings("unchecked")
public void setInterfaces(String... interfaces) throws GeneratorException {
    editLock();//from  www .  j  a v a2s. c om

    ListRewrite ilrw = getRewrite().getListRewrite(classDecl, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);

    int idx = 0;
    final int count = interfaces == null ? 0 : interfaces.length;
    for (Type it : (List<Type>) ilrw.getRewrittenList()) {
        if (idx < count) {
            Name name = (Name) getRewrite().get(it, SimpleType.NAME_PROPERTY);
            if (!name.equals(interfaces[idx])) {
                ilrw.replace(it, getAST().newSimpleType(getAST().newName(interfaces[idx])), getEditGroup());
            }
            ++idx;
        } else {
            ilrw.remove(it, getEditGroup());
        }
    }
    for (; idx < count; ++idx) {
        ilrw.insertLast(getAST().newSimpleType(getAST().newName(interfaces[idx])), getEditGroup());
    }
}

From source file:org.moe.natjgen.EditContext.java

License:Apache License

protected void setSMATypeValue(Annotation annotation, String type) throws GeneratorException {
    editLock();/*  w w  w.  j a  v  a 2  s  .c om*/

    if (annotation == null) {
        throw new IllegalArgumentException("'annotation' argument cannot be null!");
    }
    if (type == null || type.length() == 0) {
        throw new IllegalArgumentException("'type' argument must have a valid value!");
    }

    TypeLiteral literal = removeOnTypeMismatch(
            getRewrite().get(annotation, SingleMemberAnnotation.VALUE_PROPERTY), TypeLiteral.class);
    if (literal == null) {
        literal = getAST().newTypeLiteral();
        getRewrite().set(annotation, SingleMemberAnnotation.VALUE_PROPERTY, literal, getEditGroup());
    }

    SimpleType value_type = removeOnTypeMismatch(literal.getType(), SimpleType.class);

    if (value_type == null) {
        getRewrite().set(literal, TypeLiteral.TYPE_PROPERTY, getAST().newSimpleType(getAST().newName(type)),
                getEditGroup());
    } else {
        if (value_type.getName() == null || !value_type.getName().equals(type)) {
            getRewrite().set(value_type, SimpleType.NAME_PROPERTY, getAST().newName(type), getEditGroup());
        }
    }
}

From source file:org.moe.natjgen.MethodEditor.java

License:Apache License

private boolean isSimilarToSimpleType(Type natjType, org.eclipse.jdt.core.dom.Type domType) {
    if (natjType.isPrimitive() || natjType.isVoid()) {
        return false;
    }//from   w  w w.  j a  va2s.c o m

    Name simplename = (Name) getRewrite().get(domType, SimpleType.NAME_PROPERTY);
    return natjType.isSimilarTo(simplename.getFullyQualifiedName());
}