Example usage for org.eclipse.jdt.core.dom BodyDeclaration getStructuralProperty

List of usage examples for org.eclipse.jdt.core.dom BodyDeclaration getStructuralProperty

Introduction

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

Prototype

public final Object getStructuralProperty(StructuralPropertyDescriptor property) 

Source Link

Document

Returns the value of the given structural property for this node.

Usage

From source file:com.google.gdt.eclipse.core.JavaASTUtils.java

License:Open Source License

/**
 * Finds an annotation of the given type (as a fully-qualified name) on a
 * declaration (type, method, field, etc.). If no such annotation exists,
 * returns <code>null</code>.
 */// w w  w .  j a v a2  s.co m
@SuppressWarnings("unchecked")
public static Annotation findAnnotation(BodyDeclaration decl, String annotationTypeName) {
    if (annotationTypeName == null) {
        throw new IllegalArgumentException("annotationTypeName cannot be null");
    }

    List<ASTNode> modifiers = (List<ASTNode>) decl.getStructuralProperty(decl.getModifiersProperty());
    for (ASTNode modifier : modifiers) {
        if (modifier instanceof Annotation) {
            Annotation annotation = (Annotation) modifier;
            String typeName = getAnnotationTypeName(annotation);
            if (annotationTypeName.equals(typeName)) {
                return annotation;
            }
        }
    }
    return null;
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.AstUtils.java

License:Open Source License

/**
 * Returns the offset and length for the {@link MethodDeclaration}, optionally
 * including any modifiers (including annotations and visibility).
 * /*from   ww  w  .  java 2  s  . c o  m*/
 * @param declaration
 * @param includingModifiers - whether to include modifiers in the offset and length.
 */
public final static OffsetAndLength calculateOffset(BodyDeclaration declaration, boolean includingModifiers) {
    if (!(declaration instanceof MethodDeclaration) && !(declaration instanceof TypeDeclaration)) {
        throw new IllegalArgumentException("declaration must be TypeDeclaration or MethodDeclaration");
    }
    SimpleName methodName = nameFor(declaration);
    if (!includingModifiers) {
        return new OffsetAndLength(methodName.getStartPosition(), methodName.getLength());
    } else {
        // including modifiers
        int endOfMethodName = methodName.getStartPosition() + methodName.getLength();
        ChildListPropertyDescriptor modifiersProperty = declaration.getModifiersProperty();
        List<ASTNode> methodModifiers = Generics.asT(declaration.getStructuralProperty(modifiersProperty));
        if (methodModifiers.size() > 0) {
            int startOfModifiers = methodModifiers.get(0).getStartPosition();
            return new OffsetAndLength(startOfModifiers, endOfMethodName - startOfModifiers);
        }
        if (declaration instanceof MethodDeclaration) {
            Type returnType2 = ((MethodDeclaration) declaration).getReturnType2();
            if (returnType2 != null) {
                int startOfReturnType = returnType2.getStartPosition();
                return new OffsetAndLength(startOfReturnType, endOfMethodName - startOfReturnType);
            }
        }
        int startOfMethodName = declaration.getStartPosition();
        return new OffsetAndLength(startOfMethodName, endOfMethodName - startOfMethodName);
    }
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

@SuppressWarnings("unchecked")
public static <T extends Annotation> T addAnnotation(BodyDeclaration bodyDeclaration, T annotation) {
    List list = (List) bodyDeclaration.getStructuralProperty(bodyDeclaration.getModifiersProperty());
    list.add(annotationLocation(bodyDeclaration), annotation);
    return annotation;
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

public static void removeAnnotation(BodyDeclaration bodyDeclaration, Annotation annotation) {
    List list = (List) bodyDeclaration.getStructuralProperty(bodyDeclaration.getModifiersProperty());
    list.remove(annotation);//  w w  w.  ja v a2  s .co  m
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void removeAnnotationElement(BodyDeclaration bodyDeclaration, Annotation annotation,
        String elementName) {/*from   w  w w .jav  a 2  s  .c o m*/
    MemberValuePair valuePairToRemove = memberValuePair(annotation, elementName);

    if (valuePairToRemove != null) {
        if (((NormalAnnotation) annotation).values().size() == 1) {
            MarkerAnnotation newAnnotation = newMarkerAnnotation(bodyDeclaration.getAST(),
                    annotation.getTypeName().getFullyQualifiedName());

            List list = (List) bodyDeclaration.getStructuralProperty(bodyDeclaration.getModifiersProperty());
            int index = list.indexOf(annotation);
            list.set(index, newAnnotation);
        } else {
            removeValuePair((NormalAnnotation) annotation, valuePairToRemove);
        }
    }
}

From source file:com.halware.nakedide.eclipse.ext.annot.utils.dali.ASTTools.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void replaceAnnotation(BodyDeclaration bodyDeclaration, Annotation oldAnnotation,
        Annotation newAnnotation) {
    List list = (List) bodyDeclaration.getStructuralProperty(bodyDeclaration.getModifiersProperty());
    int index = list.indexOf(oldAnnotation);
    list.set(index, newAnnotation);//from  www. j a  v  a2  s .  c  o m
}

From source file:org.jboss.forge.parser.java.impl.JDTHelper.java

License:Open Source License

@SuppressWarnings("unchecked")
public static List<Type> getInterfaces(final BodyDeclaration dec) {
    return (List<Type>) dec.getStructuralProperty(TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
}

From source file:org.jboss.forge.roaster.model.impl.JDTHelper.java

License:Open Source License

@SuppressWarnings("unchecked")
public static List<Type> getInterfaces(final BodyDeclaration dec) {
    StructuralPropertyDescriptor desc;// w  w  w .  ja v a2 s.  co m
    if (dec instanceof EnumDeclaration) {
        desc = EnumDeclaration.SUPER_INTERFACE_TYPES_PROPERTY;
    } else {
        desc = TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY;
    }
    return (List<Type>) dec.getStructuralProperty(desc);
}