Skip to content

Commit e048a72

Browse files
committed
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.
1 parent ca5c2b2 commit e048a72

File tree

6 files changed

+52
-51
lines changed

6 files changed

+52
-51
lines changed

java/ql/lib/semmle/code/java/Member.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Member extends Element, Annotatable, Modifiable, @member {
4545
Callable getEnclosingCallable() {
4646
exists(NestedClass nc | this.getDeclaringType() = nc |
4747
nc.(AnonymousClass).getClassInstanceExpr().getEnclosingCallable() = result or
48-
nc.(LocalClass).getLocalClassDeclStmt().getEnclosingCallable() = result
48+
nc.(LocalClassOrInterface).getLocalClassDeclStmt().getEnclosingCallable() = result
4949
)
5050
}
5151
}

java/ql/lib/semmle/code/java/Statement.qll

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,14 +786,20 @@ class LocalVariableDeclStmt extends Stmt, @localvariabledeclstmt {
786786
override string getAPrimaryQlClass() { result = "LocalVariableDeclStmt" }
787787
}
788788

789-
/** A statement that declares a local class. */
789+
/** A statement that declares a local class or interface. */
790790
class LocalClassDeclStmt extends Stmt, @localclassdeclstmt {
791791
/** Gets the local class declared by this statement. */
792-
LocalClass getLocalClass() { isLocalClass(result, this) }
792+
LocalClassOrInterface getLocalClass() { isLocalClass(result, this) }
793793

794-
override string pp() { result = "class " + this.getLocalClass().toString() }
794+
private string getDeclKeyword() {
795+
result = "class" and this.getLocalClass() instanceof Class
796+
or
797+
result = "interface" and this.getLocalClass() instanceof Interface
798+
}
799+
800+
override string pp() { result = this.getDeclKeyword() + " " + this.getLocalClass().toString() }
795801

796-
override string toString() { result = "class ..." }
802+
override string toString() { result = this.getDeclKeyword() + " ..." }
797803

798804
override string getHalsteadID() { result = "LocalClassDeclStmt" }
799805

java/ql/lib/semmle/code/java/Type.qll

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* (`Interface`).
77
*
88
* Reference types can be at the top level (`TopLevelType`) or nested (`NestedType`).
9-
* Classes can also be local (`LocalClass`) or anonymous (`AnonymousClass`).
10-
* Enumerated types (`EnumType`) are a special kind of class.
9+
* Classes and interfaces can also be local (`LocalClassOrInterface`, `LocalClass`) or anonymous (`AnonymousClass`).
10+
* Enumerated types (`EnumType`) and records (`Record`) are a special kinds of class.
1111
*/
1212

1313
import Member
@@ -269,7 +269,7 @@ predicate declaresMember(Type t, @member m) {
269269
// Since the type `@member` in the dbscheme includes all `@reftype`s,
270270
// anonymous and local classes need to be excluded here.
271271
not m instanceof AnonymousClass and
272-
not m instanceof LocalClass
272+
not m instanceof LocalClassOrInterface
273273
}
274274

275275
/**
@@ -608,20 +608,10 @@ class SrcRefType extends RefType {
608608
}
609609

610610
/** A class declaration. */
611-
class Class extends RefType, @class {
611+
class Class extends ClassOrInterface, @class {
612612
/** Holds if this class is an anonymous class. */
613613
predicate isAnonymous() { isAnonymClass(this, _) }
614614

615-
/** Holds if this class is a local class. */
616-
predicate isLocal() { isLocalClass(this, _) }
617-
618-
/** Holds if this class is package protected, that is, neither public nor private nor protected. */
619-
predicate isPackageProtected() {
620-
not isPrivate() and
621-
not isProtected() and
622-
not isPublic()
623-
}
624-
625615
override RefType getSourceDeclaration() { classes(this, _, _, result) }
626616

627617
/**
@@ -630,11 +620,13 @@ class Class extends RefType, @class {
630620
* Note that a class may inherit annotations from super-classes.
631621
*/
632622
override Annotation getAnAnnotation() {
633-
result = RefType.super.getAnAnnotation()
623+
result = ClassOrInterface.super.getAnAnnotation()
634624
or
635625
exists(AnnotationType tp | tp = result.getType() |
636626
tp.isInherited() and
637-
not exists(Annotation ann | ann = RefType.super.getAnAnnotation() | ann.getType() = tp) and
627+
not exists(Annotation ann | ann = ClassOrInterface.super.getAnAnnotation() |
628+
ann.getType() = tp
629+
) and
638630
result = this.getASupertype().(Class).getAnAnnotation()
639631
)
640632
}
@@ -643,8 +635,6 @@ class Class extends RefType, @class {
643635
}
644636

645637
/**
646-
* PREVIEW FEATURE in Java 14. Subject to removal in a future release.
647-
*
648638
* A record declaration.
649639
*/
650640
class Record extends Class {
@@ -727,13 +717,20 @@ class AnonymousClass extends NestedClass {
727717
override string getAPrimaryQlClass() { result = "AnonymousClass" }
728718
}
729719

730-
/** A local class. */
731-
class LocalClass extends NestedClass {
732-
LocalClass() { this.isLocal() }
720+
/** A local class or interface. */
721+
class LocalClassOrInterface extends NestedType, ClassOrInterface {
722+
LocalClassOrInterface() { this.isLocal() }
733723

734724
/** Gets the statement that declares this local class. */
735725
LocalClassDeclStmt getLocalClassDeclStmt() { isLocalClass(this, result) }
736726

727+
override string getAPrimaryQlClass() { result = "LocalClassOrInterface" }
728+
}
729+
730+
/** A local class. */
731+
class LocalClass extends LocalClassOrInterface, NestedClass {
732+
LocalClass() { this.isLocal() }
733+
737734
override string getAPrimaryQlClass() { result = "LocalClass" }
738735
}
739736

@@ -847,29 +844,32 @@ class InnerClass extends NestedClass {
847844
}
848845

849846
/** An interface. */
850-
class Interface extends RefType, @interface {
847+
class Interface extends ClassOrInterface, @interface {
851848
override RefType getSourceDeclaration() { interfaces(this, _, _, result) }
852849

853850
override predicate isAbstract() {
854851
// JLS 9.1.1.1: "Every interface is implicitly abstract"
855852
any()
856853
}
857854

858-
/** Holds if this interface is package protected, that is, neither public nor private nor protected. */
859-
predicate isPackageProtected() {
860-
not isPrivate() and
861-
not isProtected() and
862-
not isPublic()
863-
}
864-
865855
override string getAPrimaryQlClass() { result = "Interface" }
866856
}
867857

868858
/** A class or interface. */
869859
class ClassOrInterface extends RefType {
870860
ClassOrInterface() {
871-
this instanceof Class or
872-
this instanceof Interface
861+
this instanceof @class or
862+
this instanceof @interface
863+
}
864+
865+
/** Holds if this class or interface is local. */
866+
predicate isLocal() { isLocalClass(this, _) }
867+
868+
/** Holds if this class or interface is package protected, that is, neither public nor private nor protected. */
869+
predicate isPackageProtected() {
870+
not isPrivate() and
871+
not isProtected() and
872+
not isPublic()
873873
}
874874
}
875875

java/ql/lib/semmle/code/java/frameworks/javaee/ejb/EJB.qll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ abstract class BusinessInterface extends Interface {
234234
abstract SessionEJB getAnEJB();
235235

236236
/** Holds if this business interface is declared local. */
237-
abstract predicate isLocal();
237+
abstract predicate isDeclaredLocal();
238238

239239
/** Holds if this business interface is declared remote. */
240-
abstract predicate isRemote();
240+
abstract predicate isDeclaredRemote();
241241
}
242242

243243
/**
@@ -259,14 +259,14 @@ class XmlSpecifiedBusinessInterface extends BusinessInterface {
259259
)
260260
}
261261

262-
override predicate isLocal() {
262+
override predicate isDeclaredLocal() {
263263
exists(EjbJarXMLFile f |
264264
this.getQualifiedName() =
265265
f.getASessionElement().getABusinessLocalElement().getACharactersSet().getCharacters()
266266
)
267267
}
268268

269-
override predicate isRemote() {
269+
override predicate isDeclaredRemote() {
270270
exists(EjbJarXMLFile f |
271271
this.getQualifiedName() =
272272
f.getASessionElement().getABusinessRemoteElement().getACharactersSet().getCharacters()
@@ -295,9 +295,9 @@ class AnnotatedBusinessInterface extends BusinessInterface {
295295
result.getAnAnnotation().(BusinessInterfaceAnnotation).getANamedType() = this
296296
}
297297

298-
override predicate isLocal() { this instanceof LocalAnnotatedBusinessInterface }
298+
override predicate isDeclaredLocal() { this instanceof LocalAnnotatedBusinessInterface }
299299

300-
override predicate isRemote() { this instanceof RemoteAnnotatedBusinessInterface }
300+
override predicate isDeclaredRemote() { this instanceof RemoteAnnotatedBusinessInterface }
301301
}
302302

303303
/**
@@ -540,7 +540,7 @@ class XmlSpecifiedLocalHomeInterface extends LegacyEjbLocalHomeInterface {
540540
class RemoteInterface extends Interface {
541541
RemoteInterface() {
542542
this instanceof RemoteAnnotatedBusinessInterface or
543-
this.(XmlSpecifiedBusinessInterface).isRemote() or
543+
this.(XmlSpecifiedBusinessInterface).isDeclaredRemote() or
544544
exists(SessionEJB ejb | this = ejb.getARemoteInterface())
545545
}
546546

java/ql/src/DeadCode/FLinesOfDeadCode.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ where
3737
// Remove local classes defined in the dead method - they are reported separately as a dead
3838
// class. We keep anonymous class counts, because anonymous classes are not reported
3939
// separately.
40-
sum(LocalClass localClass |
40+
sum(LocalClassOrInterface localClass |
4141
localClass.getLocalClassDeclStmt().getEnclosingCallable() = deadMethod
4242
|
4343
localClass.getNumberOfLinesOfCode()

java/ql/src/utils/Stubs.qll

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
import java
99

1010
/** A type that should be in the generated code. */
11-
abstract private class GeneratedType extends RefType {
11+
abstract private class GeneratedType extends ClassOrInterface {
1212
GeneratedType() {
13-
(
14-
this instanceof Interface
15-
or
16-
this instanceof Class
17-
) and
1813
not this instanceof AnonymousClass and
19-
not this instanceof LocalClass and
14+
not this.isLocal() and
2015
not this.getPackage() instanceof ExcludedPackage
2116
}
2217

0 commit comments

Comments
 (0)