比较两个元素的修饰符
/** NowJava.com - 时代Java 提供 **/ //package com.nowjava; import java.util.Set; import javax.lang.model.element.Element; import javax.lang.model.element.Modifier; public class Main { /** * Compare the modifier of two elements * * @return -1 if element a has better visibility, 0 if both have the same visibility, +1 if b has * the better visibility. The "best" visibility is PUBLIC */ public static int compareModifierVisibility(Element a, Element b) { // a better if (a.getModifiers().contains(Modifier.PUBLIC) && !b.getModifiers().contains(Modifier.PUBLIC)) { return -1; } if (isDefaultModifier(a.getModifiers()) && !isDefaultModifier(b.getModifiers())) { /** from 时 代 J a v a - nowjava.com**/ return -1; } if (a.getModifiers().contains(Modifier.PROTECTED) && !b.getModifiers().contains(Modifier.PROTECTED)) { return -1; } // b better if (b.getModifiers().contains(Modifier.PUBLIC) && !a.getModifiers().contains(Modifier.PUBLIC)) { return 1; } if (isDefaultModifier(b.getModifiers()) && !isDefaultModifier(a.getModifiers())) { return 1; } if (b.getModifiers().contains(Modifier.PROTECTED) && !a.getModifiers().contains(Modifier.PROTECTED)) { return 1; } // Same return 0; }