From 2a06d462e1a920c3773b3a7afc62beb997dd2cf3 Mon Sep 17 00:00:00 2001 From: bdanglot Date: Mon, 17 Oct 2016 11:30:14 +0200 Subject: [PATCH] add a test case on shadow class: Object --- .../java/spoon/reflect/factory/Factory.java | 2 - .../spoon/reflect/factory/FactoryImpl.java | 10 ---- .../spoon/reflect/factory/ShadowFactory.java | 57 ------------------- .../spoon/reflect/factory/TypeFactory.java | 17 +++++- src/test/java/spoon/test/type/TypeTest.java | 28 +++++++++ 5 files changed, 43 insertions(+), 71 deletions(-) delete mode 100644 src/main/java/spoon/reflect/factory/ShadowFactory.java diff --git a/src/main/java/spoon/reflect/factory/Factory.java b/src/main/java/spoon/reflect/factory/Factory.java index c190840fd57..65d5201893b 100644 --- a/src/main/java/spoon/reflect/factory/Factory.java +++ b/src/main/java/spoon/reflect/factory/Factory.java @@ -60,6 +60,4 @@ public interface Factory { EvalFactory Eval(); // used 4 times ConstructorFactory Constructor(); // used 3 times - - ShadowFactory Shadow(); } diff --git a/src/main/java/spoon/reflect/factory/FactoryImpl.java b/src/main/java/spoon/reflect/factory/FactoryImpl.java index d446e13a727..c9818986dc0 100644 --- a/src/main/java/spoon/reflect/factory/FactoryImpl.java +++ b/src/main/java/spoon/reflect/factory/FactoryImpl.java @@ -255,16 +255,6 @@ public TypeFactory Type() { return type; } - private transient ShadowFactory shadow; - - @Override - public ShadowFactory Shadow() { - if (shadow == null) { - shadow = new ShadowFactory(this); - } - return shadow; - } - /** * A constructor that takes the parent factory */ diff --git a/src/main/java/spoon/reflect/factory/ShadowFactory.java b/src/main/java/spoon/reflect/factory/ShadowFactory.java deleted file mode 100644 index 12674588481..00000000000 --- a/src/main/java/spoon/reflect/factory/ShadowFactory.java +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright (C) 2006-2016 INRIA and contributors - * Spoon - http://spoon.gforge.inria.fr/ - * - * This software is governed by the CeCILL-C License under French law and - * abiding by the rules of distribution of free software. You can use, modify - * and/or redistribute the software under the terms of the CeCILL-C license as - * circulated by CEA, CNRS and INRIA at http://www.cecill.info. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. - * - * The fact that you are presently reading this means that you have had - * knowledge of the CeCILL-C license and that you accept its terms. - */ -package spoon.reflect.factory; - -import spoon.reflect.declaration.CtType; -import spoon.support.visitor.java.JavaReflectionTreeBuilder; - -import java.util.HashMap; -import java.util.Map; - -import static spoon.testing.utils.ModelUtils.createFactory; - -/** - * Created by bdanglot on 10/17/16. - */ -public class ShadowFactory extends SubFactory { - - private Map, CtType> shadowCache; - - /** - * The sub-factory constructor takes an instance of the parent factory. - * - * @param factory - */ - public ShadowFactory(Factory factory) { - super(factory); - this.shadowCache = new HashMap<>(); - } - - @SuppressWarnings("unchecked") - public CtType get(Class cl) { - final CtType shadowClass = (CtType) shadowCache.get(cl); - if (shadowClass == null) { - final CtType newShadowClass = new JavaReflectionTreeBuilder(createFactory()).scan((Class) cl); - this.shadowCache.put(cl, newShadowClass); - return newShadowClass; - } else { - return shadowClass; - } - } - - -} diff --git a/src/main/java/spoon/reflect/factory/TypeFactory.java b/src/main/java/spoon/reflect/factory/TypeFactory.java index d62257b5ec0..c879ab093fe 100644 --- a/src/main/java/spoon/reflect/factory/TypeFactory.java +++ b/src/main/java/spoon/reflect/factory/TypeFactory.java @@ -29,16 +29,21 @@ import spoon.reflect.visitor.filter.TypeFilter; import spoon.support.DefaultCoreFactory; import spoon.support.StandardEnvironment; +import spoon.support.visitor.java.JavaReflectionTreeBuilder; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import static spoon.testing.utils.ModelUtils.createFactory; + /** * The {@link CtType} sub-factory. */ @@ -73,6 +78,8 @@ public class TypeFactory extends SubFactory { public final CtTypeReference DATE = createReference(Date.class); public final CtTypeReference OBJECT = createReference(Object.class); + private final Map, CtType> shadowCache = new HashMap<>(); + /** * Returns a reference on the null type (type of null). */ @@ -462,12 +469,18 @@ private void addNestedType(List> list, CtType t) { public CtType get(Class cl) { final CtType aType = get(cl.getName()); if (aType == null) { - return factory.Shadow().get(cl); + final CtType shadowClass = (CtType) this.shadowCache.get(cl); + if (shadowClass == null) { + final CtType newShadowClass = new JavaReflectionTreeBuilder(createFactory()).scan((Class) cl); + this.shadowCache.put(cl, newShadowClass); + return newShadowClass; + } else { + return shadowClass; + } } return aType; } - /** * Gets the declaring type name for a given Java qualified name. */ diff --git a/src/test/java/spoon/test/type/TypeTest.java b/src/test/java/spoon/test/type/TypeTest.java index 29cc8fef89f..2fc764c479e 100644 --- a/src/test/java/spoon/test/type/TypeTest.java +++ b/src/test/java/spoon/test/type/TypeTest.java @@ -40,12 +40,15 @@ import spoon.test.type.testclasses.Pozole; import java.io.Serializable; +import java.lang.reflect.Method; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static spoon.testing.utils.ModelUtils.buildClass; @@ -294,4 +297,29 @@ public boolean matches(CtConstructorCall element) { }).get(0); assertEquals(launcher.getFactory().Type().objectType(), ctConstructorCall.getExecutable().getParameters().get(9)); } + + @Test + public void testShadowType() throws Exception { + + /* build a minimalist model to test shadow elements */ + + Launcher launcher = new Launcher(); + launcher.buildModel(); + + final CtClass objectCtClass = launcher.getFactory().Class().get(Object.class); + final CtClass objectCtClass1 = launcher.getFactory().Class().get(Object.class); + + assertSame(objectCtClass, objectCtClass1); + + assertTrue(objectCtClass.isShadow()); + assertEquals("java.lang.Object", objectCtClass.getQualifiedName()); + + final List methodNameList = Arrays.asList(Object.class.getDeclaredMethods()).stream().map(Method::getName).collect(Collectors.toList()); + + for (CtMethod ctMethod : objectCtClass.getMethods()) { + assertTrue(methodNameList.contains(ctMethod.getSimpleName())); + assertTrue(ctMethod.getBody().getStatements().isEmpty()); + } + + } }