Add Interface.isLocal and use it where appropriate

Some EJB logic regrettably needs to be renamed out of the way. Hopefully the churn caused by this is less than would be caused if Interface's isLocal needed to be named differently from Class.isLocal.
This commit is contained in:
Chris Smowton
2021-08-23 19:01:32 +01:00
parent ca5c2b2acf
commit e048a729db
6 changed files with 52 additions and 51 deletions

View File

@@ -45,7 +45,7 @@ class Member extends Element, Annotatable, Modifiable, @member {
Callable getEnclosingCallable() { Callable getEnclosingCallable() {
exists(NestedClass nc | this.getDeclaringType() = nc | exists(NestedClass nc | this.getDeclaringType() = nc |
nc.(AnonymousClass).getClassInstanceExpr().getEnclosingCallable() = result or nc.(AnonymousClass).getClassInstanceExpr().getEnclosingCallable() = result or
nc.(LocalClass).getLocalClassDeclStmt().getEnclosingCallable() = result nc.(LocalClassOrInterface).getLocalClassDeclStmt().getEnclosingCallable() = result
) )
} }
} }

View File

@@ -786,14 +786,20 @@ class LocalVariableDeclStmt extends Stmt, @localvariabledeclstmt {
override string getAPrimaryQlClass() { result = "LocalVariableDeclStmt" } override string getAPrimaryQlClass() { result = "LocalVariableDeclStmt" }
} }
/** A statement that declares a local class. */ /** A statement that declares a local class or interface. */
class LocalClassDeclStmt extends Stmt, @localclassdeclstmt { class LocalClassDeclStmt extends Stmt, @localclassdeclstmt {
/** Gets the local class declared by this statement. */ /** Gets the local class declared by this statement. */
LocalClass getLocalClass() { isLocalClass(result, this) } LocalClassOrInterface getLocalClass() { isLocalClass(result, this) }
override string pp() { result = "class " + this.getLocalClass().toString() } private string getDeclKeyword() {
result = "class" and this.getLocalClass() instanceof Class
or
result = "interface" and this.getLocalClass() instanceof Interface
}
override string toString() { result = "class ..." } override string pp() { result = this.getDeclKeyword() + " " + this.getLocalClass().toString() }
override string toString() { result = this.getDeclKeyword() + " ..." }
override string getHalsteadID() { result = "LocalClassDeclStmt" } override string getHalsteadID() { result = "LocalClassDeclStmt" }

View File

@@ -6,8 +6,8 @@
* (`Interface`). * (`Interface`).
* *
* Reference types can be at the top level (`TopLevelType`) or nested (`NestedType`). * Reference types can be at the top level (`TopLevelType`) or nested (`NestedType`).
* Classes can also be local (`LocalClass`) or anonymous (`AnonymousClass`). * Classes and interfaces can also be local (`LocalClassOrInterface`, `LocalClass`) or anonymous (`AnonymousClass`).
* Enumerated types (`EnumType`) are a special kind of class. * Enumerated types (`EnumType`) and records (`Record`) are a special kinds of class.
*/ */
import Member import Member
@@ -269,7 +269,7 @@ predicate declaresMember(Type t, @member m) {
// Since the type `@member` in the dbscheme includes all `@reftype`s, // Since the type `@member` in the dbscheme includes all `@reftype`s,
// anonymous and local classes need to be excluded here. // anonymous and local classes need to be excluded here.
not m instanceof AnonymousClass and not m instanceof AnonymousClass and
not m instanceof LocalClass not m instanceof LocalClassOrInterface
} }
/** /**
@@ -608,20 +608,10 @@ class SrcRefType extends RefType {
} }
/** A class declaration. */ /** A class declaration. */
class Class extends RefType, @class { class Class extends ClassOrInterface, @class {
/** Holds if this class is an anonymous class. */ /** Holds if this class is an anonymous class. */
predicate isAnonymous() { isAnonymClass(this, _) } predicate isAnonymous() { isAnonymClass(this, _) }
/** Holds if this class is a local class. */
predicate isLocal() { isLocalClass(this, _) }
/** Holds if this class is package protected, that is, neither public nor private nor protected. */
predicate isPackageProtected() {
not isPrivate() and
not isProtected() and
not isPublic()
}
override RefType getSourceDeclaration() { classes(this, _, _, result) } override RefType getSourceDeclaration() { classes(this, _, _, result) }
/** /**
@@ -630,11 +620,13 @@ class Class extends RefType, @class {
* Note that a class may inherit annotations from super-classes. * Note that a class may inherit annotations from super-classes.
*/ */
override Annotation getAnAnnotation() { override Annotation getAnAnnotation() {
result = RefType.super.getAnAnnotation() result = ClassOrInterface.super.getAnAnnotation()
or or
exists(AnnotationType tp | tp = result.getType() | exists(AnnotationType tp | tp = result.getType() |
tp.isInherited() and tp.isInherited() and
not exists(Annotation ann | ann = RefType.super.getAnAnnotation() | ann.getType() = tp) and not exists(Annotation ann | ann = ClassOrInterface.super.getAnAnnotation() |
ann.getType() = tp
) and
result = this.getASupertype().(Class).getAnAnnotation() result = this.getASupertype().(Class).getAnAnnotation()
) )
} }
@@ -643,8 +635,6 @@ class Class extends RefType, @class {
} }
/** /**
* PREVIEW FEATURE in Java 14. Subject to removal in a future release.
*
* A record declaration. * A record declaration.
*/ */
class Record extends Class { class Record extends Class {
@@ -727,13 +717,20 @@ class AnonymousClass extends NestedClass {
override string getAPrimaryQlClass() { result = "AnonymousClass" } override string getAPrimaryQlClass() { result = "AnonymousClass" }
} }
/** A local class. */ /** A local class or interface. */
class LocalClass extends NestedClass { class LocalClassOrInterface extends NestedType, ClassOrInterface {
LocalClass() { this.isLocal() } LocalClassOrInterface() { this.isLocal() }
/** Gets the statement that declares this local class. */ /** Gets the statement that declares this local class. */
LocalClassDeclStmt getLocalClassDeclStmt() { isLocalClass(this, result) } LocalClassDeclStmt getLocalClassDeclStmt() { isLocalClass(this, result) }
override string getAPrimaryQlClass() { result = "LocalClassOrInterface" }
}
/** A local class. */
class LocalClass extends LocalClassOrInterface, NestedClass {
LocalClass() { this.isLocal() }
override string getAPrimaryQlClass() { result = "LocalClass" } override string getAPrimaryQlClass() { result = "LocalClass" }
} }
@@ -847,7 +844,7 @@ class InnerClass extends NestedClass {
} }
/** An interface. */ /** An interface. */
class Interface extends RefType, @interface { class Interface extends ClassOrInterface, @interface {
override RefType getSourceDeclaration() { interfaces(this, _, _, result) } override RefType getSourceDeclaration() { interfaces(this, _, _, result) }
override predicate isAbstract() { override predicate isAbstract() {
@@ -855,21 +852,24 @@ class Interface extends RefType, @interface {
any() any()
} }
/** Holds if this interface is package protected, that is, neither public nor private nor protected. */
predicate isPackageProtected() {
not isPrivate() and
not isProtected() and
not isPublic()
}
override string getAPrimaryQlClass() { result = "Interface" } override string getAPrimaryQlClass() { result = "Interface" }
} }
/** A class or interface. */ /** A class or interface. */
class ClassOrInterface extends RefType { class ClassOrInterface extends RefType {
ClassOrInterface() { ClassOrInterface() {
this instanceof Class or this instanceof @class or
this instanceof Interface this instanceof @interface
}
/** Holds if this class or interface is local. */
predicate isLocal() { isLocalClass(this, _) }
/** Holds if this class or interface is package protected, that is, neither public nor private nor protected. */
predicate isPackageProtected() {
not isPrivate() and
not isProtected() and
not isPublic()
} }
} }

View File

@@ -234,10 +234,10 @@ abstract class BusinessInterface extends Interface {
abstract SessionEJB getAnEJB(); abstract SessionEJB getAnEJB();
/** Holds if this business interface is declared local. */ /** Holds if this business interface is declared local. */
abstract predicate isLocal(); abstract predicate isDeclaredLocal();
/** Holds if this business interface is declared remote. */ /** Holds if this business interface is declared remote. */
abstract predicate isRemote(); abstract predicate isDeclaredRemote();
} }
/** /**
@@ -259,14 +259,14 @@ class XmlSpecifiedBusinessInterface extends BusinessInterface {
) )
} }
override predicate isLocal() { override predicate isDeclaredLocal() {
exists(EjbJarXMLFile f | exists(EjbJarXMLFile f |
this.getQualifiedName() = this.getQualifiedName() =
f.getASessionElement().getABusinessLocalElement().getACharactersSet().getCharacters() f.getASessionElement().getABusinessLocalElement().getACharactersSet().getCharacters()
) )
} }
override predicate isRemote() { override predicate isDeclaredRemote() {
exists(EjbJarXMLFile f | exists(EjbJarXMLFile f |
this.getQualifiedName() = this.getQualifiedName() =
f.getASessionElement().getABusinessRemoteElement().getACharactersSet().getCharacters() f.getASessionElement().getABusinessRemoteElement().getACharactersSet().getCharacters()
@@ -295,9 +295,9 @@ class AnnotatedBusinessInterface extends BusinessInterface {
result.getAnAnnotation().(BusinessInterfaceAnnotation).getANamedType() = this result.getAnAnnotation().(BusinessInterfaceAnnotation).getANamedType() = this
} }
override predicate isLocal() { this instanceof LocalAnnotatedBusinessInterface } override predicate isDeclaredLocal() { this instanceof LocalAnnotatedBusinessInterface }
override predicate isRemote() { this instanceof RemoteAnnotatedBusinessInterface } override predicate isDeclaredRemote() { this instanceof RemoteAnnotatedBusinessInterface }
} }
/** /**
@@ -540,7 +540,7 @@ class XmlSpecifiedLocalHomeInterface extends LegacyEjbLocalHomeInterface {
class RemoteInterface extends Interface { class RemoteInterface extends Interface {
RemoteInterface() { RemoteInterface() {
this instanceof RemoteAnnotatedBusinessInterface or this instanceof RemoteAnnotatedBusinessInterface or
this.(XmlSpecifiedBusinessInterface).isRemote() or this.(XmlSpecifiedBusinessInterface).isDeclaredRemote() or
exists(SessionEJB ejb | this = ejb.getARemoteInterface()) exists(SessionEJB ejb | this = ejb.getARemoteInterface())
} }

View File

@@ -37,7 +37,7 @@ where
// Remove local classes defined in the dead method - they are reported separately as a dead // Remove local classes defined in the dead method - they are reported separately as a dead
// class. We keep anonymous class counts, because anonymous classes are not reported // class. We keep anonymous class counts, because anonymous classes are not reported
// separately. // separately.
sum(LocalClass localClass | sum(LocalClassOrInterface localClass |
localClass.getLocalClassDeclStmt().getEnclosingCallable() = deadMethod localClass.getLocalClassDeclStmt().getEnclosingCallable() = deadMethod
| |
localClass.getNumberOfLinesOfCode() localClass.getNumberOfLinesOfCode()

View File

@@ -8,15 +8,10 @@
import java import java
/** A type that should be in the generated code. */ /** A type that should be in the generated code. */
abstract private class GeneratedType extends RefType { abstract private class GeneratedType extends ClassOrInterface {
GeneratedType() { GeneratedType() {
(
this instanceof Interface
or
this instanceof Class
) and
not this instanceof AnonymousClass and not this instanceof AnonymousClass and
not this instanceof LocalClass and not this.isLocal() and
not this.getPackage() instanceof ExcludedPackage not this.getPackage() instanceof ExcludedPackage
} }