chore: Rewrite ClassContext with scala (#2260)

This commit is contained in:
He-Pin(kerr) 2025-09-22 17:16:17 +08:00 committed by GitHub
parent 750bf235f1
commit 391b71a3d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 43 deletions

View file

@ -17,7 +17,7 @@ import scala.util.control.NonFatal
import org.apache.pekko
import pekko.annotation.InternalApi
import pekko.util.{ ClassContext, OptionVal }
import pekko.util.OptionVal
/**
* INTERNAL API
@ -26,6 +26,15 @@ import pekko.util.{ ClassContext, OptionVal }
private[pekko] object LoggerClass {
private val defaultPrefixesToSkip = List("scala.runtime", "org.apache.pekko.actor.typed.internal")
private val OPTIONS: java.util.Set[StackWalker.Option] = java.util.Set.of(
StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES)
private val CLASS_STACK_WALKER: java.util.function.Function[
java.util.stream.Stream[StackWalker.StackFrame], Array[Class[_]]] =
(frames: java.util.stream.Stream[StackWalker.StackFrame]) =>
frames.map(frame => frame.getDeclaringClass)
.toArray[Class[_]]((size: Int) => new Array[Class[_]](size))
private def getClassStack: Array[Class[_]] = StackWalker.getInstance(OPTIONS).walk(CLASS_STACK_WALKER)
/**
* Try to extract a logger class from the call stack, if not possible the provided default is used
@ -43,7 +52,7 @@ private[pekko] object LoggerClass {
loop(additionalPrefixesToSkip ::: defaultPrefixesToSkip)
}
val trace = ClassContext.getClassStack
val trace = getClassStack
var suitableClass: OptionVal[Class[_]] = OptionVal.None
var idx = 1 // skip this method/class and right away
while (suitableClass.isEmpty && idx < trace.length) {

View file

@ -1,41 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.pekko.util;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
import org.apache.pekko.annotation.InternalApi;
/** INTERNAL API */
@InternalApi
public final class ClassContext {
private ClassContext() {
throw new UnsupportedOperationException("Cannot instantiate utility class");
}
private static final Set<StackWalker.Option> OPTIONS =
Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES);
private static final Function<Stream<StackWalker.StackFrame>, Class<?>[]> CLASS_STACK_WALKER =
frames -> frames.map(StackWalker.StackFrame::getDeclaringClass).toArray(Class<?>[]::new);
public static Class<?>[] getClassStack() {
return StackWalker.getInstance(OPTIONS).walk(CLASS_STACK_WALKER);
}
}