CPD Results
The following document contains the results of PMD's CPD 6.55.0.
Duplications
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 320 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 343 |
} /** * sometimes the parent element might contain the correct value so generalize that access * * TODO find out where this is probably done elsewhere */ private static String resolveFromRootThenParent(Xpp3Dom pluginPomDom, String element) throws Exception { return Optional.ofNullable(child(pluginPomDom, element).orElseGet(() -> child(pluginPomDom, "parent") .flatMap(e -> child(e, element)) .orElse(null))) .map(Xpp3Dom::getValue) .orElseThrow(() -> new Exception("unable to determine " + element)); } /** * Convenience method to obtain the value of a variable on a mojo that might not have a getter. * <br> * NOTE: the caller is responsible for casting to what the desired type is. */ public static Object getVariableValueFromObject(Object object, String variable) throws IllegalAccessException { Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses(variable, object.getClass()); field.setAccessible(true); return field.get(object); } /** * Convenience method to obtain all variables and values from the mojo (including its superclasses) * <br> * Note: the values in the map are of type Object so the caller is responsible for casting to desired types. */ public static Map<String, Object> getVariablesAndValuesFromObject(Object object) throws IllegalAccessException { return getVariablesAndValuesFromObject(object.getClass(), object); } /** * Convenience method to obtain all variables and values from the mojo (including its superclasses) * <br> * Note: the values in the map are of type Object so the caller is responsible for casting to desired types. * * @return map of variable names and values */ public static Map<String, Object> getVariablesAndValuesFromObject(Class<?> clazz, Object object) throws IllegalAccessException { Map<String, Object> map = new HashMap<>(); Field[] fields = clazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (Field field : fields) { map.put(field.getName(), field.get(object)); } Class<?> superclass = clazz.getSuperclass(); if (!Object.class.equals(superclass)) { map.putAll(getVariablesAndValuesFromObject(superclass, object)); } return map; } /** * Convenience method to set values to variables in objects that don't have setters */ public static void setVariableValueToObject(Object object, String variable, Object value) throws IllegalAccessException { Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses(variable, object.getClass()); Objects.requireNonNull(field, "Field " + variable + " not found"); field.setAccessible(true); field.set(object, value); } static class WrapEvaluator implements TypeAwareExpressionEvaluator { private final PlexusContainer container; private final TypeAwareExpressionEvaluator evaluator; WrapEvaluator(PlexusContainer container, TypeAwareExpressionEvaluator evaluator) { this.container = container; this.evaluator = evaluator; } @Override public Object evaluate(String expression) throws ExpressionEvaluationException { return evaluate(expression, null); } @Override public Object evaluate(String expression, Class<?> type) throws ExpressionEvaluationException { Object value = evaluator.evaluate(expression, type); if (value == null) { String expr = stripTokens(expression); if (expr != null) { try { value = container.lookup(type, expr); } catch (ComponentLookupException e) { // nothing } } } return value; } private String stripTokens(String expr) { if (expr.startsWith("${") && expr.endsWith("}")) { return expr.substring(2, expr.length() - 1); } return null; } @Override public File alignToBaseDirectory(File path) { return evaluator.alignToBaseDirectory(path); } } } |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 91 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 93 |
public class MojoExtension extends PlexusExtension implements ParameterResolver { @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { return parameterContext.isAnnotated(InjectMojo.class) || parameterContext.getDeclaringExecutable().isAnnotationPresent(InjectMojo.class); } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { try { InjectMojo injectMojo = parameterContext .findAnnotation(InjectMojo.class) .orElseGet(() -> parameterContext.getDeclaringExecutable().getAnnotation(InjectMojo.class)); Set<MojoParameter> mojoParameters = new HashSet<>(parameterContext.findRepeatableAnnotations(MojoParameter.class)); Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameter.class)) .ifPresent(mojoParameters::add); Optional.ofNullable(parameterContext.getDeclaringExecutable().getAnnotation(MojoParameters.class)) .map(MojoParameters::value) .map(Arrays::asList) .ifPresent(mojoParameters::addAll); Class<?> holder = parameterContext.getTarget().get().getClass(); PluginDescriptor descriptor = extensionContext .getStore(ExtensionContext.Namespace.GLOBAL) .get(PluginDescriptor.class, PluginDescriptor.class); return lookupMojo(holder, injectMojo, mojoParameters, descriptor); } catch (Exception e) { throw new ParameterResolutionException("Unable to resolve parameter", e); } } @Override public void beforeEach(ExtensionContext context) throws Exception { // TODO provide protected setters in PlexusExtension Field field = PlexusExtension.class.getDeclaredField("basedir"); field.setAccessible(true); field.set(null, getBasedir()); field = PlexusExtension.class.getDeclaredField("context"); field.setAccessible(true); field.set(this, context); getContainer().addComponent(getContainer(), PlexusContainer.class.getName()); ((DefaultPlexusContainer) getContainer()).addPlexusInjector(Collections.emptyList(), binder -> { binder.install(ProviderMethodsModule.forObject(context.getRequiredTestInstance())); binder.requestInjection(context.getRequiredTestInstance()); binder.bind(Log.class).toInstance(new DefaultLog(LoggerFactory.getLogger("anonymous"))); |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 164 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 189 |
} protected String getPluginDescriptorLocation() { return "META-INF/maven/plugin.xml"; } private Mojo lookupMojo( Class<?> holder, InjectMojo injectMojo, Collection<MojoParameter> mojoParameters, PluginDescriptor descriptor) throws Exception { String goal = injectMojo.goal(); String pom = injectMojo.pom(); String[] coord = mojoCoordinates(goal); Xpp3Dom pomDom; if (pom.startsWith("file:")) { Path path = Paths.get(getBasedir()).resolve(pom.substring("file:".length())); pomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(path.toFile())); } else if (pom.startsWith("classpath:")) { URL url = holder.getResource(pom.substring("classpath:".length())); if (url == null) { throw new IllegalStateException("Unable to find pom on classpath: " + pom); } pomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(url.openStream())); } else if (pom.contains("<project>")) { pomDom = Xpp3DomBuilder.build(new StringReader(pom)); } else { Path path = Paths.get(getBasedir()).resolve(pom); pomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(path.toFile())); } |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/ResolverExpressionEvaluatorStub.java | 52 |
org/apache/maven/plugin/testing/ResolverExpressionEvaluatorStub.java | 37 |
public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationException { Object value = null; if (expr == null) { return null; } String expression = stripTokens(expr); if (expression.equals(expr)) { int index = expr.indexOf("${"); if (index >= 0) { int lastIndex = expr.indexOf("}", index); if (lastIndex >= 0) { String retVal = expr.substring(0, index); if (index > 0 && expr.charAt(index - 1) == '$') { retVal += expr.substring(index + 1, lastIndex + 1); } else { retVal += evaluate(expr.substring(index, lastIndex + 1)); } retVal += evaluate(expr.substring(lastIndex + 1)); return retVal; } } |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 144 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 148 |
binder.bind(Log.class).toInstance(new DefaultLog(LoggerFactory.getLogger("anonymous"))); }); Map<Object, Object> map = getContainer().getContext().getContextData(); ClassLoader classLoader = context.getRequiredTestClass().getClassLoader(); try (InputStream is = Objects.requireNonNull( classLoader.getResourceAsStream(getPluginDescriptorLocation()), "Unable to find plugin descriptor: " + getPluginDescriptorLocation()); Reader reader = new BufferedReader(new XmlStreamReader(is)); InterpolationFilterReader interpolationReader = new InterpolationFilterReader(reader, map, "${", "}")) { PluginDescriptor pluginDescriptor = new PluginDescriptorBuilder().build(interpolationReader); context.getStore(ExtensionContext.Namespace.GLOBAL).put(PluginDescriptor.class, pluginDescriptor); for (ComponentDescriptor<?> desc : pluginDescriptor.getComponents()) { getContainer().addComponentDescriptor(desc); } } } |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 201 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 231 |
pluginConfiguration = XmlNode.merge(config, pluginConfiguration); } Mojo mojo = lookupMojo(coord, pluginConfiguration, descriptor); return mojo; } protected String[] mojoCoordinates(String goal) throws Exception { if (goal.matches(".*:.*:.*:.*")) { return goal.split(":"); } else { Path pluginPom = Paths.get(getBasedir(), "pom.xml"); Xpp3Dom pluginPomDom = Xpp3DomBuilder.build(ReaderFactory.newXmlReader(pluginPom.toFile())); String artifactId = pluginPomDom.getChild("artifactId").getValue(); String groupId = resolveFromRootThenParent(pluginPomDom, "groupId"); String version = resolveFromRootThenParent(pluginPomDom, "version"); return new String[] {groupId, artifactId, version, goal}; } } /** * lookup the mojo while we have all the relevent information */ protected Mojo lookupMojo(String[] coord, XmlNode pluginConfiguration, PluginDescriptor descriptor) |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 362 |
org/apache/maven/plugin/testing/AbstractMojoTestCase.java | 638 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 385 |
public static Map<String, Object> getVariablesAndValuesFromObject(Class<?> clazz, Object object) throws IllegalAccessException { Map<String, Object> map = new HashMap<>(); Field[] fields = clazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (Field field : fields) { map.put(field.getName(), field.get(object)); } Class<?> superclass = clazz.getSuperclass(); if (!Object.class.equals(superclass)) { map.putAll(getVariablesAndValuesFromObject(superclass, object)); } return map; } |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 223 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 253 |
protected Mojo lookupMojo(String[] coord, XmlNode pluginConfiguration, PluginDescriptor descriptor) throws Exception { // pluginkey = groupId : artifactId : version : goal Mojo mojo = lookup(Mojo.class, coord[0] + ":" + coord[1] + ":" + coord[2] + ":" + coord[3]); for (MojoDescriptor mojoDescriptor : descriptor.getMojos()) { if (Objects.equals( mojoDescriptor.getImplementation(), mojo.getClass().getName())) { if (pluginConfiguration != null) { pluginConfiguration = finalizeConfig(pluginConfiguration, mojoDescriptor); } } } if (pluginConfiguration != null) { |
File | Line |
---|---|
org/apache/maven/api/plugin/testing/MojoExtension.java | 308 |
org/apache/maven/plugin/testing/junit5/MojoExtension.java | 331 |
public static XmlNode extractPluginConfiguration(String artifactId, Xpp3Dom pomDom) throws Exception { Xpp3Dom pluginConfigurationElement = child(pomDom, "build") .flatMap(buildElement -> child(buildElement, "plugins")) .map(MojoExtension::children) .orElseGet(Stream::empty) .filter(e -> e.getChild("artifactId").getValue().equals(artifactId)) .findFirst() .flatMap(buildElement -> child(buildElement, "configuration")) .orElseThrow( () -> new ConfigurationException("Cannot find a configuration element for a plugin with an " + "artifactId of " + artifactId + ".")); return pluginConfigurationElement.getDom(); |