Remove ruleContext attributes

Refs #2676

Was forgotten in #2672
This commit is contained in:
Clément Fournier
2020-08-25 15:16:30 +02:00
parent 285e7f2e94
commit 7a456e5b93
2 changed files with 1 additions and 145 deletions

View File

@ -5,8 +5,6 @@
package net.sourceforge.pmd;
import java.io.File;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
import net.sourceforge.pmd.lang.LanguageVersion;
@ -34,14 +32,13 @@ public class RuleContext {
private Report report = new Report();
private File sourceCodeFile;
private LanguageVersion languageVersion;
private final ConcurrentMap<String, Object> attributes;
private boolean ignoreExceptions = true;
/**
* Default constructor.
*/
public RuleContext() {
attributes = new ConcurrentHashMap<>();
// nothing to do
}
/**
@ -52,7 +49,6 @@ public class RuleContext {
* the context from which the values are shared
*/
public RuleContext(RuleContext ruleContext) {
this.attributes = ruleContext.attributes;
this.report.addListeners(ruleContext.getReport().getListeners());
}
@ -146,92 +142,6 @@ public class RuleContext {
this.languageVersion = languageVersion;
}
/**
* Set an attribute value on the RuleContext, if it does not already exist.
* <p>
* Attributes can be shared between RuleContext instances. This operation is
* thread-safe.
* <p>
* Attribute values should be modified directly via the reference provided.
* It is not necessary to call <code>setAttribute(String, Object)</code> to
* update an attribute value. Modifications made to the attribute value will
* automatically be seen by other threads. Because of this, you must ensure
* the attribute values are themselves thread safe.
*
* @param name
* The attribute name.
* @param value
* The attribute value.
* @exception IllegalArgumentException
* if <code>name</code> or <code> value</code> are
* <code>null</code>
* @return <code>true</code> if the attribute was set, <code>false</code>
* otherwise.
*
* @deprecated Stateful methods of the rule context will be removed.
* Their interaction with incremental analysis are unspecified.
*/
@Deprecated
public boolean setAttribute(String name, Object value) {
if (name == null) {
throw new IllegalArgumentException("Parameter 'name' cannot be null.");
}
if (value == null) {
throw new IllegalArgumentException("Parameter 'value' cannot be null.");
}
return this.attributes.putIfAbsent(name, value) == null;
}
/**
* Get an attribute value on the RuleContext.
* <p>
* Attributes can be shared between RuleContext instances. This operation is
* thread-safe.
* <p>
* Attribute values should be modified directly via the reference provided.
* It is not necessary to call <code>setAttribute(String, Object)</code> to
* update an attribute value. Modifications made to the attribute value will
* automatically be seen by other threads. Because of this, you must ensure
* the attribute values are themselves thread safe.
*
* @param name
* The attribute name.
* @return The current attribute value, or <code>null</code> if the
* attribute does not exist.
*
* @deprecated Stateful methods of the rule context will be removed.
* Their interaction with incremental analysis are unspecified.
*/
@Deprecated
public Object getAttribute(String name) {
return this.attributes.get(name);
}
/**
* Remove an attribute value on the RuleContext.
* <p>
* Attributes can be shared between RuleContext instances. This operation is
* thread-safe.
* <p>
* Attribute values should be modified directly via the reference provided.
* It is not necessary to call <code>setAttribute(String, Object)</code> to
* update an attribute value. Modifications made to the attribute value will
* automatically be seen by other threads. Because of this, you must ensure
* the attribute values are themselves thread safe.
*
* @param name
* The attribute name.
* @return The current attribute value, or <code>null</code> if the
* attribute does not exist.
*
* @deprecated Stateful methods of the rule context will be removed.
* Their interaction with incremental analysis are unspecified.
*/
@Deprecated
public Object removeAttribute(String name) {
return this.attributes.remove(name);
}
/**
* Configure whether exceptions during applying a rule should be ignored or
* not. If set to <code>true</code> then such exceptions are logged as

View File

@ -5,11 +5,7 @@
package net.sourceforge.pmd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.File;
@ -45,56 +41,6 @@ public class RuleContextTest {
assertEquals("filename mismatch", new File("somefile.java"), ctx.getSourceCodeFile());
}
@Test
public void testAttributes() {
RuleContext ctx1 = new RuleContext();
Object obj1 = new Object();
Object obj2 = new Object();
assertNull("attribute should be null", ctx1.getAttribute("attribute"));
boolean set = ctx1.setAttribute("attribute", obj1);
assertTrue("attribute should have been set", set);
assertNotNull("attribute should not be null", ctx1.getAttribute("attribute"));
assertSame("attribute should be expected instance", ctx1.getAttribute("attribute"), obj1);
set = ctx1.setAttribute("attribute", obj2);
assertFalse("attribute should not have been set", set);
assertSame("attribute should be expected instance", ctx1.getAttribute("attribute"), obj1);
Object value = ctx1.removeAttribute("attribute");
assertSame("attribute value should be expected instance", value, obj1);
assertNull("attribute should be null", ctx1.getAttribute("attribute"));
}
@Test
public void testSharedAttributes() {
RuleContext ctx1 = new RuleContext();
RuleContext ctx2 = new RuleContext(ctx1);
StringBuilder obj1 = new StringBuilder();
StringBuilder obj2 = new StringBuilder();
ctx1.setAttribute("attribute1", obj1);
ctx2.setAttribute("attribute2", obj2);
assertNotNull("attribute should not be null", ctx1.getAttribute("attribute1"));
assertNotNull("attribute should not be null", ctx1.getAttribute("attribute2"));
assertNotNull("attribute should not be null", ctx2.getAttribute("attribute1"));
assertNotNull("attribute should not be null", ctx2.getAttribute("attribute2"));
assertSame("attribute should be expected instance", ctx1.getAttribute("attribute1"), obj1);
assertSame("attribute should be expected instance", ctx1.getAttribute("attribute2"), obj2);
assertSame("attribute should be expected instance", ctx2.getAttribute("attribute1"), obj1);
assertSame("attribute should be expected instance", ctx2.getAttribute("attribute2"), obj2);
ctx1.removeAttribute("attribute1");
assertNull("attribute should be null", ctx1.getAttribute("attribute1"));
assertNull("attribute should be null", ctx2.getAttribute("attribute1"));
assertNotNull("attribute should not be null", ctx1.getAttribute("attribute2"));
assertNotNull("attribute should not be null", ctx2.getAttribute("attribute2"));
StringBuilder value = (StringBuilder) ctx1.getAttribute("attribute2");
assertEquals("attribute value should be empty", "", value.toString());
value.append("x");
StringBuilder value1 = (StringBuilder) ctx1.getAttribute("attribute2");
assertEquals("attribute value should be 'x'", "x", value1.toString());
StringBuilder value2 = (StringBuilder) ctx2.getAttribute("attribute2");
assertEquals("attribute value should be 'x'", "x", value2.toString());
}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(RuleContextTest.class);