/** * {@inheritDoc} */ @SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}, justification = "The array is not to be modified by contract") public byte[] transform(ClassLoader classLoader, String internalName, Class<?> redefinedType, ProtectionDomain protectionDomain, byte[] binaryRepresentation) { if (internalName != null && isChildOf(this.classLoader).matches(classLoader) && typeName.equals(internalName.replace('/', '.'))) { this.binaryRepresentation = binaryRepresentation.clone(); } return DO_NOT_TRANSFORM; }
public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader) { if (DEBUG_INSTRUMENTATION && logger.isDebugEnabled() && getTypeMatcher().matches(typeDescription)) { logger.debug("IGNORE {} ({})", typeDescription.getName(), getClass().getSimpleName()); } }
@Override public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader) { if (DEBUG_INSTRUMENTATION && typeDescription.getName().contains("DataSource")) { if (getTypeMatcher().matches(typeDescription)) { final boolean classLoaderMatches = getClassLoaderMatcher().matches(classLoader); logger.warn("IGNORE {} in {}. ClassLoader: {} matches: {}", typeDescription.getName(), getClass().getSimpleName(), classLoader, classLoaderMatches); if (!classLoaderMatches) { logger.warn("excluded by classloader matcher"); } else { logger.warn("excluded by global matcher"); } } else { logger.info("IGNORE {} in {}", typeDescription.getName(), getClass().getSimpleName()); } } }
private boolean declaresInHierarchy(MethodDescription targetMethod, TypeDescription type) { if (declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures())) .and(extraMethodMatcher)) .matches(type)) { return true; } for (TypeDescription interfaze : type.getInterfaces().asErasures()) { if (superClassMatcher.matches(interfaze)) { if (declaresInHierarchy(targetMethod, interfaze)) { return true; } } } final TypeDescription.Generic superClass = type.getSuperClass(); if (superClass != null && superClassMatcher.matches(superClass.asErasure())) { return declaresInHierarchy(targetMethod, superClass.asErasure()); } return false; }
@Override public boolean matches(TypeDescription targetInterface) { if (ElementMatchers.declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures()))) .matches(targetInterface)) { return true; } else { for (TypeDescription typeDescription : targetInterface.getInterfaces().asErasures()) { if (matches(typeDescription)) { return true; } } } return false; } }
@Override public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader) { if (DEBUG_INSTRUMENTATION && typeDescription.getName().contains("DataSource")) { if (getTypeMatcher().matches(typeDescription)) { final boolean classLoaderMatches = getClassLoaderMatcher().matches(classLoader); logger.warn("IGNORE {} in {}. ClassLoader: {} matches: {}", typeDescription.getName(), getClass().getSimpleName(), classLoader, classLoaderMatches); if (!classLoaderMatches) { logger.warn("excluded by classloader matcher"); } else { logger.warn("excluded by global matcher"); } } else { logger.info("IGNORE {} in {}", typeDescription.getName(), getClass().getSimpleName()); } } }
/** * Makes sure that this transformer is only applied on Freemarker versions >= 2.3.23 where the * {@link Environment#getCurrentTemplate()} method was made public. This prevents nasty * {@link IllegalAccessError}s and {@link NoSuchMethodError}s. */ @Override public boolean isActive() { try { return hasMethodThat(named("getCurrentTemplate") .and(ElementMatchers.<MethodDescription.InDefinedShape>isPublic()) .and(takesArguments(0))) .matches(new TypeDescription.ForLoadedType(Class.forName("freemarker.core.Environment"))); } catch (ClassNotFoundException e) { return false; } }
@Nullable private Object getAnnotationValue(MethodDescription instrumentedMethod, AnnotationValueExtractor annotationValueExtractor) { for (TypeDescription typeDescription : instrumentedMethod.getDeclaredAnnotations().asTypeList()) { if (named(annotationValueExtractor.annotationClassName()).matches(typeDescription)) { for (MethodDescription.InDefinedShape annotationMethod : typeDescription.getDeclaredMethods()) { if (annotationMethod.getName().equals(annotationValueExtractor.method())) { return instrumentedMethod.getDeclaredAnnotations().ofType(typeDescription).getValue(annotationMethod).resolve(); } } } } return null; }
@Test void testIncludedPackages() { final TypeDescription thisClass = TypeDescription.ForLoadedType.of(getClass()); assertThat(isInAnyPackage(List.of(), none()).matches(thisClass)).isFalse(); assertThat(isInAnyPackage(List.of(thisClass.getPackage().getName()), none()).matches(thisClass)).isTrue(); assertThat(isInAnyPackage(List.of(thisClass.getPackage().getName()), none()).matches(TypeDescription.ForLoadedType.of(Object.class))).isFalse(); }
@Override public final boolean matches(final TypeDescription target) { return hasAnnotation(annotationType(annotation)).matches(target); } }
@Test void testClassLoaderCanLoadClass() { assertThat(classLoaderCanLoadClass(Object.class.getName()).matches(ClassLoader.getSystemClassLoader())).isTrue(); assertThat(classLoaderCanLoadClass(Object.class.getName()).matches(null)).isTrue(); assertThat(classLoaderCanLoadClass("not.Here").matches(ClassLoader.getSystemClassLoader())).isFalse(); } }
@Override public final boolean matches(TypeDescription target) { return target.isPrimitive() || target.isPrimitiveWrapper() || anyOf(JAVA_ATOMS).matches(target); } }
if (resolution.isResolved()) { TypeDescription typeDescription = resolution.resolve(); if (!ignoredTypeMatcher.matches(typeDescription)) { List<Plugin> applied = new ArrayList<Plugin>(), ignored = new ArrayList<Plugin>(); List<Throwable> errored = new ArrayList<Throwable>();
@Override public boolean matches(TypeDescription targetInterface) { if (ElementMatchers.declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures()))) .matches(targetInterface)) { return true; } else { for (TypeDescription typeDescription : targetInterface.getInterfaces().asErasures()) { if (matches(typeDescription)) { return true; } } } return false; } }
@Override public boolean matches(MethodDescription targetMethod) { TypeDescription superClass = targetMethod.getDeclaringType().asErasure(); do { superClass = superClass.getSuperClass().asErasure(); if (declaresMethod(named(targetMethod.getName()) .and(returns(targetMethod.getReturnType().asErasure())) .and(takesArguments(targetMethod.getParameters().asTypeList().asErasures())) .and(extraMethodMatcher)) .matches(superClass)) { return true; } } while (superClassMatcher.matches(superClass)); return false; } }
/** * Makes sure that this transformer is only applied on Freemarker versions {@code >= 2.3.23} where the * {@link Environment#getCurrentTemplate()} method was made public. This prevents nasty * {@link IllegalAccessError}s and {@link NoSuchMethodError}s. */ @Override public boolean isActive() { try { return hasMethodThat(named("getCurrentTemplate") .and(ElementMatchers.<MethodDescription.InDefinedShape>isPublic()) .and(takesArguments(0))) .matches(new TypeDescription.ForLoadedType(Class.forName("freemarker.core.Environment"))); } catch (ClassNotFoundException e) { return false; } }
private boolean filterCoreJavaClasses(ClassLoader classLoader, TypeDescription typeDescription) { if (transformsCoreJavaClasses()) { return true; } else { final boolean isCoreJavaClass = isBootstrapClassLoader().matches(classLoader) || nameStartsWith("java") .or(nameStartsWith("com.sun.")) .or(nameStartsWith("sun.")) .or(nameStartsWith("jdk.")).matches(typeDescription); return !isCoreJavaClass; } }
/** * Resolves a field locator for a potential accessor method. * * @param fieldLocator The field locator to use. * @param methodDescription The method description that is the potential accessor. * @return A resolution for a field locator. */ private static FieldLocator.Resolution resolveAccessor(FieldLocator fieldLocator, MethodDescription methodDescription) { String fieldName; if (isSetter().matches(methodDescription)) { fieldName = methodDescription.getInternalName().substring(3); } else if (isGetter().matches(methodDescription)) { fieldName = methodDescription.getInternalName().substring(methodDescription.getInternalName().startsWith("is") ? 2 : 3); } else { return FieldLocator.Resolution.Illegal.INSTANCE; } return fieldLocator.locate(Character.toLowerCase(fieldName.charAt(0)) + fieldName.substring(1)); }
@Override public MethodVisitor visitMethod(int modifiers, String internalName, String descriptor, String signature, String[] exception) { MethodDescription methodDescription = methods.get(internalName + descriptor); return methodDescription != null && methodMatcher.matches(methodDescription) ? REMOVE_METHOD : super.visitMethod(modifiers, internalName, descriptor, signature, exception); } }
@Override public FieldVisitor visitField(int modifiers, String internalName, String descriptor, String signature, Object value) { FieldDescription.InDefinedShape fieldDescription = fields.get(internalName + descriptor); return fieldDescription != null && fieldMatcher.matches(fieldDescription) ? REMOVE_FIELD : super.visitField(modifiers, internalName, descriptor, signature, value); }