/**
  * Determine whether the given field's type is changed when type parameters from the defining
  * type's declaration are replaced with the actual type arguments from the defining type.
  *
  * @param baseField the base field
  * @param definingType the type defining the parameters and arguments to be used in the
  *     substitution
  * @return true if the type is changed by type substitution.
  */
 private static boolean isChangedByTypeSubstitution(
     FieldElement baseField, InterfaceType definingType) {
   Type[] argumentTypes = definingType.getTypeArguments();
   if (baseField != null && argumentTypes.length != 0) {
     Type baseType = baseField.getType();
     Type[] parameterTypes = definingType.getElement().getType().getTypeArguments();
     if (baseType != null) {
       Type substitutedType = baseType.substitute(argumentTypes, parameterTypes);
       if (!baseType.equals(substitutedType)) {
         return true;
       }
     }
     // If the field has a propagated type, then we need to check whether the propagated type
     // needs substitution.
     Type basePropagatedType = baseField.getPropagatedType();
     if (basePropagatedType != null) {
       Type substitutedPropagatedType =
           basePropagatedType.substitute(argumentTypes, parameterTypes);
       if (!basePropagatedType.equals(substitutedPropagatedType)) {
         return true;
       }
     }
   }
   return false;
 }
Exemple #2
0
  public void test_visitMethodDeclaration_setter() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String methodName = "m";
    MethodDeclaration methodDeclaration =
        methodDeclaration(
            null,
            null,
            Keyword.SET,
            null,
            identifier(methodName),
            formalParameterList(),
            blockFunctionBody());
    methodDeclaration.accept(builder);
    FieldElement[] fields = holder.getFields();
    assertLength(1, fields);

    FieldElement field = fields[0];
    assertNotNull(field);
    assertEquals(methodName, field.getName());
    assertTrue(field.isSynthetic());
    assertNull(field.getGetter());
    PropertyAccessorElement setter = field.getSetter();
    assertNotNull(setter);
    assertTrue(setter.isSetter());
    assertFalse(setter.isSynthetic());
    assertEquals(methodName, setter.getName());
    assertEquals(field, setter.getField());
    assertLength(0, setter.getFunctions());
    assertLength(0, setter.getLabels());
    assertLength(0, setter.getLocalVariables());
    assertLength(0, setter.getParameters());
  }
 public FieldElement getField(String fieldName) {
   if (fields == null) {
     return null;
   }
   for (FieldElement field : fields) {
     if (field.getName().equals(fieldName)) {
       return field;
     }
   }
   return null;
 }
Exemple #4
0
  public void test_visitClassDeclaration_withMembers() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String className = "C";
    String typeVariableName = "E";
    String fieldName = "f";
    String methodName = "m";
    ClassDeclaration classDeclaration =
        classDeclaration(
            null,
            className,
            typeParameterList(typeVariableName),
            null,
            null,
            null,
            fieldDeclaration(false, null, variableDeclaration(fieldName)),
            methodDeclaration(
                null,
                null,
                null,
                null,
                identifier(methodName),
                formalParameterList(),
                blockFunctionBody()));
    classDeclaration.accept(builder);
    ClassElement[] types = holder.getTypes();
    assertLength(1, types);

    ClassElement type = types[0];
    assertNotNull(type);
    assertEquals(className, type.getName());
    assertFalse(type.isAbstract());
    assertFalse(type.isSynthetic());

    TypeVariableElement[] typeVariables = type.getTypeVariables();
    assertLength(1, typeVariables);
    TypeVariableElement typeVariable = typeVariables[0];
    assertNotNull(typeVariable);
    assertEquals(typeVariableName, typeVariable.getName());

    FieldElement[] fields = type.getFields();
    assertLength(1, fields);
    FieldElement field = fields[0];
    assertNotNull(field);
    assertEquals(fieldName, field.getName());

    MethodElement[] methods = type.getMethods();
    assertLength(1, methods);
    MethodElement method = methods[0];
    assertNotNull(method);
    assertEquals(methodName, method.getName());
  }
 public void test_metadata_field() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "class C {", "  @A int f;", "}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   ClassElement[] classes = unit.getTypes();
   assertLength(1, classes);
   FieldElement field = classes[0].getFields()[0];
   ElementAnnotation[] annotations = field.getMetadata();
   assertLength(1, annotations);
   assertNoErrors(source);
   verify(source);
 }
Exemple #6
0
  public void test_visitFieldDeclaration() {
    ElementHolder holder = new ElementHolder();
    ElementBuilder builder = new ElementBuilder(holder);
    String firstFieldName = "x";
    String secondFieldName = "y";
    FieldDeclaration fieldDeclaration =
        fieldDeclaration(
            false, null, variableDeclaration(firstFieldName), variableDeclaration(secondFieldName));
    fieldDeclaration.accept(builder);
    FieldElement[] fields = holder.getFields();
    assertLength(2, fields);

    FieldElement firstField = fields[0];
    assertNotNull(firstField);
    assertEquals(firstFieldName, firstField.getName());
    assertNull(firstField.getInitializer());
    assertFalse(firstField.isConst());
    assertFalse(firstField.isFinal());
    assertFalse(firstField.isSynthetic());

    FieldElement secondField = fields[1];
    assertNotNull(secondField);
    assertEquals(secondFieldName, secondField.getName());
    assertNull(secondField.getInitializer());
    assertFalse(secondField.isConst());
    assertFalse(secondField.isFinal());
    assertFalse(secondField.isSynthetic());
  }