Search in sources :

Example 6 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class MemberVisibilityAdjustor method thresholdTypeToType.

/**
	 * Returns the visibility threshold from a type to another type.
	 *
	 * @param referencing the referencing type
	 * @param referenced the referenced type
	 * @param monitor the progress monitor to use
	 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
	 * @throws JavaModelException if the java elements could not be accessed
	 */
private ModifierKeyword thresholdTypeToType(final IType referencing, final IType referenced, final IProgressMonitor monitor) throws JavaModelException {
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    final ICompilationUnit referencedUnit = referenced.getCompilationUnit();
    if (referencing.equals(referenced.getDeclaringType()))
        keyword = ModifierKeyword.PRIVATE_KEYWORD;
    else {
        final ITypeHierarchy hierarchy = getTypeHierarchy(referencing, new SubProgressMonitor(monitor, 1));
        final IType[] types = hierarchy.getSupertypes(referencing);
        IType superType = null;
        for (int index = 0; index < types.length; index++) {
            superType = types[index];
            if (superType.equals(referenced)) {
                keyword = null;
                return keyword;
            }
        }
    }
    final ICompilationUnit typeUnit = referencing.getCompilationUnit();
    if (referencedUnit != null && referencedUnit.equals(typeUnit)) {
        if (referenced.getDeclaringType() != null)
            keyword = null;
        else
            keyword = ModifierKeyword.PRIVATE_KEYWORD;
    } else if (referencedUnit != null && typeUnit != null && referencedUnit.getParent().equals(typeUnit.getParent()))
        keyword = null;
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 7 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project flux by eclipse.

the class ModifierCorrectionSubProcessor method findVisibilityModifier.

private static Modifier findVisibilityModifier(List<IExtendedModifier> modifiers) {
    for (int i = 0; i < modifiers.size(); i++) {
        IExtendedModifier curr = modifiers.get(i);
        if (curr instanceof Modifier) {
            Modifier modifier = (Modifier) curr;
            ModifierKeyword keyword = modifier.getKeyword();
            if (keyword == ModifierKeyword.PUBLIC_KEYWORD || keyword == ModifierKeyword.PROTECTED_KEYWORD || keyword == ModifierKeyword.PRIVATE_KEYWORD) {
                return modifier;
            }
        }
    }
    return null;
}
Also used : ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 8 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project lombok by rzwitserloot.

the class PatchValEclipse method addFinalAndValAnnotationToModifierList.

public static void addFinalAndValAnnotationToModifierList(Object converter, List<IExtendedModifier> modifiers, AST ast, LocalDeclaration in) {
    // First check that 'in' has the final flag on, and a @val / @lombok.val annotation.
    if ((in.modifiers & ClassFileConstants.AccFinal) == 0)
        return;
    if (in.annotations == null)
        return;
    boolean found = false;
    Annotation valAnnotation = null;
    for (Annotation ann : in.annotations) {
        if (couldBeVal(ann.type)) {
            found = true;
            valAnnotation = ann;
            break;
        }
    }
    if (!found)
        return;
    // This is null only if the project is 1.4 or less. Lombok doesn't work in that.
    if (modifiers == null)
        return;
    boolean finalIsPresent = false;
    boolean valIsPresent = false;
    for (Object present : modifiers) {
        if (present instanceof Modifier) {
            ModifierKeyword keyword = ((Modifier) present).getKeyword();
            if (keyword == null)
                continue;
            if (keyword.toFlagValue() == Modifier.FINAL)
                finalIsPresent = true;
        }
        if (present instanceof org.eclipse.jdt.core.dom.Annotation) {
            Name typeName = ((org.eclipse.jdt.core.dom.Annotation) present).getTypeName();
            if (typeName != null) {
                String fullyQualifiedName = typeName.getFullyQualifiedName();
                if ("val".equals(fullyQualifiedName) || "lombok.val".equals(fullyQualifiedName)) {
                    valIsPresent = true;
                }
            }
        }
    }
    if (!finalIsPresent) {
        modifiers.add(createModifier(ast, ModifierKeyword.FINAL_KEYWORD, valAnnotation.sourceStart, valAnnotation.sourceEnd));
    }
    if (!valIsPresent) {
        MarkerAnnotation newAnnotation = createValAnnotation(ast, valAnnotation, valAnnotation.sourceStart, valAnnotation.sourceEnd);
        try {
            Reflection.astConverterRecordNodes.invoke(converter, newAnnotation, valAnnotation);
            Reflection.astConverterRecordNodes.invoke(converter, newAnnotation.getTypeName(), valAnnotation.type);
        } catch (IllegalAccessException e) {
            throw Lombok.sneakyThrow(e);
        } catch (InvocationTargetException e) {
            throw Lombok.sneakyThrow(e.getCause());
        }
        modifiers.add(newAnnotation);
    }
}
Also used : MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 9 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class MemberVisibilityAdjustor method needsVisibilityAdjustments.

/**
	 * Does the specified member need further visibility adjustment?
	 *
	 * @param member the member to test
	 * @param threshold the visibility threshold to test for
	 * @param adjustments the map of members to visibility adjustments
	 * @return <code>true</code> if the member needs further adjustment, <code>false</code> otherwise
	 */
public static boolean needsVisibilityAdjustments(final IMember member, final int threshold, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments) {
    Assert.isNotNull(member);
    Assert.isTrue(isVisibilityModifier(threshold));
    Assert.isNotNull(adjustments);
    final IncomingMemberVisibilityAdjustment adjustment = adjustments.get(member);
    if (adjustment != null) {
        final ModifierKeyword keyword = adjustment.getKeyword();
        return hasLowerVisibility(keyword == null ? Modifier.NONE : keyword.toFlagValue(), threshold);
    }
    return true;
}
Also used : ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)

Example 10 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class MemberVisibilityAdjustor method thresholdTypeToMethod.

/**
	 * Returns the visibility threshold from a type to a method.
	 *
	 * @param referencing the referencing type
	 * @param referenced the referenced method
	 * @param monitor the progress monitor to use
	 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
	 * @throws JavaModelException if the java elements could not be accessed
	 */
private ModifierKeyword thresholdTypeToMethod(final IType referencing, final IMethod referenced, final IProgressMonitor monitor) throws JavaModelException {
    final ICompilationUnit referencedUnit = referenced.getCompilationUnit();
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    if (referenced.getDeclaringType().equals(referencing))
        keyword = ModifierKeyword.PRIVATE_KEYWORD;
    else {
        final ITypeHierarchy hierarchy = getTypeHierarchy(referencing, new SubProgressMonitor(monitor, 1));
        final IType[] types = hierarchy.getSupertypes(referencing);
        IType superType = null;
        for (int index = 0; index < types.length; index++) {
            superType = types[index];
            if (superType.equals(referenced.getDeclaringType())) {
                keyword = ModifierKeyword.PROTECTED_KEYWORD;
                return keyword;
            }
        }
    }
    final ICompilationUnit typeUnit = referencing.getCompilationUnit();
    if (referencedUnit != null && referencedUnit.equals(typeUnit)) {
        if (referenced.getDeclaringType().getDeclaringType() != null)
            keyword = null;
        else
            keyword = ModifierKeyword.PRIVATE_KEYWORD;
    } else if (referencedUnit != null && referencedUnit.getParent().equals(typeUnit.getParent()))
        keyword = null;
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Aggregations

ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)11 IType (org.eclipse.jdt.core.IType)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 IField (org.eclipse.jdt.core.IField)3 IMethod (org.eclipse.jdt.core.IMethod)3 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)3 Modifier (org.eclipse.jdt.core.dom.Modifier)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IInitializer (org.eclipse.jdt.core.IInitializer)1 IMember (org.eclipse.jdt.core.IMember)1 MarkerAnnotation (org.eclipse.jdt.core.dom.MarkerAnnotation)1 Name (org.eclipse.jdt.core.dom.Name)1 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 MethodOverrideTester (org.eclipse.jdt.internal.corext.util.MethodOverrideTester)1