Skip to content

Commit

Permalink
[FORGE-1248] Redesign parser generics API: permit management of type …
Browse files Browse the repository at this point in the history
…variable bounds
  • Loading branch information
mbenson committed Nov 20, 2013
1 parent a821d16 commit 978c071
Show file tree
Hide file tree
Showing 16 changed files with 469 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
* @author mbenson
*
*/
public interface GenericCapable
public interface GenericCapable<O extends JavaType<O>>
{
/**
* Returns all the generic types associated with this object
*/
List<String> getGenericTypes();
List<? extends TypeVariable<O>> getTypeVariables();

/**
* Returns the named {@link TypeVariable}.
* @param name
* @return TypeVariable or {@code null}
*/
TypeVariable<O> getTypeVariable(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface JavaClass<O extends JavaClass<O>> extends
InterfaceCapable,
FieldHolder<O>,
MethodHolder<O>,
GenericCapable,
GenericCapable<O>,
Extendable<O>,
Abstractable<O>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public interface JavaInterface<O extends JavaInterface<O>> extends
InterfaceCapable,
FieldHolder<O>,
MethodHolder<O>,
GenericCapable
GenericCapable<O>
{
}
5 changes: 3 additions & 2 deletions api/src/main/java/org/jboss/forge/parser/java/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*/
public interface Method<O extends JavaType<O>, T extends Method<O, T>> extends Abstractable<T>, Member<O>,
GenericCapable,
public interface Method<O extends JavaType<O>, T extends Method<O, T>> extends Abstractable<T>,
Member<O>,
GenericCapable<O>,
Origin<O>
{
/**
Expand Down
1 change: 0 additions & 1 deletion api/src/main/java/org/jboss/forge/parser/java/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public interface Type<O extends JavaType<O>> extends Origin<O>

public abstract boolean isWildcard();


}
28 changes: 28 additions & 0 deletions api/src/main/java/org/jboss/forge/parser/java/TypeVariable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.parser.java;

import java.util.List;

import org.jboss.forge.parser.Internal;
import org.jboss.forge.parser.Origin;

/**
* Represents a type variable of a {@link GenericCapable} {@link JavaType}.
*
* @author mbenson
*
*/
public interface TypeVariable<O extends JavaType<O>> extends Named, Internal, Origin<O>
{
/**
* Get the upper bounds of this type variable.
*
* @return (possibly empty) Type[]
*/
List<Type<O>> getBounds();
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
package org.jboss.forge.parser.java.source;

import java.util.List;

import org.jboss.forge.parser.java.GenericCapable;
import org.jboss.forge.parser.java.TypeVariable;

/**
* Represents a Java source element that may define type variables.
*
* @author mbenson
*
*
*/
public interface GenericCapableSource<T> extends GenericCapable
public interface GenericCapableSource<O extends JavaSource<O>, T> extends
GenericCapable<O>
{
@Override
List<TypeVariableSource<O>> getTypeVariables();

@Override
TypeVariableSource<O> getTypeVariable(String name);

/**
* Adds a type variable.
*
* @return {@link TypeVariableSource}
*/
TypeVariableSource<O> addTypeVariable();

/**
* Adds a generic type
* Removes a type variable.
*
* @param genericType should never be null
* @return
* @param name should never be null
* @return this
*/
T addGenericType(String genericType);
T removeTypeVariable(String name);

/**
* Removes a generic type
* Removes a type variable.
*
* @param genericType should never be null
* @return
* @param typeVariable should never be null
* @return this
*/
T removeGenericType(String genericType);
T removeTypeVariable(TypeVariable<?> typeVariable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface JavaClassSource extends JavaClass<JavaClassSource>,
InterfaceCapableSource<JavaClassSource>,
FieldHolderSource<JavaClassSource>,
MethodHolderSource<JavaClassSource>,
GenericCapableSource<JavaClassSource>,
GenericCapableSource<JavaClassSource, JavaClassSource>,
ExtendableSource<JavaClassSource>,
AbstractableSource<JavaClassSource>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface JavaInterfaceSource extends JavaInterface<JavaInterfaceSource>,
JavaSource<JavaInterfaceSource>,
FieldHolderSource<JavaInterfaceSource>,
MethodHolderSource<JavaInterfaceSource>,
GenericCapableSource<JavaInterfaceSource>,
GenericCapableSource<JavaInterfaceSource, JavaInterfaceSource>,
InterfaceCapableSource<JavaInterfaceSource>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*
*/
public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodSource<O>>,
AbstractableSource<MethodSource<O>>,
MemberSource<O, MethodSource<O>>, GenericCapableSource<MethodSource<O>>
public interface MethodSource<O extends JavaSource<O>> extends Method<O, MethodSource<O>>, AbstractableSource<MethodSource<O>>,
MemberSource<O, MethodSource<O>>, GenericCapableSource<O, MethodSource<O>>
{
/**
* Set this {@link Method} to return the given type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.parser.java.source;

import org.jboss.forge.parser.java.JavaType;
import org.jboss.forge.parser.java.TypeVariable;

/**
* Represents a type variable of a {@link GenericCapableSource} {@link JavaSource}.
*
* @author mbenson
*
*/
public interface TypeVariableSource<O extends JavaSource<O>> extends TypeVariable<O>,
NamedSource<TypeVariableSource<O>>
{
/**
* Set the bounds of this type variable.
*
* @param bounds
* @return this
*/
TypeVariableSource<O> setBounds(JavaType<?>... bounds);

/**
* Set the bounds of this type variable.
*
* @param bounds
* @return this
*/
TypeVariableSource<O> setBounds(Class<?>... bounds);

/**
* Set the bounds of this type variable.
*
* @param bounds
* @return this
*/
TypeVariableSource<O> setBounds(String... bounds);

/**
* Remove any bounds declared on this type variable.
*
* @return this
*/
TypeVariableSource<O> removeBounds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jface.text.Document;
import org.jboss.forge.parser.java.TypeVariable;
import org.jboss.forge.parser.java.source.GenericCapableSource;
import org.jboss.forge.parser.java.source.JavaSource;
import org.jboss.forge.parser.java.source.TypeVariableSource;
import org.jboss.forge.parser.java.util.Strings;

/**
*
* @author mbenson
*
*
* @param <O>
*/
@SuppressWarnings("unchecked")
public abstract class AbstractGenericCapableJavaSource<O extends JavaSource<O>> extends AbstractJavaSourceMemberHolder<O>
implements GenericCapableSource<O>
implements GenericCapableSource<O, O>
{

public AbstractGenericCapableJavaSource(JavaSource<?> enclosingType, Document document, CompilationUnit unit,
Expand All @@ -37,49 +40,62 @@ public AbstractGenericCapableJavaSource(JavaSource<?> enclosingType, Document do
}

@Override
public List<String> getGenericTypes()
public List<TypeVariableSource<O>> getTypeVariables()
{
List<String> result = new ArrayList<String>();
TypeDeclaration type = (TypeDeclaration) body;
List<TypeParameter> typeParameters = type.typeParameters();
if (typeParameters != null)
List<TypeVariableSource<O>> result = new ArrayList<TypeVariableSource<O>>();
for (TypeParameter typeParameter : typeParameters)
{
for (TypeParameter typeParameter : typeParameters)
result.add(new TypeVariableImpl<O>((O) this, typeParameter));
}
return Collections.unmodifiableList(result);
}

@Override
public TypeVariableSource<O> getTypeVariable(String name)
{
TypeDeclaration type = (TypeDeclaration) body;
List<TypeParameter> typeParameters = type.typeParameters();
for (TypeParameter typeParameter : typeParameters)
{
if (Strings.areEqual(name, typeParameter.getName().getIdentifier()))
{
result.add(typeParameter.getName().getIdentifier());
return new TypeVariableImpl<O>((O) this, typeParameter);
}
}
return Collections.unmodifiableList(result);
return null;
}

@Override
public O addGenericType(String genericType)
public TypeVariableSource<O> addTypeVariable()
{
TypeDeclaration type = (TypeDeclaration) body;
TypeParameter tp2 = unit.getAST().newTypeParameter();
tp2.setName(unit.getAST().newSimpleName(genericType));
type.typeParameters().add(tp2);
return (O) this;
return new TypeVariableImpl<O>((O) this, tp2);
}

@Override
public O removeGenericType(String genericType)
public O removeTypeVariable(String name)
{
TypeDeclaration type = (TypeDeclaration) body;
List<TypeParameter> typeParameters = type.typeParameters();
if (typeParameters != null)
for (Iterator<TypeParameter> iter = typeParameters.iterator(); iter.hasNext();)
{
Iterator<TypeParameter> it = typeParameters.iterator();
while (it.hasNext())
if (Strings.areEqual(name, iter.next().getName().getIdentifier()))
{
TypeParameter typeParameter = it.next();
if (typeParameter.getName().getIdentifier().equals(genericType))
{
it.remove();
}
iter.remove();
break;
}
}
return (O) this;
}

@Override
public O removeTypeVariable(TypeVariable<?> typeVariable)
{
return removeTypeVariable(typeVariable.getName());
}

}
Loading

0 comments on commit 978c071

Please sign in to comment.