Example usage for org.eclipse.jdt.core IMethod getDefaultValue

List of usage examples for org.eclipse.jdt.core IMethod getDefaultValue

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IMethod getDefaultValue.

Prototype

IMemberValuePair getDefaultValue() throws JavaModelException;

Source Link

Document

Returns a IMemberValuePair member value pair representing the default value of this method if any, or null if this method's parent is not an annotation type, or else if this method does not have a default value.

Usage

From source file:edu.cmu.cs.crystal.annotations.AnnotationDatabase.java

License:Open Source License

/**
 * Returns the default annotation values for the given annotation. 
 * @throws JavaModelException /*from w ww .  j  av  a  2s . c om*/
 */
private List<IMemberValuePair> findAnnotationDefaults(IType anno_type) throws JavaModelException {
    IMethod[] properties = anno_type.getMethods();
    List<IMemberValuePair> result = new ArrayList<IMemberValuePair>();

    for (IMethod property : properties) {
        IMemberValuePair default_val_ = property.getDefaultValue();
        if (default_val_ != null)
            result.add(default_val_);
    }

    return result;
}

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

License:Open Source License

protected AnnotationTypeMemberDeclaration visitAnnotationTypeMemberDeclaration(final IMethod method)
        throws JavaModelException {
    AnnotationTypeMemberDeclaration element = getFactory().createAnnotationTypeMemberDeclaration();
    initializeNode(element);//  w ww  .j  a va  2s.  c  o m

    element.setName(method.getElementName());
    element.setType(getRefOnType(method.getReturnType()));

    // the default value of this annotation member
    IMemberValuePair defaultValue = method.getDefaultValue();
    if (defaultValue != null) {
        Expression result = manageMemberValuePair(defaultValue);
        element.setDefault(result);
    }

    Modifier m = getFactory().createModifier();
    m.setBodyDeclaration(element);
    element.setModifier(m);
    manageModifier(m, method.getFlags(), method);

    ClassFileParserUtils.manageBindingDeclaration(element, method, this);

    return element;
}

From source file:org.hawkinssoftware.rns.analysis.compile.util.RNSBuildAnalyzerUtils.java

License:Open Source License

public static boolean getDefaultBooleanValue(IType annotatedType, IAnnotation annotation, String methodName)
        throws JavaModelException {
    IType annotationType = annotation.getJavaProject().findType(annotation.getElementName());
    if (annotationType == null) {
        String[][] typename = annotatedType.resolveType(annotation.getElementName());
        annotationType = annotation.getJavaProject().findType(typename[0][0], typename[0][1]);
    }/*from www . java2  s .  c om*/

    IMethod method = annotationType.getMethod(methodName, new String[0]);
    if (method == null) {
        return false;
    }

    Object value = method.getDefaultValue().getValue();
    if (value instanceof Boolean) {
        return (Boolean) value;
    } else {
        Log.out(Tag.WARNING,
                "Attempt to get the default boolean value from annotation %s, which has default value of type %s.",
                annotation.getElementName(), value == null ? "null" : value.getClass().getName());
        return false;
    }
}

From source file:org.jboss.tools.cdi.internal.core.impl.CDIProject.java

License:Open Source License

private static String getAnnotationDeclarationKey(IAnnotationDeclaration d, Collection<IMethod> ignoredMembers)
        throws CoreException {
    Collection<IMethod> nb = ignoredMembers == null ? new ArrayList<IMethod>() : ignoredMembers;
    IType type = d.getType();//from w  ww .j  ava  2s  .  com
    StringBuffer result = new StringBuffer();
    result.append(d.getTypeName());
    if (CDIConstants.NAMED_QUALIFIER_TYPE_NAME.equals(d.getTypeName())) {
        //Declared name is excluded from comparison; names should be compared by invoking getName() method.
        return result.toString();
    }
    IMethod[] ms = type.getMethods();
    if (ms.length > 0) {
        TreeMap<String, String> values = new TreeMap<String, String>();
        IMemberValuePair[] ps = d.getMemberValuePairs();
        if (ps != null)
            for (IMemberValuePair p : ps) {
                String n = p.getMemberName();
                Object o = d.getMemberValue(n);
                values.put(n, o == null ? "" : o.toString());

            }
        for (IMethod m : ms) {
            String n = m.getElementName();
            if (nb.contains(m)) {
                values.remove(n);
            } else if (!values.containsKey(n)) {
                IMemberValuePair p = m.getDefaultValue();
                if (p != null) {
                    n = p.getMemberName();
                    Object o = p.getValue();
                    // Default value can be null since JDT does not computes complex values
                    // E.g. values (char)7 or (2 + 3) will be resolved to null.  
                    if (!values.containsKey(n) && o != null) {
                        values.put(n, o.toString());
                    }
                }
            }
        }
        for (String n : values.keySet()) {
            result.append(';').append(n).append('=').append(values.get(n));
        }
    }
    return result.toString();
}

From source file:org.jboss.tools.cdi.internal.core.refactoring.ValuedQualifier.java

License:Open Source License

public ValuedQualifier(IQualifier qualifier, IQualifierDeclaration declaration) {
    this.qualifier = qualifier;
    if (declaration != null) {
        // copy pairs from qualifier declaration
        for (IMemberValuePair mvp : declaration.getMemberValuePairs()) {
            Pair pair = new Pair();
            pair.name = mvp.getMemberName();
            pair.value = mvp.getValue();
            if (mvp.getValueKind() == IMemberValuePair.K_STRING) {
                pair.type = "String";
            } else if (mvp.getValueKind() == IMemberValuePair.K_CHAR) {
                pair.type = "char";
            } else if (mvp.getValueKind() == IMemberValuePair.K_CLASS) {
                pair.type = "Class";
            } else if (mvp.getValueKind() == IMemberValuePair.K_BOOLEAN) {
                pair.type = "boolean";
            } else if (mvp.getValueKind() == IMemberValuePair.K_BYTE) {
                pair.type = "byte";
            } else if (mvp.getValueKind() == IMemberValuePair.K_DOUBLE) {
                pair.type = "double";
            } else if (mvp.getValueKind() == IMemberValuePair.K_FLOAT) {
                pair.type = "float";
            } else if (mvp.getValueKind() == IMemberValuePair.K_INT) {
                pair.type = "int";
            } else if (mvp.getValueKind() == IMemberValuePair.K_LONG) {
                pair.type = "long";
            } else if (mvp.getValueKind() == IMemberValuePair.K_QUALIFIED_NAME) {
                pair.type = "name";
            } else if (mvp.getValueKind() == IMemberValuePair.K_SHORT) {
                pair.type = "short";
            } else if (mvp.getValueKind() == IMemberValuePair.K_SIMPLE_NAME) {
                pair.type = "name";
            }/*from   w w  w .  j  av a  2s  . c  o m*/
            pairs.add(pair);
        }
    } else {
        IType type = qualifier.getSourceType();
        try {
            if (type.isAnnotation()) {
                for (IMethod method : type.getMethods()) {
                    IMemberValuePair mvp = method.getDefaultValue();
                    Pair pair = new Pair();
                    pair.type = Signature.getSignatureSimpleName(method.getReturnType());
                    pair.name = method.getElementName();
                    if (mvp != null && mvp.getValue() != null) {
                        pair.value = mvp.getValue();
                        pair.required = false;
                    } else {
                        pair.required = true;
                        if (pair.type.equals("boolean")) {
                            pair.value = "false";
                        } else if (pair.type.equals("int") || pair.type.equals("short")
                                || pair.type.equals("long")) {
                            pair.value = "0";
                        } else if (pair.type.equals("float")) {
                            pair.value = "0";
                        } else if (pair.type.equals("double")) {
                            pair.value = "0.0";
                        } else if (pair.type.equals("char")) {
                            pair.value = ' ';
                        } else if (pair.type.equals("byte")) {
                            pair.value = "0";
                        } else if (pair.type.equals("String")) {
                            pair.value = "default";
                        } else {
                            pair.value = "String";
                        }
                    }
                    pairs.add(pair);
                }
            }
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.jboss.tools.common.java.impl.AnnotationDeclaration.java

License:Open Source License

private void loadDefaults() throws CoreException {
    IType type = getType();//  w w w. ja  v a2 s.  c o m
    if (type == null) {
        return;
    }
    IMethod[] ms = type.getMethods();
    for (IMethod m : ms) {
        String n = m.getElementName();
        IMemberValuePair p = m.getDefaultValue();
        if (p != null) {
            n = p.getMemberName();
            Object o = p.getValue();
            // Default value can be null since JDT does not computes complex values
            // E.g. values (char)7 or (2 + 3) will be resolved to null.
            if (o == null) {
                String expression = null;
                String s = ((ISourceReference) m).getSource();
                if (s != null) {
                    int i = s.indexOf("default");
                    int j = s.lastIndexOf(';');
                    if (i > 0 && j > i) {
                        expression = s.substring(i + 7, j).trim();
                    }
                }
                if (expression != null) {
                    ValueResolver r = new ValueResolver(m);
                    o = r.resolveExpression(expression);
                    r.dispose();
                }
            }
            if (o != null) {
                defaults = defaults.put(n, o);
            }
        }
    }
}

From source file:org.springframework.ide.eclipse.core.java.classreading.JdtAnnotationUtils.java

License:Open Source License

public static void processAnnotation(IAnnotation annotation, IType type,
        Map<String, Map<String, Object>> annotationMap) {
    Map<String, Object> attributesMap = new HashMap<String, Object>();
    String annotationName = null;
    if (type.isBinary()) {
        annotationName = annotation.getElementName();
    } else {/*w  w w  .j  a  v a 2s .  com*/
        annotationName = JdtUtils.resolveClassName(annotation.getElementName(), type);
    }
    IType annotationType = JdtUtils.getJavaType(type.getJavaProject().getProject(), annotationName);
    try {
        for (IMemberValuePair member : annotation.getMemberValuePairs()) {
            processAnnotation(member, annotationType, attributesMap, type);
        }

        for (IMethod annotationMethod : Introspector.getAllMethods(annotationType)) {
            if (annotationMethod.getDefaultValue() != null) {
                IMemberValuePair member = annotationMethod.getDefaultValue();
                if (!attributesMap.containsKey(member.getMemberName())) {
                    processAnnotation(member, annotationType, attributesMap, type);
                }
            }
        }

        annotationMap.put(annotationName, attributesMap);
    } catch (JavaModelException e) {
        throw new JdtMetadataReaderException(e);
    }
}