use of javax.lang.model.element.Parameterizable in project immutables by immutables.
the class FactoryMethodAttributesCollector method collect.
void collect() {
ExecutableElement factoryMethodElement = (ExecutableElement) protoclass.sourceElement();
Parameterizable element = (Parameterizable) (factoryMethodElement.getKind() == ElementKind.CONSTRUCTOR ? factoryMethodElement.getEnclosingElement() : type.element);
for (VariableElement parameter : factoryMethodElement.getParameters()) {
TypeMirror returnType = parameter.asType();
ValueAttribute attribute = new ValueAttribute();
attribute.isGenerateAbstract = true;
attribute.reporter = reporter;
attribute.returnType = returnType;
attribute.element = parameter;
String parameterName = parameter.getSimpleName().toString();
attribute.names = styles.forAccessorWithRaw(parameterName, parameterName);
attribute.containingType = type;
attributes.add(attribute);
}
Instantiator encodingInstantiator = protoclass.encodingInstantiator();
@Nullable InstantiationCreator instantiationCreator = encodingInstantiator.creatorFor(element);
for (ValueAttribute attribute : attributes) {
attribute.initAndValidate(instantiationCreator);
}
if (instantiationCreator != null) {
type.additionalImports(instantiationCreator.imports);
}
type.attributes.addAll(attributes);
type.throwing = extractThrowsClause(factoryMethodElement);
}
use of javax.lang.model.element.Parameterizable in project immutables by immutables.
the class Generics method computeParameters.
private static Parameter[] computeParameters(final Protoclass protoclass, final Element element) {
if (!(element instanceof Parameterizable)) {
return NO_PARAMETERS;
}
final List<? extends TypeParameterElement> typeParameters = ((Parameterizable) element).getTypeParameters();
if (typeParameters.isEmpty()) {
return NO_PARAMETERS;
}
class Creator {
final String[] vars = collectVars(typeParameters);
final DeclaringType declaringType = protoclass.environment().round().inferDeclaringTypeFor(element);
Parameter[] create() {
final Parameter[] parameters = new Parameter[typeParameters.size()];
int i = 0;
for (TypeParameterElement e : typeParameters) {
parameters[i] = new Parameter(i, e.getSimpleName().toString(), boundsFrom(e));
i++;
}
return parameters;
}
String[] boundsFrom(TypeParameterElement e) {
List<? extends TypeMirror> boundMirrors = e.getBounds();
if (boundMirrors.isEmpty()) {
return NO_STRINGS;
}
String[] bounds = new String[boundMirrors.size()];
int c = 0;
for (TypeMirror m : boundMirrors) {
TypeStringProvider provider = newProvider(m);
provider.process();
bounds[c++] = provider.returnTypeName();
}
if (bounds.length == 1 && bounds[0].equals(Object.class.getName())) {
return NO_STRINGS;
}
return bounds;
}
TypeStringProvider newProvider(TypeMirror type) {
return new TypeStringProvider(protoclass.report(), element, type, new ImportsTypeStringResolver(declaringType, declaringType), vars, null);
}
}
return new Creator().create();
}
use of javax.lang.model.element.Parameterizable in project revapi by revapi.
the class FormalTypeParametersChanged method doVisit.
private void doVisit(@Nullable JavaModelElement oldElement, @Nullable JavaModelElement newElement) {
if (!isBothAccessible(oldElement, newElement)) {
return;
}
assert oldElement != null;
assert newElement != null;
Parameterizable oldEl = (Parameterizable) oldElement.getDeclaringElement();
Parameterizable newEl = (Parameterizable) newElement.getDeclaringElement();
List<? extends TypeParameterElement> oldPars = oldEl.getTypeParameters();
List<? extends TypeParameterElement> newPars = newEl.getTypeParameters();
if (oldPars.size() == 0 && oldPars.size() == newPars.size()) {
return;
}
List<TypeParameterElement> added = new ArrayList<>();
List<TypeParameterElement> removed = new ArrayList<>();
Map<TypeParameterElement, TypeParameterElement> changed = new LinkedHashMap<>();
Iterator<? extends TypeParameterElement> oldIt = oldPars.iterator();
Iterator<? extends TypeParameterElement> newIt = newPars.iterator();
while (oldIt.hasNext() && newIt.hasNext()) {
TypeParameterElement oldT = oldIt.next();
TypeParameterElement newT = newIt.next();
String oldS = Util.toUniqueString(oldT.asType());
String newS = Util.toUniqueString(newT.asType());
if (!oldS.equals(newS)) {
changed.put(oldT, newT);
}
}
while (oldIt.hasNext()) {
removed.add(oldIt.next());
}
while (newIt.hasNext()) {
added.add(newIt.next());
}
if (!added.isEmpty() || !removed.isEmpty() || !changed.isEmpty()) {
pushActive(oldElement, newElement, added, removed, changed);
}
}
use of javax.lang.model.element.Parameterizable in project revapi by revapi.
the class FormalTypeParametersChanged method doEnd.
@Nullable
@Override
protected List<Difference> doEnd() {
ActiveElements<JavaModelElement> els = popIfActive();
if (els == null) {
return null;
}
@SuppressWarnings("unchecked") List<TypeParameterElement> added = (List<TypeParameterElement>) els.context[0];
@SuppressWarnings("unchecked") List<TypeParameterElement> removed = (List<TypeParameterElement>) els.context[1];
@SuppressWarnings("unchecked") Map<TypeParameterElement, TypeParameterElement> changed = (Map<TypeParameterElement, TypeParameterElement>) els.context[2];
Parameterizable oldT = (Parameterizable) els.oldElement.getDeclaringElement();
List<Difference> diffs = new ArrayList<>();
if (oldT.getTypeParameters().isEmpty()) {
diffs.add(createDifference(Code.GENERICS_ELEMENT_NOW_PARAMETERIZED, Code.attachmentsFor(els.oldElement, els.newElement)));
}
for (TypeParameterElement e : added) {
diffs.add(createDifferenceWithExplicitParams(Code.GENERICS_FORMAL_TYPE_PARAMETER_ADDED, Code.attachmentsFor(els.oldElement, els.newElement), Util.toHumanReadableString(e)));
}
for (TypeParameterElement e : removed) {
diffs.add(createDifferenceWithExplicitParams(Code.GENERICS_FORMAL_TYPE_PARAMETER_REMOVED, Code.attachmentsFor(els.oldElement, els.newElement), Util.toHumanReadableString(e)));
}
for (Map.Entry<TypeParameterElement, TypeParameterElement> e : changed.entrySet()) {
String oldP = Util.toHumanReadableString(e.getKey());
String newP = Util.toHumanReadableString(e.getValue());
diffs.add(createDifferenceWithExplicitParams(Code.GENERICS_FORMAL_TYPE_PARAMETER_CHANGED, Code.attachmentsFor(els.oldElement, els.newElement, "oldTypeParameter", oldP, "newTypeParameter", newP), oldP, newP));
}
return diffs;
}
Aggregations