Search in sources :

Example 11 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class PackageFactory method createReference.

/**
 * Creates a reference to a package. The given name has to be a fully qualified name.
 *
 * @param name
 * 		full name of the package to reference
 */
public CtPackageReference createReference(String name) {
    CtPackageReference ref = factory.Core().createPackageReference();
    ref.setSimpleName(name);
    return ref;
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference)

Example 12 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class MetaModelTest method singleValueRoleAddSetRemove.

@Test
public void singleValueRoleAddSetRemove() {
    // contract: single value roles supports multivalue interface too
    Launcher launcher = new Launcher();
    Factory factory = launcher.getFactory();
    CtTypeReference<?> typeRef = factory.Type().createReference("some.test.package.TestType");
    RoleHandler rh = RoleHandlerHelper.getRoleHandler(typeRef.getClass(), CtRole.PACKAGE_REF);
    // contract: single value role provides a List
    List<CtPackageReference> packages = rh.asList(typeRef);
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: adding of existing value fails and changes nothing
    try {
        packages.add(typeRef.getPackage());
        fail();
    } catch (Exception e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: adding of null fails and changes nothing
    try {
        assertFalse(packages.add(null));
        fail();
    } catch (Exception e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: adding of different value fails, and changes nothing
    try {
        packages.add(factory.Package().createReference("some.test.another_package"));
        fail();
    } catch (SpoonException e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract remove of different value changes nothing
    assertFalse(packages.remove(factory.Package().createReference("some.test.another_package")));
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract remove of null value changes nothing
    assertFalse(packages.remove(null));
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract remove of existing value sets value to null and size to 0
    assertTrue(packages.remove(factory.Package().createReference("some.test.package")));
    assertListContracts(packages, typeRef, 0, null);
    // contract add of null into empty collection changes size to 1, but value is still null
    assertTrue(packages.add(null));
    assertListContracts(packages, typeRef, 1, null);
    // contract: adding of new value into collection with single null value fails and changes nothing
    try {
        packages.add(factory.Package().createReference("some.test.another_package"));
        fail();
    } catch (SpoonException e) {
    // OK
    }
    assertListContracts(packages, typeRef, 1, null);
    // contract: set of new value replaces existing value
    assertEquals(null, packages.set(0, factory.Package().createReference("some.test.package")));
    assertListContracts(packages, typeRef, 1, "some.test.package");
    // contract: set of null value keeps size==1 even if value is replaced by null
    assertEquals("some.test.package", packages.set(0, null).getQualifiedName());
    assertListContracts(packages, typeRef, 1, null);
    // contract: remove of null value by index sets size==0 the value is still null
    assertNull(packages.remove(0));
    assertListContracts(packages, typeRef, 0, null);
    // contract: add of null value sets size==1 the value is still null
    assertTrue(packages.add(null));
    assertListContracts(packages, typeRef, 1, null);
    // contract: remove of null value by value sets size==0 the value is still null
    assertTrue(packages.remove(null));
    assertListContracts(packages, typeRef, 0, null);
    // contract: set of new value on empty collection fails with IndexOutOfBounds and changes nothing
    try {
        packages.set(0, factory.Package().createReference("some.test.another_package"));
        fail();
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    assertListContracts(packages, typeRef, 0, null);
    // contract: adding of value into empty collection adds value
    assertTrue(packages.add(factory.Package().createReference("some.test.another_package")));
    assertListContracts(packages, typeRef, 1, "some.test.another_package");
    // contract: remove of value by index from collection removes that value
    assertEquals("some.test.another_package", packages.remove(0).getQualifiedName());
    assertListContracts(packages, typeRef, 0, null);
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference) SpoonException(spoon.SpoonException) RoleHandler(spoon.reflect.meta.RoleHandler) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) SpoonException(spoon.SpoonException) Test(org.junit.Test)

Example 13 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class JDTTreeBuilderHelper method createModuleExport.

CtPackageExport createModuleExport(OpensStatement opensStatement) {
    String packageName = new String(opensStatement.pkgName);
    int sourceStart = opensStatement.sourceStart;
    int sourceEnd = opensStatement.sourceEnd;
    CtPackageReference ctPackageReference = jdtTreeBuilder.references.getPackageReference(packageName);
    CtPackageExport moduleExport = jdtTreeBuilder.getFactory().Module().createPackageExport(ctPackageReference);
    if (opensStatement.targets != null && opensStatement.targets.length > 0) {
        List<CtModuleReference> moduleReferences = new ArrayList<>();
        for (ModuleReference moduleReference : opensStatement.targets) {
            moduleReferences.add(this.jdtTreeBuilder.references.getModuleReference(moduleReference));
        }
        moduleExport.setTargetExport(moduleReferences);
    }
    moduleExport.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPosition(sourceStart, sourceEnd));
    return moduleExport;
}
Also used : CtPackageExport(spoon.reflect.declaration.CtPackageExport) CtPackageReference(spoon.reflect.reference.CtPackageReference) ModuleReference(org.eclipse.jdt.internal.compiler.ast.ModuleReference) CtModuleReference(spoon.reflect.reference.CtModuleReference) CtModuleReference(spoon.reflect.reference.CtModuleReference) ArrayList(java.util.ArrayList)

Example 14 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class ReferenceBuilder method getTypeReference.

@SuppressWarnings("unchecked")
<T> CtTypeReference<T> getTypeReference(TypeBinding binding) {
    if (binding == null) {
        return null;
    }
    CtTypeReference<?> ref = null;
    if (binding instanceof RawTypeBinding) {
        ref = getTypeReference(((ParameterizedTypeBinding) binding).genericType());
    } else if (binding instanceof ParameterizedTypeBinding) {
        if (binding.actualType() != null && binding.actualType() instanceof LocalTypeBinding) {
            // When we define a nested class in a method and when the enclosing class of this method
            // is a parameterized type binding, JDT give a ParameterizedTypeBinding for the nested class
            // and hide the real class in actualType().
            ref = getTypeReference(binding.actualType());
        } else {
            ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
            this.exploringParameterizedBindings.put(binding, ref);
            if (binding.isAnonymousType()) {
                ref.setSimpleName("");
            } else {
                ref.setSimpleName(String.valueOf(binding.sourceName()));
                if (binding.enclosingType() != null) {
                    ref.setDeclaringType(getTypeReference(binding.enclosingType()));
                } else {
                    ref.setPackage(getPackageReference(binding.getPackage()));
                }
            }
        }
        if (binding.actualType() instanceof MissingTypeBinding) {
            ref = getTypeReference(binding.actualType());
        }
        if (((ParameterizedTypeBinding) binding).arguments != null) {
            for (TypeBinding b : ((ParameterizedTypeBinding) binding).arguments) {
                if (bindingCache.containsKey(b)) {
                    ref.addActualTypeArgument(getCtCircularTypeReference(b));
                } else {
                    if (!this.exploringParameterizedBindings.containsKey(b)) {
                        this.exploringParameterizedBindings.put(b, null);
                        CtTypeReference typeRefB = getTypeReference(b);
                        this.exploringParameterizedBindings.put(b, typeRefB);
                        ref.addActualTypeArgument(typeRefB);
                    } else {
                        CtTypeReference typeRefB = this.exploringParameterizedBindings.get(b);
                        if (typeRefB != null) {
                            ref.addActualTypeArgument(typeRefB.clone());
                        }
                    }
                }
            }
        }
    } else if (binding instanceof MissingTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.sourceName()));
        ref.setPackage(getPackageReference(binding.getPackage()));
        if (!this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports) {
            final CtReference declaring = this.getDeclaringReferenceFromImports(binding.sourceName());
            if (declaring instanceof CtPackageReference) {
                ref.setPackage((CtPackageReference) declaring);
            } else if (declaring instanceof CtTypeReference) {
                ref.setDeclaringType((CtTypeReference) declaring);
            }
        }
    } else if (binding instanceof BinaryTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.enclosingType() != null) {
            ref.setDeclaringType(getTypeReference(binding.enclosingType()));
        } else {
            ref.setPackage(getPackageReference(binding.getPackage()));
        }
        ref.setSimpleName(new String(binding.sourceName()));
    } else if (binding instanceof TypeVariableBinding) {
        boolean oldBounds = bounds;
        if (binding instanceof CaptureBinding) {
            ref = this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
            bounds = true;
        } else {
            TypeVariableBinding typeParamBinding = (TypeVariableBinding) binding;
            ReferenceBinding superClass = typeParamBinding.superclass;
            ReferenceBinding[] superInterfaces = typeParamBinding.superInterfaces();
            CtTypeReference refSuperClass = null;
            // superClass.superclass() is null if it's java.lang.Object
            if (superClass != null && !(superClass.superclass() == null)) {
                // to conserve the same behavior as JavaReflectionTreeBuilder
                if (!(superClass instanceof ParameterizedTypeBinding) || !this.exploringParameterizedBindings.containsKey(superClass)) {
                    refSuperClass = this.getTypeReference(superClass);
                }
            // if the type parameter has a super interface, then we'll get it too, as a superclass
            // type parameter can only extends an interface or a class, so we don't make the distinction
            // in Spoon. Moreover we can only have one extends in a type parameter.
            } else if (superInterfaces != null && superInterfaces.length == 1) {
                refSuperClass = this.getTypeReference(superInterfaces[0]);
            }
            ref = this.jdtTreeBuilder.getFactory().Core().createTypeParameterReference();
            ref.setSimpleName(new String(binding.sourceName()));
            if (refSuperClass != null) {
                ((CtTypeParameterReference) ref).addBound(refSuperClass);
            }
        }
        TypeVariableBinding b = (TypeVariableBinding) binding;
        if (bounds) {
            if (b instanceof CaptureBinding && ((CaptureBinding) b).wildcard != null) {
                bounds = oldBounds;
                return getTypeReference(((CaptureBinding) b).wildcard);
            } else if (b.superclass != null && b.firstBound == b.superclass) {
                bounds = false;
                bindingCache.put(binding, ref);
                ((CtTypeParameterReference) ref).setBoundingType(getTypeReference(b.superclass));
                bounds = oldBounds;
            }
        }
        if (bounds && b.superInterfaces != null && b.superInterfaces != Binding.NO_SUPERINTERFACES) {
            bounds = false;
            bindingCache.put(binding, ref);
            List<CtTypeReference<?>> bounds = new ArrayList<>();
            CtTypeParameterReference typeParameterReference = (CtTypeParameterReference) ref;
            if (!(typeParameterReference.isDefaultBoundingType())) {
                // if it's object we can ignore it
                bounds.add(typeParameterReference.getBoundingType());
            }
            for (ReferenceBinding superInterface : b.superInterfaces) {
                bounds.add(getTypeReference(superInterface));
            }
            ((CtTypeParameterReference) ref).setBoundingType(this.jdtTreeBuilder.getFactory().Type().createIntersectionTypeReferenceWithBounds(bounds));
        }
        if (binding instanceof CaptureBinding) {
            bounds = false;
        }
    } else if (binding instanceof BaseTypeBinding) {
        String name = new String(binding.sourceName());
        ref = basestypes.get(name);
        if (ref == null) {
            ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
            ref.setSimpleName(name);
            basestypes.put(name, ref);
        } else {
            ref = ref == null ? ref : ref.clone();
        }
    } else if (binding instanceof WildcardBinding) {
        WildcardBinding wildcardBinding = (WildcardBinding) binding;
        ref = this.jdtTreeBuilder.getFactory().Core().createWildcardReference();
        if (wildcardBinding.boundKind == Wildcard.SUPER && ref instanceof CtTypeParameterReference) {
            ((CtTypeParameterReference) ref).setUpper(false);
        }
        if (wildcardBinding.bound != null && ref instanceof CtTypeParameterReference) {
            if (bindingCache.containsKey(wildcardBinding.bound)) {
                ((CtTypeParameterReference) ref).setBoundingType(getCtCircularTypeReference(wildcardBinding.bound));
            } else {
                ((CtTypeParameterReference) ref).setBoundingType(getTypeReference(((WildcardBinding) binding).bound));
            }
        }
    } else if (binding instanceof LocalTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.isAnonymousType()) {
            ref.setSimpleName(JDTTreeBuilderHelper.computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
            ref.setDeclaringType(getTypeReference((binding.enclosingType())));
        } else {
            ref.setSimpleName(new String(binding.sourceName()));
            if (((LocalTypeBinding) binding).enclosingMethod == null && binding.enclosingType() != null && binding.enclosingType() instanceof LocalTypeBinding) {
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            } else if (binding.enclosingMethod() != null) {
                ref.setSimpleName(JDTTreeBuilderHelper.computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            }
        }
    } else if (binding instanceof SourceTypeBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        if (binding.isAnonymousType()) {
            ref.setSimpleName(JDTTreeBuilderHelper.computeAnonymousName(((SourceTypeBinding) binding).constantPoolName()));
            ref.setDeclaringType(getTypeReference((binding.enclosingType())));
        } else {
            ref.setSimpleName(new String(binding.sourceName()));
            if (binding.enclosingType() != null) {
                ref.setDeclaringType(getTypeReference(binding.enclosingType()));
            } else {
                ref.setPackage(getPackageReference(binding.getPackage()));
            }
        // if(((SourceTypeBinding) binding).typeVariables!=null &&
        // ((SourceTypeBinding) binding).typeVariables.length>0){
        // for (TypeBinding b : ((SourceTypeBinding)
        // binding).typeVariables) {
        // ref.getActualTypeArguments().add(getTypeReference(b));
        // }
        // }
        }
    } else if (binding instanceof ArrayBinding) {
        CtArrayTypeReference<Object> arrayref;
        arrayref = this.jdtTreeBuilder.getFactory().Core().createArrayTypeReference();
        ref = arrayref;
        for (int i = 1; i < binding.dimensions(); i++) {
            CtArrayTypeReference<Object> tmp = this.jdtTreeBuilder.getFactory().Core().createArrayTypeReference();
            arrayref.setComponentType(tmp);
            arrayref = tmp;
        }
        arrayref.setComponentType(getTypeReference(binding.leafComponentType()));
    } else if (binding instanceof PolyTypeBinding) {
        // JDT can't resolve the type of this binding and we only have a string.
        // In this case, we return a type Object because we can't know more about it.
        ref = this.jdtTreeBuilder.getFactory().Type().objectType();
    } else if (binding instanceof ProblemReferenceBinding) {
        // Spoon is able to analyze also without the classpath
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.readableName()));
        final CtReference declaring = this.getDeclaringReferenceFromImports(binding.sourceName());
        setPackageOrDeclaringType(ref, declaring);
    } else if (binding instanceof JDTTreeBuilder.SpoonReferenceBinding) {
        ref = this.jdtTreeBuilder.getFactory().Core().createTypeReference();
        ref.setSimpleName(new String(binding.sourceName()));
        ref.setDeclaringType(getTypeReference(binding.enclosingType()));
    } else if (binding instanceof IntersectionTypeBinding18) {
        List<CtTypeReference<?>> bounds = new ArrayList<>();
        for (ReferenceBinding superInterface : binding.getIntersectingTypes()) {
            bounds.add(getTypeReference(superInterface));
        }
        ref = this.jdtTreeBuilder.getFactory().Type().createIntersectionTypeReferenceWithBounds(bounds);
    } else {
        throw new RuntimeException("Unknown TypeBinding: " + binding.getClass() + " " + binding);
    }
    bindingCache.remove(binding);
    this.exploringParameterizedBindings.remove(binding);
    return (CtTypeReference<T>) ref;
}
Also used : ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) ArrayList(java.util.ArrayList) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) IntersectionTypeBinding18(org.eclipse.jdt.internal.compiler.lookup.IntersectionTypeBinding18) CaptureBinding(org.eclipse.jdt.internal.compiler.lookup.CaptureBinding) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtReference(spoon.reflect.reference.CtReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) List(java.util.List) ArrayList(java.util.ArrayList) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 15 with CtPackageReference

use of spoon.reflect.reference.CtPackageReference in project spoon by INRIA.

the class ReferenceBuilder method setPackageOrDeclaringType.

/**
 * Sets {@code declaring} as inner of {@code ref}, as either the package or the declaring type
 */
void setPackageOrDeclaringType(CtTypeReference<?> ref, CtReference declaring) {
    if (declaring instanceof CtPackageReference) {
        ref.setPackage((CtPackageReference) declaring);
    } else if (declaring instanceof CtTypeReference) {
        ref.setDeclaringType((CtTypeReference) declaring);
    } else if (declaring == null) {
        try {
            // sometimes JDT does not provide the information that ref comes from java.lang
            // it seems to occurs in particular with anonymous inner classes: see #1307
            // In that case, we try to load the class to check if it belongs to java.lang
            Class.forName("java.lang." + ref.getSimpleName());
            CtPackageReference javaLangPackageReference = this.jdtTreeBuilder.getFactory().Core().createPackageReference();
            javaLangPackageReference.setSimpleName("java.lang");
            ref.setPackage(javaLangPackageReference);
        } catch (ClassNotFoundException e) {
            // in that case we consider the package should be the same as the current one. Fix #1293
            ref.setPackage(jdtTreeBuilder.getContextBuilder().compilationUnitSpoon.getDeclaredPackage().getReference());
        }
    } else {
        throw new AssertionError("unexpected declaring type: " + declaring.getClass() + " of " + declaring);
    }
}
Also used : CtPackageReference(spoon.reflect.reference.CtPackageReference) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Aggregations

CtPackageReference (spoon.reflect.reference.CtPackageReference)18 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)4 CtClass (spoon.reflect.declaration.CtClass)3 CtField (spoon.reflect.declaration.CtField)3 CtReference (spoon.reflect.reference.CtReference)3 ModuleReference (org.eclipse.jdt.internal.compiler.ast.ModuleReference)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtElement (spoon.reflect.declaration.CtElement)2 CtImport (spoon.reflect.declaration.CtImport)2 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)1