Skip to content

Commit

Permalink
fix: calls to default constructors outside the factory should still b…
Browse files Browse the repository at this point in the history
…e possible, and a default factory is made available (#1448)
  • Loading branch information
surli authored and monperrus committed Jun 29, 2017
1 parent b509a5c commit d835b8e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.ParentNotInitializedException;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.FactoryImpl;
import spoon.reflect.path.CtRole;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
Expand All @@ -37,6 +38,8 @@
import spoon.reflect.visitor.chain.CtFunction;
import spoon.reflect.visitor.chain.CtQuery;
import spoon.reflect.visitor.filter.AnnotationFilter;
import spoon.support.DefaultCoreFactory;
import spoon.support.StandardEnvironment;
import spoon.support.util.EmptyClearableList;
import spoon.support.util.EmptyClearableSet;
import spoon.support.visitor.HashcodeVisitor;
Expand Down Expand Up @@ -70,6 +73,8 @@ public abstract class CtElementImpl implements CtElement, Serializable {
private static final long serialVersionUID = 1L;
protected static final Logger LOGGER = Logger.getLogger(CtElementImpl.class);
public static final String ERROR_MESSAGE_TO_STRING = "Error in printing the node. One parent isn't initialized!";
private static final Factory DEFAULT_FACTORY = new FactoryImpl(new DefaultCoreFactory(), new StandardEnvironment());


public static <T> List<T> emptyList() {
return EmptyClearableList.instance();
Expand Down Expand Up @@ -379,6 +384,9 @@ public void updateAllParentsBelow() {

@Override
public Factory getFactory() {
if (this.factory == null) {
return DEFAULT_FACTORY;
}
return factory;
}

Expand Down
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/ctElement/ElementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spoon.test.ctElement;

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtElement;
import spoon.support.reflect.declaration.CtAnnotationImpl;
import spoon.support.reflect.declaration.CtMethodImpl;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;

/**
* Created by urli on 28/06/2017.
*/
public class ElementTest {

@Test
public void testGetFactory() {
// contract: getFactory should always return an object
// even if an element is created via its constructor
// and not through the factory

Launcher spoon = new Launcher();

CtElement element = spoon.getFactory().createAnnotation();
assertNotNull(element.getFactory());

CtElement otherElement = new CtAnnotationImpl<>();
assertNotNull(otherElement.getFactory());

CtElement yetAnotherOne = new CtMethodImpl<>();
assertNotNull(yetAnotherOne.getFactory());

// contract: a singleton is used for the default factory
assertSame(otherElement.getFactory(), yetAnotherOne.getFactory());
}
}

0 comments on commit d835b8e

Please sign in to comment.