use of com.sun.source.tree.AnnotationTree in project palantir-java-format by palantir.
the class JavaInputAstVisitor method visitModule.
@Override
public Void visitModule(ModuleTree node, Void unused) {
for (AnnotationTree annotation : node.getAnnotations()) {
scan(annotation, null);
builder.forcedBreak();
}
if (node.getModuleType() == ModuleTree.ModuleKind.OPEN) {
token("open");
builder.space();
}
token("module");
builder.space();
scan(node.getName(), null);
builder.space();
if (node.getDirectives().isEmpty()) {
tokenBreakTrailingComment("{", plusTwo);
builder.blankLineWanted(BlankLineWanted.NO);
token("}", plusTwo);
} else {
builder.open(plusTwo);
token("{");
builder.forcedBreak();
Optional<Tree.Kind> previousDirective = Optional.empty();
for (DirectiveTree directiveTree : node.getDirectives()) {
markForPartialFormat();
builder.blankLineWanted(previousDirective.map(k -> !k.equals(directiveTree.getKind())).orElse(false) ? BlankLineWanted.YES : BlankLineWanted.NO);
builder.forcedBreak();
scan(directiveTree, null);
previousDirective = Optional.of(directiveTree.getKind());
}
builder.close();
builder.forcedBreak();
token("}");
}
return null;
}
use of com.sun.source.tree.AnnotationTree in project gradle-baseline by palantir.
the class ImmutablesStyle method matchStyleAnnotatedType.
private Description matchStyleAnnotatedType(ClassTree tree, VisitorState state) {
if (ASTHelpers.enclosingClass(ASTHelpers.getSymbol(tree)) == null) {
// additional top-level classes.
return buildDescription(tree).addFix(SuggestedFixes.addSuppressWarnings(state, canonicalName(), "Automatically suppressed to unblock enforcement in new code")).build();
}
SuggestedFix.Builder fix = SuggestedFix.builder();
String qualifiedTarget = SuggestedFixes.qualifyType(state, fix, Target.class.getName());
String qualifiedElementType = SuggestedFixes.qualifyType(state, fix, ElementType.class.getName());
String qualifiedRetention = SuggestedFixes.qualifyType(state, fix, Retention.class.getName());
String qualifiedRetentionPolicy = SuggestedFixes.qualifyType(state, fix, RetentionPolicy.class.getName());
AnnotationTree styleAnnotationTree = getAnnotation(tree, "org.immutables.value.Value$Style", state);
return buildDescription(tree).addFix(fix.prefixWith(tree, String.format("\n@%s(%s.TYPE)\n@%s(%s.SOURCE)\n%s\n@interface %sStyle {}\n@%sStyle\n", qualifiedTarget, qualifiedElementType, qualifiedRetention, qualifiedRetentionPolicy, state.getSourceForNode(styleAnnotationTree), tree.getSimpleName(), tree.getSimpleName())).replace(styleAnnotationTree, "").build()).build();
}
use of com.sun.source.tree.AnnotationTree in project btrace by btraceio.
the class Verifier method hasTrustedAnnotation.
/**
* Detects if the class is annotated as @BTrace(trusted=true).
*/
private boolean hasTrustedAnnotation(ClassTree ct, Element topElement) {
for (AnnotationTree at : ct.getModifiers().getAnnotations()) {
String annFqn = ((JCTree) at.getAnnotationType()).type.tsym.getQualifiedName().toString();
if (!annFqn.equals(BTrace.class.getName())) {
continue;
}
// now we have @BTrace, look for unsafe = xxx or trusted = xxx
for (ExpressionTree ext : at.getArguments()) {
if (!(ext instanceof JCAssign)) {
continue;
}
JCAssign assign = (JCAssign) ext;
String name = ((JCIdent) assign.lhs).name.toString();
if (!"unsafe".equals(name) && !"trusted".equals(name)) {
continue;
}
// now rhs is the value of @BTrace.unsafe.
// The value can be complex (!!true, 1 == 2, etc.) - we support only booleans
String val = assign.rhs.toString();
if ("true".equals(val)) {
// bingo!
return true;
} else if (!"false".equals(val)) {
processingEnv.getMessager().printMessage(Kind.WARNING, Messages.get("no.complex.unsafe.value"), topElement);
}
}
}
return false;
}
use of com.sun.source.tree.AnnotationTree in project btrace by btraceio.
the class VerifierVisitor method visitMethod.
@Override
public Void visitMethod(MethodTree node, Void v) {
boolean oldInsideMethod = insideMethod;
insideMethod = true;
try {
Name name = node.getName();
if (name.contentEquals("<init>")) {
return super.visitMethod(node, v);
} else {
checkSampling(node);
if (isExitHandler(node)) {
if (node.getParameters().size() != 1 || !"int".equals(node.getParameters().get(0).getType().toString())) {
reportError("onexit.invalid", node);
return super.visitMethod(node, v);
}
}
if (isErrorHandler(node)) {
Element thrElement = getElement(node.getParameters().get(0).getType());
if (node.getParameters().size() != 1 || !THROWABLE_TYPE.equals(thrElement.toString())) {
reportError("onerror.invalid", node);
}
}
for (VariableTree vt : node.getParameters()) {
vt.accept(new TreeScanner<Void, Void>() {
@Override
public Void visitAnnotation(AnnotationTree at, Void p) {
isInAnnotation = true;
try {
return super.visitAnnotation(at, p);
} finally {
isInAnnotation = false;
}
}
}, null);
}
Set<Modifier> flags = node.getModifiers().getFlags();
if (shortSyntax) {
if (isStatic(flags)) {
reportError("no.static.method", node);
}
if (isSynchronized(flags)) {
reportError("no.synchronized.methods", node);
}
} else {
boolean isStatic = isStatic(flags);
if (isStatic) {
boolean isPublic = isPublic(node.getModifiers().getFlags());
if (isPublic) {
if (isSynchronized(flags)) {
reportError("no.synchronized.methods", node);
}
} else {
// force the "public" modifier only on the annotated methods
if (isAnnotated(node)) {
reportError("method.should.be.public", node);
}
}
} else {
reportError("no.instance.method", node);
}
}
node.accept(jfrFieldNameCollector, null);
return super.visitMethod(node, v);
}
} finally {
insideMethod = oldInsideMethod;
}
}
use of com.sun.source.tree.AnnotationTree in project btrace by btraceio.
the class VerifierVisitor method isExitHandler.
private boolean isExitHandler(MethodTree node) {
ModifiersTree mt = node.getModifiers();
List<? extends AnnotationTree> annos = mt.getAnnotations();
for (AnnotationTree at : annos) {
String annFqn = ((JCTree) at.getAnnotationType()).type.tsym.getQualifiedName().toString();
if (annFqn.equals(ON_EXIT_TYPE)) {
return true;
}
}
return false;
}
Aggregations