use of javax.lang.model.element.Modifier in project epoxy by airbnb.
the class ProcessorUtils method implementsMethod.
/**
* @return True if the clazz (or one of its superclasses) implements the given method. Returns
* false if the method doesn't exist anywhere in the class hierarchy or it is abstract.
*/
static boolean implementsMethod(TypeElement clazz, MethodSpec method, Types typeUtils) {
ExecutableElement methodOnClass = getMethodOnClass(clazz, method, typeUtils);
if (methodOnClass == null) {
return false;
}
Set<Modifier> modifiers = methodOnClass.getModifiers();
return !modifiers.contains(Modifier.ABSTRACT);
}
use of javax.lang.model.element.Modifier in project epoxy by airbnb.
the class EpoxyProcessor method validateAccessibleViaGeneratedCode.
private void validateAccessibleViaGeneratedCode(Element attribute) {
TypeElement enclosingElement = (TypeElement) attribute.getEnclosingElement();
// Verify method modifiers.
Set<Modifier> modifiers = attribute.getModifiers();
if (modifiers.contains(PRIVATE) || modifiers.contains(STATIC)) {
logError("%s annotations must not be on private or static fields. (class: %s, field: %s)", EpoxyAttribute.class.getSimpleName(), enclosingElement.getSimpleName(), attribute.getSimpleName());
}
// Nested classes must be static
if (enclosingElement.getNestingKind().isNested()) {
if (!enclosingElement.getModifiers().contains(STATIC)) {
logError("Nested classes with %s annotations must be static. (class: %s, field: %s)", EpoxyAttribute.class.getSimpleName(), enclosingElement.getSimpleName(), attribute.getSimpleName());
}
}
// Verify containing type.
if (enclosingElement.getKind() != CLASS) {
logError("%s annotations may only be contained in classes. (class: %s, field: %s)", EpoxyAttribute.class.getSimpleName(), enclosingElement.getSimpleName(), attribute.getSimpleName());
}
// Verify containing class visibility is not private.
if (enclosingElement.getModifiers().contains(PRIVATE)) {
logError("%s annotations may not be contained in private classes. (class: %s, field: %s)", EpoxyAttribute.class.getSimpleName(), enclosingElement.getSimpleName(), attribute.getSimpleName());
}
}
use of javax.lang.model.element.Modifier in project javapoet by square.
the class MethodSpec method overriding.
/**
* Returns a new method spec builder that overrides {@code method}.
*
* <p>This will copy its visibility modifiers, type parameters, return type, name, parameters, and
* throws declarations. An {@link Override} annotation will be added.
*
* <p>Note that in JavaPoet 1.2 through 1.7 this method retained annotations from the method and
* parameters of the overridden method. Since JavaPoet 1.8 annotations must be added separately.
*/
public static Builder overriding(ExecutableElement method) {
checkNotNull(method, "method == null");
Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.FINAL) || modifiers.contains(Modifier.STATIC)) {
throw new IllegalArgumentException("cannot override method with modifiers: " + modifiers);
}
String methodName = method.getSimpleName().toString();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName);
methodBuilder.addAnnotation(Override.class);
modifiers = new LinkedHashSet<>(modifiers);
modifiers.remove(Modifier.ABSTRACT);
// LinkedHashSet permits null as element for Java 7
modifiers.remove(Util.DEFAULT);
methodBuilder.addModifiers(modifiers);
for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
TypeVariable var = (TypeVariable) typeParameterElement.asType();
methodBuilder.addTypeVariable(TypeVariableName.get(var));
}
methodBuilder.returns(TypeName.get(method.getReturnType()));
methodBuilder.addParameters(ParameterSpec.parametersOf(method));
methodBuilder.varargs(method.isVarArgs());
for (TypeMirror thrownType : method.getThrownTypes()) {
methodBuilder.addException(TypeName.get(thrownType));
}
return methodBuilder;
}
use of javax.lang.model.element.Modifier in project error-prone by google.
the class SuggestedFixes method addModifiers.
/** Add modifiers to the given class, method, or field declaration. */
@Nullable
public static SuggestedFix addModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
ModifiersTree originalModifiers = getModifiers(tree);
if (originalModifiers == null) {
return null;
}
Set<Modifier> toAdd = Sets.difference(new TreeSet<>(Arrays.asList(modifiers)), originalModifiers.getFlags());
if (originalModifiers.getFlags().isEmpty()) {
int pos = state.getEndPosition(originalModifiers) != Position.NOPOS ? state.getEndPosition(originalModifiers) + 1 : ((JCTree) tree).getStartPosition();
int base = ((JCTree) tree).getStartPosition();
java.util.Optional<Integer> insert = state.getTokensForNode(tree).stream().map(token -> token.pos() + base).filter(thisPos -> thisPos >= pos).findFirst();
// shouldn't ever be able to get to the else
int insertPos = insert.orElse(pos);
return SuggestedFix.replace(insertPos, insertPos, Joiner.on(' ').join(toAdd) + " ");
}
// a map from modifiers to modifier position (or -1 if the modifier is being added)
// modifiers are sorted in Google Java Style order
Map<Modifier, Integer> modifierPositions = new TreeMap<>();
for (Modifier mod : toAdd) {
modifierPositions.put(mod, -1);
}
List<ErrorProneToken> tokens = state.getTokensForNode(originalModifiers);
int base = ((JCTree) originalModifiers).getStartPosition();
for (ErrorProneToken tok : tokens) {
Modifier mod = getTokModifierKind(tok);
if (mod != null) {
modifierPositions.put(mod, base + tok.pos());
}
}
SuggestedFix.Builder fix = SuggestedFix.builder();
// walk the map of all modifiers, and accumulate a list of new modifiers to insert
// beside an existing modifier
List<Modifier> modifiersToWrite = new ArrayList<>();
for (Modifier mod : modifierPositions.keySet()) {
int p = modifierPositions.get(mod);
if (p == -1) {
modifiersToWrite.add(mod);
} else if (!modifiersToWrite.isEmpty()) {
fix.replace(p, p, Joiner.on(' ').join(modifiersToWrite) + " ");
modifiersToWrite.clear();
}
}
if (!modifiersToWrite.isEmpty()) {
fix.postfixWith(originalModifiers, " " + Joiner.on(' ').join(modifiersToWrite));
}
return fix.build();
}
use of javax.lang.model.element.Modifier in project error-prone by google.
the class FindIdentifiers method isVisible.
private static boolean isVisible(VarSymbol var, final TreePath path) {
switch(var.getKind()) {
case ENUM_CONSTANT:
case FIELD:
// TODO(eaftan): Switch collector to ImmutableList.toImmutableList() when released
List<ClassSymbol> enclosingClasses = StreamSupport.stream(path.spliterator(), false).filter(tree -> tree instanceof ClassTree).map(ClassTree.class::cast).map(ASTHelpers::getSymbol).collect(Collectors.toCollection(ArrayList::new));
if (!var.isStatic()) {
// Instance fields are not visible if we are in a static context...
if (inStaticContext(path)) {
return false;
}
// the enclosing static nested class (JLS 8.5.1).
if (lowerThan(path, (curr, unused) -> curr instanceof ClassTree && ASTHelpers.getSymbol((ClassTree) curr).isStatic(), (curr, unused) -> curr instanceof ClassTree && ASTHelpers.getSymbol((ClassTree) curr).equals(var.owner))) {
return false;
}
}
// fields (JLS 6.6.1).
if (enclosingClasses.contains(ASTHelpers.enclosingClass(var))) {
return true;
}
PackageSymbol enclosingPackage = ((JCCompilationUnit) path.getCompilationUnit()).packge;
Set<Modifier> modifiers = var.getModifiers();
// (JLS 6.6.1).
if (Objects.equals(enclosingPackage, ASTHelpers.enclosingPackage(var))) {
return !modifiers.contains(Modifier.PRIVATE);
}
// in the enclosing class or a superclass).
return modifiers.contains(Modifier.PUBLIC) || modifiers.contains(Modifier.PROTECTED);
case PARAMETER:
case LOCAL_VARIABLE:
// final or effectively final (JLS 8.1.3).
if (lowerThan(path, (curr, parent) -> curr.getKind() == Kind.LAMBDA_EXPRESSION || (curr.getKind() == Kind.NEW_CLASS && ((NewClassTree) curr).getClassBody() != null) || (curr.getKind() == Kind.CLASS && parent.getKind() == Kind.BLOCK), (curr, unused) -> Objects.equals(var.owner, ASTHelpers.getSymbol(curr)))) {
if ((var.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
return false;
}
}
return true;
case EXCEPTION_PARAMETER:
case RESOURCE_VARIABLE:
return true;
default:
throw new IllegalArgumentException("Unexpected variable type: " + var.getKind());
}
}
Aggregations