jps model: references cleaned & obtaining typed libraries from collections

This commit is contained in:
nik
2012-08-23 15:52:31 +04:00
parent 0c436985d7
commit 2e27d6fb89
8 changed files with 56 additions and 33 deletions
@@ -1,6 +1,7 @@
package org.jetbrains.jps.model.library;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.JpsNamedElement;
import org.jetbrains.jps.model.JpsReferenceableElement;
@@ -32,6 +33,10 @@ public interface JpsLibrary extends JpsNamedElement, JpsReferenceableElement<Jps
@NotNull
JpsLibraryType<?> getType();
@Nullable
<P extends JpsElement>
JpsTypedLibrary<P> asTyped(@NotNull JpsLibraryType<P> type);
@NotNull
JpsElement getProperties();
@@ -22,6 +22,10 @@ public interface JpsLibraryCollection {
@NotNull
List<JpsLibrary> getLibraries();
@NotNull
<P extends JpsElement>
Iterable<JpsTypedLibrary<P>> getLibraries(@NotNull JpsLibraryType<P> type);
void addLibrary(@NotNull JpsLibrary library);
@Nullable
@@ -12,21 +12,15 @@ import java.util.List;
public abstract class JpsNamedElementReferenceBase<S extends JpsNamedElement, T extends JpsNamedElement, Self extends JpsNamedElementReferenceBase<S, T, Self>>
extends JpsCompositeElementBase<Self> implements JpsElementReference<T> {
private static final JpsElementChildRole<JpsElementReference<? extends JpsCompositeElement>> PARENT_REFERENCE_ROLE = JpsElementChildRoleBase.create("parent");
protected final JpsElementCollectionRole<? extends S> myCollectionRole;
protected final String myElementName;
protected JpsNamedElementReferenceBase(@NotNull JpsElementCollectionRole<? extends S> role,
@NotNull String elementName,
@NotNull JpsElementReference<? extends JpsCompositeElement> parentReference) {
super();
myCollectionRole = role;
protected JpsNamedElementReferenceBase(@NotNull String elementName, @NotNull JpsElementReference<? extends JpsCompositeElement> parentReference) {
myElementName = elementName;
myContainer.setChild(PARENT_REFERENCE_ROLE, parentReference);
}
protected JpsNamedElementReferenceBase(JpsNamedElementReferenceBase<S, T, Self> original) {
super(original);
myCollectionRole = original.myCollectionRole;
myElementName = original.myElementName;
}
@@ -35,7 +29,7 @@ public abstract class JpsNamedElementReferenceBase<S extends JpsNamedElement, T
final JpsCompositeElement parent = getParentReference().resolve();
if (parent == null) return null;
JpsElementCollectionImpl<? extends S> collection = parent.getContainer().getChild(myCollectionRole);
JpsElementCollectionImpl<? extends S> collection = getCollection(parent);
if (collection == null) return null;
final List<? extends S> elements = collection.getElements();
@@ -50,10 +44,19 @@ public abstract class JpsNamedElementReferenceBase<S extends JpsNamedElement, T
return null;
}
@Nullable
protected abstract JpsElementCollectionImpl<? extends S> getCollection(@NotNull JpsCompositeElement parent);
@Nullable
protected abstract T resolve(S element);
public JpsElementReference<? extends JpsCompositeElement> getParentReference() {
return myContainer.getChild(PARENT_REFERENCE_ROLE);
}
@Override
public JpsElementReference<T> asExternal(@NotNull JpsModel model) {
model.registerExternalReference(this);
return this;
}
}
@@ -1,6 +1,7 @@
package org.jetbrains.jps.model.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.JpsCompositeElement;
import org.jetbrains.jps.model.JpsElementReference;
import org.jetbrains.jps.model.JpsNamedElement;
@@ -9,17 +10,26 @@ import org.jetbrains.jps.model.JpsNamedElement;
* @author nik
*/
public abstract class JpsNamedElementReferenceImpl<T extends JpsNamedElement, Self extends JpsNamedElementReferenceImpl<T, Self>> extends JpsNamedElementReferenceBase<T, T, Self> {
protected final JpsElementCollectionRole<? extends T> myCollectionRole;
protected JpsNamedElementReferenceImpl(@NotNull JpsElementCollectionRole<? extends T> role, @NotNull String elementName,
@NotNull JpsElementReference<? extends JpsCompositeElement> parentReference) {
super(role, elementName, parentReference);
super(elementName, parentReference);
myCollectionRole = role;
}
protected JpsNamedElementReferenceImpl(JpsNamedElementReferenceImpl<T, Self> original) {
super(original);
myCollectionRole = original.myCollectionRole;
}
@Override
protected T resolve(T element) {
return element;
}
@Nullable
protected JpsElementCollectionImpl<? extends T> getCollection(@NotNull JpsCompositeElement parent) {
return parent.getContainer().getChild(myCollectionRole);
}
}
@@ -2,8 +2,8 @@ package org.jetbrains.jps.model.library.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.JpsElementCollection;
import org.jetbrains.jps.model.JpsElementTypeWithDefaultProperties;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
import org.jetbrains.jps.model.library.JpsLibrary;
import org.jetbrains.jps.model.library.JpsLibraryCollection;
import org.jetbrains.jps.model.library.JpsLibraryType;
@@ -15,9 +15,9 @@ import java.util.List;
* @author nik
*/
public class JpsLibraryCollectionImpl implements JpsLibraryCollection {
private final JpsElementCollection<JpsLibrary> myCollection;
private final JpsElementCollectionImpl<JpsLibrary> myCollection;
public JpsLibraryCollectionImpl(JpsElementCollection<JpsLibrary> collection) {
public JpsLibraryCollectionImpl(JpsElementCollectionImpl<JpsLibrary> collection) {
myCollection = collection;
}
@@ -41,6 +41,12 @@ public class JpsLibraryCollectionImpl implements JpsLibraryCollection {
return myCollection.getElements();
}
@NotNull
@Override
public <P extends JpsElement> Iterable<JpsTypedLibrary<P>> getLibraries(@NotNull JpsLibraryType<P> type) {
return myCollection.getElementsOfType(type);
}
@Override
public void addLibrary(@NotNull JpsLibrary library) {
myCollection.addChild(library);
@@ -2,6 +2,7 @@ package org.jetbrains.jps.model.library.impl;
import com.intellij.openapi.util.io.FileUtilRt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.JpsPathUtil;
import org.jetbrains.jps.model.*;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
@@ -37,6 +38,13 @@ public class JpsLibraryImpl<P extends JpsElement> extends JpsNamedCompositeEleme
return myLibraryType;
}
@Nullable
@Override
public <P extends JpsElement> JpsTypedLibrary<P> asTyped(@NotNull JpsLibraryType<P> type) {
//noinspection unchecked
return myLibraryType.equals(type) ? (JpsTypedLibrary<P>)this : null;
}
@NotNull
@Override
public P getProperties() {
@@ -1,10 +1,11 @@
package org.jetbrains.jps.model.library.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.JpsCompositeElement;
import org.jetbrains.jps.model.JpsElement;
import org.jetbrains.jps.model.JpsElementReference;
import org.jetbrains.jps.model.JpsModel;
import org.jetbrains.jps.model.impl.JpsElementCollectionImpl;
import org.jetbrains.jps.model.impl.JpsNamedElementReferenceBase;
import org.jetbrains.jps.model.library.JpsLibrary;
import org.jetbrains.jps.model.library.JpsTypedLibrary;
@@ -18,10 +19,9 @@ import org.jetbrains.jps.model.library.sdk.JpsSdkType;
public class JpsSdkReferenceImpl<P extends JpsElement> extends JpsNamedElementReferenceBase<JpsLibrary, JpsTypedLibrary<JpsSdk<P>>, JpsSdkReferenceImpl<P>> implements JpsSdkReference<P> {
private final JpsSdkType<P> mySdkType;
public JpsSdkReferenceImpl(@NotNull String elementName,
@NotNull JpsSdkType<P> sdkType,
public JpsSdkReferenceImpl(@NotNull String elementName, @NotNull JpsSdkType<P> sdkType,
@NotNull JpsElementReference<? extends JpsCompositeElement> parentReference) {
super(JpsLibraryRole.LIBRARIES_COLLECTION_ROLE, elementName, parentReference);
super(elementName, parentReference);
mySdkType = sdkType;
}
@@ -37,11 +37,7 @@ public class JpsSdkReferenceImpl<P extends JpsElement> extends JpsNamedElementRe
@Override
protected JpsTypedLibrary<JpsSdk<P>> resolve(JpsLibrary element) {
if (element.getType().equals(mySdkType)) {
//noinspection unchecked
return (JpsTypedLibrary<JpsSdk<P>>)element;
}
return null;
return element.asTyped(mySdkType);
}
@NotNull
@@ -50,9 +46,8 @@ public class JpsSdkReferenceImpl<P extends JpsElement> extends JpsNamedElementRe
return new JpsSdkReferenceImpl<P>(this);
}
@Override
public JpsSdkReferenceImpl<P> asExternal(@NotNull JpsModel model) {
model.registerExternalReference(this);
return this;
@Nullable
protected JpsElementCollectionImpl<? extends JpsLibrary> getCollection(@NotNull JpsCompositeElement parent) {
return parent.getContainer().getChild(JpsLibraryRole.LIBRARIES_COLLECTION_ROLE);
}
}
@@ -1,8 +1,6 @@
package org.jetbrains.jps.model.module.impl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.model.JpsElementReference;
import org.jetbrains.jps.model.JpsModel;
import org.jetbrains.jps.model.impl.JpsNamedElementReferenceImpl;
import org.jetbrains.jps.model.module.JpsFacet;
import org.jetbrains.jps.model.module.JpsFacetReference;
@@ -25,10 +23,4 @@ public class JpsFacetReferenceImpl extends JpsNamedElementReferenceImpl<JpsFacet
public JpsFacetReferenceImpl createCopy() {
return new JpsFacetReferenceImpl(this);
}
@Override
public JpsElementReference<JpsFacet> asExternal(@NotNull JpsModel model) {
model.registerExternalReference(this);
return this;
}
}