use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class InitializationVisitor method visitBlock.
@Override
public Void visitBlock(BlockTree node, Void p) {
// then check that all static fields have been initialized.
if (node.isStatic()) {
ClassTree enclosingClass = TreeUtils.enclosingClass(getCurrentPath());
boolean isStaticInitBlock = false;
boolean isLastStaticInitBlock = true;
for (Tree m : enclosingClass.getMembers()) {
if (m == node) {
isStaticInitBlock = true;
continue;
}
if (isStaticInitBlock && m.getKind() == Kind.BLOCK && ((BlockTree) m).isStatic()) {
isLastStaticInitBlock = false;
}
}
if (isLastStaticInitBlock && isStaticInitBlock) {
boolean isStatic = true;
Store store = atypeFactory.getRegularExitStore(node);
// Add field values for fields with an initializer.
for (Pair<VariableElement, Value> t : store.getAnalysis().getFieldValues()) {
store.addInitializedField(t.first);
}
// Check that all static fields are initialized.
List<AnnotationMirror> receiverAnnotations = Collections.emptyList();
checkFieldsInitialized(node, isStatic, store, receiverAnnotations);
}
}
return super.visitBlock(node, p);
}
use of com.sun.source.tree.ClassTree in project st-js by st-js.
the class LambdaExpressionWriter method accessOuterScope.
private boolean accessOuterScope(LambdaExpressionTree lambda) {
AtomicBoolean outerScopeAccess = new AtomicBoolean(false);
lambda.accept(new TreeScanner<Void, Void>() {
private boolean checkStopped;
@Override
public Void visitIdentifier(IdentifierTree tree, Void arg1) {
if (checkStopped) {
return super.visitIdentifier(tree, arg1);
}
Element fieldElement = TreeUtils.elementFromUse(tree);
if (IdentifierAccessOuterScopeCheck.isRegularInstanceField(fieldElement, tree) || GeneratorConstants.THIS.equals(tree.getName().toString())) {
outerScopeAccess.set(true);
}
return super.visitIdentifier(tree, arg1);
}
@Override
public Void visitClass(ClassTree arg0, Void arg1) {
// stop the checks if a new type is encountered
checkStopped = true;
return super.visitClass(arg0, arg1);
}
@Override
public Void visitMethodInvocation(MethodInvocationTree tree, Void arg1) {
if (checkStopped) {
return super.visitMethodInvocation(tree, arg1);
}
Element methodElement = TreeUtils.elementFromUse(tree);
if (JavaNodes.isStatic(methodElement)) {
// only instance methods
return super.visitMethodInvocation(tree, arg1);
}
String name = MethodInvocationWriter.buildMethodName(tree);
if (GeneratorConstants.THIS.equals(name) || GeneratorConstants.SUPER.equals(name)) {
// this and super call are ok
return super.visitMethodInvocation(tree, arg1);
}
if (!(tree.getMethodSelect() instanceof IdentifierTree)) {
// check for Outer.this check
return super.visitMethodInvocation(tree, arg1);
}
outerScopeAccess.set(true);
return super.visitMethodInvocation(tree, arg1);
}
}, null);
return outerScopeAccess.get();
}
use of com.sun.source.tree.ClassTree in project st-js by st-js.
the class ClassDuplicateMemberNameCheck method visit.
/**
* {@inheritDoc}
*/
@Override
public Void visit(CheckVisitor visitor, ClassTree tree, GenerationContext<Void> context) {
Multimap<String, Element> names = LinkedListMultimap.create();
// TypeElement classElement = TreeUtils.elementFromDeclaration(tree);
TypeElement classElement = (TypeElement) context.getTrees().getElement(context.getCurrentPath());
TypeMirror superType = classElement.getSuperclass();
if (superType.getKind() != TypeKind.NONE) {
// add the names from the super class
TypeElement superclassElement = (TypeElement) ((DeclaredType) superType).asElement();
for (Element memberElement : context.getElements().getAllMembers(superclassElement)) {
if (!JavaNodes.isNative(memberElement)) {
names.put(memberElement.getSimpleName().toString(), memberElement);
}
}
}
// check first the methods
for (Tree member : tree.getMembers()) {
checkMethod(classElement, member, context, names);
}
// check the fields
for (Tree member : tree.getMembers()) {
checkField(member, context, names);
}
return null;
}
use of com.sun.source.tree.ClassTree in project st-js by st-js.
the class ClassForbidExtendsSyntheticTypeCheck method visit.
/**
* {@inheritDoc}
*/
@Override
public Void visit(CheckVisitor visitor, ClassTree tree, GenerationContext<Void> context) {
TreeWrapper<ClassTree, Void> tw = context.getCurrentWrapper();
TypeElement element = (TypeElement) tw.getElement();
if (element.getNestingKind() == NestingKind.ANONYMOUS) {
return null;
}
if (tree.getExtendsClause() != null && tw.child(tree.getExtendsClause()).isSyntheticType()) {
context.addError(tree, "You cannot extend from a class that is marked as synthetic (@SyntheticType)");
}
for (Tree iface : tree.getImplementsClause()) {
checkInterface(tw.child(iface));
}
return null;
}
use of com.sun.source.tree.ClassTree in project st-js by st-js.
the class IdentifierAccessOuterScopeCheck method visit.
/**
* {@inheritDoc}
*/
@Override
public Void visit(CheckVisitor visitor, IdentifierTree tree, GenerationContext<Void> context) {
Element fieldElement = TreeUtils.elementFromUse(tree);
if (!isRegularInstanceField(fieldElement, tree)) {
return null;
}
ClassTree enclosingClassTree = enclosingClassSkipAnonymousInitializer(context.getCurrentPath());
TypeElement currentScopeClassElement = TreeUtils.elementFromDeclaration(enclosingClassTree);
TypeElement fieldOwnerElement = (TypeElement) fieldElement.getEnclosingElement();
if (isOuterType(context, fieldOwnerElement, currentScopeClassElement)) {
context.addError(tree, "In Javascript you cannot access a field from the outer type. " + "You should define a variable var that=this outside your function definition and use the property of this object. The field: " + tree);
}
return null;
}
Aggregations