Skip to content

Commit

Permalink
surface (fix): Fix #3416 and #3419 - protected and package private ac…
Browse files Browse the repository at this point in the history
…cess (#3423)

Starting with tests - I hope I will be able to implement the fix as
well.

---------

Co-authored-by: Taro L. Saito <[email protected]>
  • Loading branch information
OndrejSpanel and xerial authored Feb 28, 2024
1 parent a188e7b commit d68b2bd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
case t
if !t.typeSymbol.flags.is(Flags.Abstract) && !t.typeSymbol.flags.is(Flags.Trait)
&& Option(t.typeSymbol.primaryConstructor)
.exists(p => p.exists && !p.flags.is(Flags.Private) && p.paramSymss.nonEmpty) =>
.exists { p =>
p.exists && !p.flags.is(Flags.Private) && !p.flags.is(Flags.Protected) &&
p.privateWithin.isEmpty && p.paramSymss.nonEmpty
} =>
val typeArgs = typeArgsOf(t.simplified).map(surfaceOf(_))
val methodParams = constructorParametersOf(t)
// val isStatic = !t.typeSymbol.flags.is(Flags.Local)
Expand Down Expand Up @@ -599,6 +602,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
case m if m.flags.is(Flags.Private) => false
case m if m.flags.is(Flags.Protected) => false
case m if m.flags.is(Flags.Artifact) => false
case m if m.privateWithin.nonEmpty => false
case _ => true
// println(s"${paramName} ${paramIsAccessible}")

Expand Down Expand Up @@ -832,6 +836,7 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
nonObject(x.owner) &&
x.isDefDef &&
// x.isPublic &&
x.privateWithin.isEmpty &&
!x.flags.is(Flags.Private) &&
!x.flags.is(Flags.Protected) &&
!x.flags.is(Flags.PrivateLocal) &&
Expand Down
38 changes: 38 additions & 0 deletions airframe-surface/src/test/scala/wvlet/airframe/surface/i3416.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.surface

import wvlet.airspec.AirSpec

object i3416 extends AirSpec {

object O {
class C(private[O] val id: Int) {
private[O] def getId: Int = id
}
}

test("List package private fields as parameters") {
val s = Surface.of[O.C]
debug(s.params)
s.params.size shouldBe 1
val p1 = s.params(0)
p1.name shouldBe "id"
}

test("Do not list package private methods") {
val s = Surface.methodsOf[O.C]
s.size shouldBe 0
}
}
45 changes: 45 additions & 0 deletions airframe-surface/src/test/scala/wvlet/airframe/surface/i3419.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.surface

import wvlet.airspec.AirSpec

object i3419 extends AirSpec {

class QPrivate private (val id: Int)

class QProtected protected (val id: Int)

object O {
class QPackagePrivate private[O] (val id: Int)
}

test("Handle private constructor") {
val s = Surface.of[QPrivate]
debug(s.params)
s.params.size shouldBe 0
}

test("Handle protected constructor") {
val s = Surface.of[QProtected]
debug(s.params)
s.params.size shouldBe 0
}

test("Handle package private constructor") {
val s = Surface.of[O.QPackagePrivate]
debug(s.params)
s.params.size shouldBe 0
}
}

0 comments on commit d68b2bd

Please sign in to comment.