Search in sources :

Example 1 with LanguageAdapterResult

use of com.newrelic.weave.weavepackage.language.LanguageAdapterResult in project newrelic-java-agent by newrelic.

the class WeavePackage method processWeaveBytes.

/**
 * Processes all classes in the package.  Part of package initialization.
 * @param weavePackageBytes list of all class bytes in the package
 * @return list of weave violations found during package initialization
 */
protected final List<WeaveViolation> processWeaveBytes(List<byte[]> weavePackageBytes) {
    List<WeaveViolation> violations = new ArrayList<>();
    for (LanguageAdapter adapter : RegisteredLanguageAdapters.getLanguageAdapters()) {
        try {
            LanguageAdapterResult result = adapter.adapt(weavePackageBytes);
            weavePackageBytes = result.getAdaptedBytes();
            violations.addAll(result.getViolations());
        } catch (Throwable ignored) {
        }
    }
    // read weave annotations on each class bytes and put in appropriate map
    for (byte[] weaveClassBytes : weavePackageBytes) {
        ClassNode weaveNode = WeaveUtils.convertToClassNode(weaveClassBytes);
        WeaveClassInfo weave = new WeaveClassInfo(weaveNode);
        violations.addAll(weave.getViolations());
        final boolean isClassAnnotationMatch = !weave.getRequiredClassAnnotations().isEmpty();
        final boolean isMethodAnnotationMatch = !weave.getRequiredMethodAnnotations().isEmpty();
        if (isClassAnnotationMatch || isMethodAnnotationMatch) {
            if (isClassAnnotationMatch) {
                for (String requiredAnnotation : weave.getRequiredClassAnnotations()) {
                    if (weave.getMatchType().equals(Interface)) {
                        baseAnnotationWeaves.put(requiredAnnotation, weaveNode);
                    }
                    allClassAnnotationWeaves.put(requiredAnnotation, weaveNode);
                }
                requiredClassAnnotationsLookup.put(weaveNode.name, weave.getRequiredClassAnnotations());
            }
            if (isMethodAnnotationMatch) {
                for (String requiredAnnotation : weave.getRequiredMethodAnnotations()) {
                    if (!isClassAnnotationMatch) {
                        allMethodAnnotationWeaves.put(requiredAnnotation, weaveNode);
                    } else if (!allMethodAnnotationWeaves.containsKey(requiredAnnotation)) {
                        allMethodAnnotationWeaves.put(requiredAnnotation, weaveNode);
                    }
                }
                requiredMethodAnnotationsLookup.put(weaveNode.name, weave.getRequiredMethodAnnotations());
            }
        } else if (null == weave.getMatchType()) {
            if (weave.isSkipIfPresent()) {
                skipIfPresentClasses.add(weave.getOriginalName());
            } else {
                utilClasses.put(weaveNode.name, weaveNode);
            }
        } else {
            if (!weaveNode.name.equals(weave.getOriginalName())) {
                renames.put(weaveNode.name, weave.getOriginalName());
            }
            // check if added and merge
            weaveMatches.put(weave.getOriginalName(), weave.getMatchType());
            switch(weave.getMatchType()) {
                case BaseClass:
                case Interface:
                    baseWeaves.put(weave.getOriginalName(), weaveNode);
                    break;
                case ExactClass:
                default:
                    exactWeaves.put(weave.getOriginalName(), weaveNode);
                    break;
            }
        }
    }
    // preprocess
    this.preprocessAllWeaveCode();
    this.packageViolations.addAll(violations);
    if (isBootstrapClassName(this.exactWeaves.keySet()) || isBootstrapClassName(this.baseWeaves.keySet())) {
        this.weavesBootstrap = true;
    }
    return violations;
}
Also used : SynchronizedClassNode(com.newrelic.weave.utils.SynchronizedClassNode) ClassNode(org.objectweb.asm.tree.ClassNode) LanguageAdapterResult(com.newrelic.weave.weavepackage.language.LanguageAdapterResult) LanguageAdapter(com.newrelic.weave.weavepackage.language.LanguageAdapter) ArrayList(java.util.ArrayList) WeaveClassInfo(com.newrelic.weave.utils.WeaveClassInfo) WeaveViolation(com.newrelic.weave.violation.WeaveViolation)

Aggregations

SynchronizedClassNode (com.newrelic.weave.utils.SynchronizedClassNode)1 WeaveClassInfo (com.newrelic.weave.utils.WeaveClassInfo)1 WeaveViolation (com.newrelic.weave.violation.WeaveViolation)1 LanguageAdapter (com.newrelic.weave.weavepackage.language.LanguageAdapter)1 LanguageAdapterResult (com.newrelic.weave.weavepackage.language.LanguageAdapterResult)1 ArrayList (java.util.ArrayList)1 ClassNode (org.objectweb.asm.tree.ClassNode)1