kanela.agent.util.classloader.AnalyzedClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kanela-agent Show documentation
Show all versions of kanela-agent Show documentation
The Kamon Instrumentation Agent
/*
* =========================================================================================
* Copyright © 2013-2018 the kamon project
*
* 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 kanela.agent.util.classloader;
import io.vavr.Tuple;
import io.vavr.collection.Array;
import io.vavr.collection.List;
import io.vavr.control.Option;
import io.vavr.control.Try;
import kanela.agent.api.instrumentation.classloader.ClassRefiner;
import kanela.agent.util.log.Logger;
import lombok.Value;
import lombok.val;
import net.bytebuddy.jar.asm.ClassReader;
import net.bytebuddy.jar.asm.Opcodes;
import net.bytebuddy.jar.asm.Type;
import net.bytebuddy.jar.asm.tree.ClassNode;
import net.bytebuddy.utility.OpenedClassReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
interface ClassMatcher {
Boolean match();
}
@Value
public class AnalyzedClass implements ClassMatcher {
private ClassRefiner classRefiner;
private Map fields;
private Map> methodsWithArguments;
public static ClassMatcher from(ClassRefiner refiner, ClassLoader loader) {
return Try.of(() -> {
val target = refiner.getTarget();
val resourceName = target.replace('.', '/') + ".class";
try(InputStream in = loader.getResourceAsStream(resourceName)) {
val classNode = convertToClassNode(in);
return (ClassMatcher) new AnalyzedClass(refiner, extractFieldsAndValues(refiner, classNode, loader), extractMethods(classNode));
}
})
.onFailure((cause) -> Logger.debug(() -> "Error trying to build an AnalyzedClass of type: " + refiner.getTarget() + " with error: " + cause.getMessage()))
.getOrElse(new NoOpAnalyzedClass());
}
public Boolean match() {
val evaluated = buildClassRefinerPredicate(this.classRefiner).test(true);
if(!evaluated) Logger.debug(() -> "The Class: " + this.classRefiner.getTarget() + " was filtered because not match with the provided ClassRefined: " + this.classRefiner);
return evaluated;
}
private Boolean containsFields(Map> fieldsAndValues) {
if(fieldsAndValues.isEmpty()) return true;
return !fieldsAndValues
.entrySet()
.stream()
.map((entry) -> containsField(entry.getKey(), entry.getValue()))
.collect(Collectors.toSet())
.contains(false);
}
private Boolean containsField(String fieldName, Option
© 2015 - 2025 Weber Informatics LLC | Privacy Policy