Search in sources :

Example 1 with TypeLocation

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation in project eclipse.jdt.ls by eclipse.

the class RedundantNullnessTypeAnnotationsFilter method createIfConfigured.

public static /* @Nullable */
RedundantNullnessTypeAnnotationsFilter createIfConfigured(/* @Nullable */
ASTNode node) {
    if (node == null) {
        return null;
    }
    final ASTNode root = node.getRoot();
    if (root instanceof CompilationUnit) {
        CompilationUnit compilationUnit = (CompilationUnit) root;
        IJavaElement javaElement = compilationUnit.getJavaElement();
        IJavaProject javaProject = javaElement == null ? null : javaElement.getJavaProject();
        if (javaProject != null) {
            if (JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, true))) {
                String nonNullAnnotationName = javaProject.getOption(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, true);
                String nullableAnnotationName = javaProject.getOption(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, true);
                String nonNullByDefaultName = javaProject.getOption(JavaCore.COMPILER_NONNULL_BY_DEFAULT_ANNOTATION_NAME, true);
                if (nonNullAnnotationName == null || nullableAnnotationName == null || nonNullByDefaultName == null) {
                    return null;
                }
                EnumSet<TypeLocation> nonNullByDefaultLocations = determineNonNullByDefaultLocations(node, nonNullByDefaultName);
                return new RedundantNullnessTypeAnnotationsFilter(nonNullAnnotationName, nullableAnnotationName, nonNullByDefaultLocations);
            }
        }
    }
    return null;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) TypeLocation(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation) IJavaProject(org.eclipse.jdt.core.IJavaProject) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 2 with TypeLocation

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation in project eclipse.jdt.ls by eclipse.

the class RedundantNullnessTypeAnnotationsFilter method determineNNBDValue.

private static EnumSet<TypeLocation> determineNNBDValue(IAnnotationBinding annot) {
    EnumSet<TypeLocation> result = EnumSet.noneOf(TypeLocation.class);
    IMemberValuePairBinding[] pairs = annot.getAllMemberValuePairs();
    for (final IMemberValuePairBinding pair : pairs) {
        if (pair.getKey() == null || pair.getKey().equals("value")) {
            // $NON-NLS-1$
            Object value = pair.getValue();
            if (value instanceof Object[]) {
                Object[] values = (Object[]) value;
                for (int k = 0; k < values.length; k++) {
                    if (values[k] instanceof IVariableBinding) {
                        String name = ((IVariableBinding) values[k]).getName();
                        try {
                            result.add(TypeLocation.valueOf(name));
                        } catch (IllegalArgumentException e) {
                        // ignore
                        }
                    }
                }
            } else if (value instanceof IVariableBinding) {
                String name = ((IVariableBinding) value).getName();
                try {
                    result.add(TypeLocation.valueOf(name));
                } catch (IllegalArgumentException e) {
                // ignore
                }
            }
        }
    }
    return result;
}
Also used : TypeLocation(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation) IMemberValuePairBinding(org.eclipse.jdt.core.dom.IMemberValuePairBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

TypeLocation (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.TypeLocation)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 IMemberValuePairBinding (org.eclipse.jdt.core.dom.IMemberValuePairBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1