Java 类org.eclipse.xtext.common.types.JvmField 实例源码

项目:xtext-extras    文件:JvmModelGeneratorTest.java   
@Test
public void testBug426073() {
  try {
    final XExpression expression = this.expression("org.eclipse.emf.common.util.URI.createURI(\"dummy\")");
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      final Procedure1<JvmField> _function_1 = (JvmField it_1) -> {
        this.builder.setInitializer(it_1, expression);
      };
      JvmField _field = this.builder.toField(expression, "URI", this.typeRef(expression, URI.class), _function_1);
      this.builder.<JvmField>operator_add(_members, _field);
    };
    final JvmGenericType clazz = this.builder.toClass(expression, "my.test.Foo", _function);
    this.compile(expression.eResource(), clazz);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
项目:xtext-extras    文件:JvmModelGeneratorTest.java   
@Test
public void testCompilationStrategy() {
  try {
    final XExpression expression = this.expression("null", false);
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      JvmField _field = this.builder.toField(expression, "x", this.typeRef(expression, String.class));
      this.builder.<JvmField>operator_add(_members, _field);
      EList<JvmMember> _members_1 = it.getMembers();
      JvmOperation _getter = this.builder.toGetter(expression, "x", this.typeRef(expression, String.class));
      this.builder.<JvmOperation>operator_add(_members_1, _getter);
      EList<JvmMember> _members_2 = it.getMembers();
      JvmOperation _setter = this.builder.toSetter(expression, "x", this.typeRef(expression, String.class));
      this.builder.<JvmOperation>operator_add(_members_2, _setter);
    };
    final JvmGenericType clazz = this.builder.toClass(expression, "my.test.Foo", _function);
    final Class<?> compiled = this.compile(expression.eResource(), clazz);
    final Object inst = compiled.newInstance();
    final Method getter = compiled.getMethod("getX");
    final Method setter = compiled.getMethod("setX", String.class);
    setter.invoke(inst, "FOO");
    Assert.assertEquals("FOO", getter.invoke(inst));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
项目:xtext-extras    文件:LogicalContainerAwareReentrantTypeResolver.java   
protected void doPrepare(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmIdentifiableElement element, Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
    XExpression expression = getLogicalContainerProvider().getAssociatedExpression(element);
    if (expression != null) {
        if (!rootedInstances.contains(expression)) {
            throw new IllegalStateException("Expression not yet recorded: " + expression);
        }
    }
    if (element instanceof JvmDeclaredType) {
        _doPrepare(resolvedTypes, featureScopeSession, (JvmDeclaredType) element, resolvedTypesByContext);
    } else if (element instanceof JvmConstructor) {
        _doPrepare(resolvedTypes, featureScopeSession, (JvmConstructor) element, resolvedTypesByContext);
    } else if (element instanceof JvmField) {
        _doPrepare(resolvedTypes, featureScopeSession, (JvmField) element, resolvedTypesByContext);
    } else if (element instanceof JvmOperation) {
        _doPrepare(resolvedTypes, featureScopeSession, (JvmOperation) element, resolvedTypesByContext);
    }
}
项目:xtext-extras    文件:FeatureLinkingCandidate.java   
@Override
protected int getConformanceFlags(int idx, boolean recompute) {
    if (isStatic()) {
        if (idx == -1) {
            return ConformanceFlags.CHECKED_SUCCESS;
        }
    }
    if (idx == 0) {
        if (getReceiver() != null) {
            int result = getReceiverConformanceFlags();
            return result;
        }
    } else if (idx == 1) {
        if (getExpression() instanceof XAssignment && getFeature() instanceof JvmField) {
            return super.getConformanceFlags(0, recompute);
        }
    }
    return super.getConformanceFlags(idx, recompute);
}
项目:xtext-extras    文件:FeatureLinkingCandidate.java   
protected CandidateCompareResult compareByArgumentTypesAndStaticFlag(FeatureLinkingCandidate right, int leftBoxing, int rightBoxing) {
    if (isStatic() != right.isStatic()) {
        if (isSyntacticReceiverPossibleArgument() != right.isSyntacticReceiverPossibleArgument()) {
            if (isStatic() && !isSyntacticReceiverPossibleArgument())
                return CandidateCompareResult.THIS;
            if (right.isStatic() && !right.isSyntacticReceiverPossibleArgument()) {
                return CandidateCompareResult.OTHER;
            }
        } else if (getFeature() instanceof JvmField == right.getFeature() instanceof JvmField) {
            if (isStatic()) {
                if (getImplicitReceiver() != null && right.getImplicitReceiver() == null) {
                    return CandidateCompareResult.OTHER;
                }
            } else {
                if (getImplicitReceiver() != null && right.getImplicitReceiver() == null) {
                    return CandidateCompareResult.THIS;
                }
            }
        }
    }
    return compareByBoxing(leftBoxing, rightBoxing);
}
项目:xtext-extras    文件:ResolvedTypes.java   
protected JvmTypeReference getDeclaredType(JvmIdentifiableElement identifiable) {
    if (identifiable instanceof JvmOperation) {
        return ((JvmOperation) identifiable).getReturnType();
    }
    if (identifiable instanceof JvmField) {
        return ((JvmField) identifiable).getType();
    }
    if (identifiable instanceof JvmConstructor) {
        return shared.resolver.getServices().getTypeReferences().createTypeRef(((JvmConstructor) identifiable).getDeclaringType());
    }
    if (identifiable instanceof JvmFormalParameter) {
        JvmTypeReference parameterType = ((JvmFormalParameter) identifiable).getParameterType();
        return parameterType;
    }
    return null;
}
项目:xtext-extras    文件:FeatureKinds.java   
public static String getTypeName(JvmIdentifiableElement feature) {
    if (feature instanceof JvmFormalParameter) {
        return "parameter";
    }
    if (feature instanceof XVariableDeclaration) {
        return "local variable";
    }
    if (feature instanceof JvmEnumerationLiteral) {
        return "enum literal";
    }
    if (feature instanceof JvmField) {
        return "field";
    }
    if (feature instanceof JvmOperation) {
        return "method";
    }
    if (feature instanceof JvmConstructor) {
        return "constructor";
    }
    if (feature instanceof JvmType) {
        return "type";
    }
    throw new IllegalStateException();
}
项目:xtext-extras    文件:RawResolvedFeatures.java   
protected void computeAllFeatures(
        JvmDeclaredType type,
        Multimap<String, AbstractResolvedOperation> processedOperations,
        Set<String> processedFields,
        ListMultimap<String, JvmFeature> result,
        Set<String> seenNames) {
    Iterable<JvmFeature> features = type.getAllFeatures();
    for(JvmFeature feature: features) {
        if (!seenNames.contains(feature.getSimpleName())) {
            if (feature instanceof JvmOperation) {
                JvmOperation operation = (JvmOperation) feature;
                String simpleName = operation.getSimpleName();
                if (processedOperations.containsKey(simpleName)) {
                    if (isOverridden(operation, processedOperations.get(simpleName))) {
                        continue;
                    }
                }
                BottomResolvedOperation resolvedOperation = createResolvedOperation(operation);
                processedOperations.put(simpleName, resolvedOperation);
                result.put(simpleName, operation);  
            } else if (feature instanceof JvmField && processedFields.add(feature.getSimpleName())) {
                result.put(feature.getSimpleName(), feature);
            }
        }
    }
}
项目:xtext-extras    文件:RawResolvedFeatures.java   
protected void computeAllFeatures(
        JvmDeclaredType type,
        String name,
        Multimap<String, AbstractResolvedOperation> processedOperations,
        Set<String> processedFields,
        List<JvmFeature> result) {
    Iterable<JvmFeature> features = type.findAllFeaturesByName(name);
    for(JvmFeature feature: features) {
        if (feature instanceof JvmOperation) {
            JvmOperation operation = (JvmOperation) feature;
            String simpleName = operation.getSimpleName();
            if (processedOperations.containsKey(simpleName)) {
                if (isOverridden(operation, processedOperations.get(simpleName))) {
                    continue;
                }
            }
            BottomResolvedOperation resolvedOperation = createResolvedOperation(operation);
            processedOperations.put(simpleName, resolvedOperation);
            result.add(operation);  
        } else if (feature instanceof JvmField && processedFields.add(feature.getSimpleName())) {
            result.add(feature);
        }
    }
}
项目:xtext-extras    文件:SerializerScopeProvider.java   
public IScope createFeatureCallSerializationScope(EObject context) {
    if (!(context instanceof XAbstractFeatureCall)) {
        return IScope.NULLSCOPE;
    }
    XAbstractFeatureCall call = (XAbstractFeatureCall) context;
    JvmIdentifiableElement feature = call.getFeature();
    // this and super - logical container aware FeatureScopes
    if (feature instanceof JvmType) {
        return getTypeScope(call, (JvmType) feature);
    }
    if (feature instanceof JvmConstructor) {
        return getThisOrSuperScope(call, (JvmConstructor) feature);
    }
    if (feature instanceof JvmExecutable) {
        return getExecutableScope(call, feature);
    }
    if (feature instanceof JvmFormalParameter || feature instanceof JvmField || feature instanceof XVariableDeclaration || feature instanceof XSwitchExpression) {
        return new SingletonScope(EObjectDescription.create(feature.getSimpleName(), feature), IScope.NULLSCOPE);
    }
    return IScope.NULLSCOPE;
}
项目:xtext-extras    文件:XbaseInterpreter.java   
protected Object _assignValueTo(JvmField jvmField, XAbstractFeatureCall assignment, Object value,
        IEvaluationContext context, CancelIndicator indicator) {
    Object receiver = getReceiver(assignment, context, indicator);
    Field field = javaReflectAccess.getField(jvmField);
    try {
        if (field == null) {
            throw new NoSuchFieldException("Could not find field " + jvmField.getIdentifier());
        }
        if (!Modifier.isStatic(field.getModifiers()) && receiver == null)
            throw new EvaluationException(new NullPointerException("Cannot assign value to field: "
                    + jvmField.getIdentifier() + " on null instance"));
        JvmTypeReference type = jvmField.getType();
        Object coerced = coerceArgumentType(value, type);
        field.setAccessible(true);
        field.set(receiver, coerced);
        return value;
    } catch (Exception e) {
        throw new IllegalStateException("Could not access field: " + jvmField.getIdentifier()
                + " on instance: " + receiver, e);
    }
}
项目:xtext-extras    文件:TypeUsageCollector.java   
protected void collectStaticImportsFrom(XExpression expression, JvmIdentifiableElement feature) {
    if (expression instanceof XAbstractFeatureCall) {
        if (feature instanceof JvmEnumerationLiteral && expression instanceof XFeatureCall) {
            if (isEnumLiteralImplicitelyImported(expression, (JvmEnumerationLiteral) feature)) {
                return;
            }
        }
        XAbstractFeatureCall featureCall = (XAbstractFeatureCall) expression;
        if ((feature instanceof JvmOperation || feature instanceof JvmField) && featureCall.isStatic()) {
            if (featureCall.isExtension()) {
                acceptStaticExtensionImport((JvmMember) feature);
            } else {
                acceptStaticImport((JvmMember) feature);
            }
        }
    }
}
项目:xtext-extras    文件:ReferencedInvalidTypeFinder.java   
protected LightweightTypeReference internalFindReferencedInvalidType(final JvmIdentifiableElement operation) {
  if (operation instanceof JvmOperation) {
    return _internalFindReferencedInvalidType((JvmOperation)operation);
  } else if (operation instanceof JvmExecutable) {
    return _internalFindReferencedInvalidType((JvmExecutable)operation);
  } else if (operation instanceof JvmField) {
    return _internalFindReferencedInvalidType((JvmField)operation);
  } else if (operation != null) {
    return _internalFindReferencedInvalidType(operation);
  } else if (operation == null) {
    return _internalFindReferencedInvalidType((Void)null);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(operation).toString());
  }
}
项目:xtext-extras    文件:JvmModelGenerator.java   
protected ITreeAppendable _generateModifier(final JvmField it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    this.generateVisibilityModifier(it, appendable);
    boolean _isFinal = it.isFinal();
    if (_isFinal) {
      appendable.append("final ");
    }
    boolean _isStatic = it.isStatic();
    if (_isStatic) {
      appendable.append("static ");
    }
    boolean _isTransient = it.isTransient();
    if (_isTransient) {
      appendable.append("transient ");
    }
    ITreeAppendable _xifexpression = null;
    boolean _isVolatile = it.isVolatile();
    if (_isVolatile) {
      _xifexpression = appendable.append("volatile ");
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
项目:xtext-extras    文件:JvmModelGenerator.java   
protected ITreeAppendable _generateMember(final JvmField it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable tracedAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
    this.generateModifier(it, tracedAppendable, config);
    this._errorSafeExtensions.serializeSafely(it.getType(), "Object", tracedAppendable);
    tracedAppendable.append(" ");
    final String name = tracedAppendable.declareVariable(it, this.makeJavaIdentifier(it.getSimpleName()));
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(name);
    this.generateInitialization(it, tracedAppendable, config);
    _xblockexpression = tracedAppendable.append(";");
  }
  return _xblockexpression;
}
项目:xtext-extras    文件:JvmModelGenerator.java   
public ITreeAppendable generateModifier(final JvmMember it, final ITreeAppendable appendable, final GeneratorConfig config) {
  if (it instanceof JvmConstructor) {
    return _generateModifier((JvmConstructor)it, appendable, config);
  } else if (it instanceof JvmOperation) {
    return _generateModifier((JvmOperation)it, appendable, config);
  } else if (it instanceof JvmField) {
    return _generateModifier((JvmField)it, appendable, config);
  } else if (it instanceof JvmGenericType) {
    return _generateModifier((JvmGenericType)it, appendable, config);
  } else if (it instanceof JvmDeclaredType) {
    return _generateModifier((JvmDeclaredType)it, appendable, config);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, appendable, config).toString());
  }
}
项目:xtext-extras    文件:JavaReflectAccessTest.java   
@Test public void testGetField_1() throws Exception {
    JvmDeclaredType type = getType(Y.class);
    JvmMember addMethod = Iterables.find(type.getMembers(), new Predicate<JvmMember>() {
        @Override
        public boolean apply(JvmMember input) {
            if (input instanceof JvmField) {
                return input.getSimpleName().equals("z");
            }
            return false;
        }
    });

    Field field = Y.class.getDeclaredField("z");

    assertEquals(field, getJavaReflectAccess().getField((JvmField) addMethod));
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
protected JvmField getFieldFromType(EObject context, Class<?> type, String field) {
    String fieldName = type.getName() + "." + field;
    assertNotNull(context);
    JvmField result = (JvmField) context.eResource().getEObject(fieldName);
    assertNotNull(fieldName, result);
    return result;
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testFields_privateT_01() {
    String typeName = Fields.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, Fields.class, "privateT");
    assertSame(type, field.getDeclaringType());
    assertEquals(JvmVisibility.PRIVATE, field.getVisibility());
    JvmType fieldType = field.getType().getType();
    assertSame(type.getTypeParameters().get(0), fieldType);
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testFields_defaultListT_01() {
    String typeName = Fields.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, Fields.class, "defaultListT");
    assertSame(type, field.getDeclaringType());
    assertEquals(JvmVisibility.DEFAULT, field.getVisibility());
    JvmTypeReference fieldType = field.getType();
    assertEquals("java.util.List<T>", fieldType.getIdentifier());
    assertTrue(fieldType instanceof JvmParameterizedTypeReference);
    JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
    JvmTypeReference refTypeArg = parameterizedFieldType.getArguments().get(0);
    assertSame(type.getTypeParameters().get(0), refTypeArg.getType());
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testFields_protectedString_01() {
    String typeName = Fields.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, Fields.class, "protectedString");
    assertSame(type, field.getDeclaringType());
    assertEquals(JvmVisibility.PROTECTED, field.getVisibility());
    JvmType fieldType = field.getType().getType();
    assertEquals("java.lang.String", fieldType.getIdentifier());
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testFields_protectedStaticString_01() {
    String typeName = Fields.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, Fields.class, "protectedStaticString");
    assertSame(type, field.getDeclaringType());
    assertTrue(field.isStatic());
    assertEquals(JvmVisibility.PROTECTED, field.getVisibility());
    JvmType fieldType = field.getType().getType();
    assertEquals("java.lang.String", fieldType.getIdentifier());
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testFields_volatileInt_01() {
    String typeName = Fields.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, Fields.class, "volatileInt");
    assertSame(type, field.getDeclaringType());
    assertTrue(field.isVolatile());
    assertFalse(field.isTransient());
    assertEquals(JvmVisibility.DEFAULT, field.getVisibility());
    JvmType fieldType = field.getType().getType();
    assertEquals("int", fieldType.getIdentifier());
}
项目:xtext-extras    文件:JvmModelBasedLinkingTest.java   
@Test
public void testLinkToParameter_1() {
  try {
    XExpression _expression = this.expression("x", false);
    final XFeatureCall expr = ((XFeatureCall) _expression);
    final Resource resource = expr.eResource();
    resource.eSetDeliver(false);
    EList<EObject> _contents = resource.getContents();
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      JvmField _field = this._jvmTypesBuilder.toField(expr, "x", this.stringType(expr));
      this._jvmTypesBuilder.<JvmField>operator_add(_members, _field);
      EList<JvmMember> _members_1 = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        EList<JvmFormalParameter> _parameters = it_1.getParameters();
        JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(expr, "x", this.stringType(expr));
        this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
        this._jvmTypesBuilder.setBody(it_1, expr);
      };
      JvmOperation _method = this._jvmTypesBuilder.toMethod(expr, "doStuff", this.stringType(expr), _function_1);
      this._jvmTypesBuilder.<JvmOperation>operator_add(_members_1, _method);
    };
    JvmGenericType _class = this._jvmTypesBuilder.toClass(expr, "Foo", _function);
    this._jvmTypesBuilder.<JvmGenericType>operator_add(_contents, _class);
    this._validationTestHelper.assertNoErrors(expr);
    JvmIdentifiableElement _feature = expr.getFeature();
    Assert.assertTrue((_feature instanceof JvmFormalParameter));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
项目:xtext-extras    文件:JvmModelBasedLinkingTest.java   
@Test
public void testLinkToField_1() {
  try {
    XExpression _expression = this.expression("x", false);
    final XFeatureCall expr = ((XFeatureCall) _expression);
    final Resource resource = expr.eResource();
    resource.eSetDeliver(false);
    EList<EObject> _contents = resource.getContents();
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      JvmField _field = this._jvmTypesBuilder.toField(expr, "x", this.stringType(expr));
      this._jvmTypesBuilder.<JvmField>operator_add(_members, _field);
      EList<JvmMember> _members_1 = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        this._jvmTypesBuilder.setBody(it_1, expr);
      };
      JvmOperation _method = this._jvmTypesBuilder.toMethod(expr, "getX", this.stringType(expr), _function_1);
      this._jvmTypesBuilder.<JvmOperation>operator_add(_members_1, _method);
    };
    JvmGenericType _class = this._jvmTypesBuilder.toClass(expr, "Foo", _function);
    this._jvmTypesBuilder.<JvmGenericType>operator_add(_contents, _class);
    this._validationTestHelper.assertNoErrors(expr);
    JvmIdentifiableElement _feature = expr.getFeature();
    Assert.assertTrue((_feature instanceof JvmField));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testFields_innerFields_01() {
    String typeName = Fields.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, Fields.Inner.class, "innerFields");
    JvmGenericType innerType = (JvmGenericType) getTypeProvider().findTypeByName(Fields.Inner.class.getName());
    assertSame(innerType, field.getDeclaringType());
    assertSame(type, innerType.getDeclaringType());
    assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
    JvmTypeReference fieldType = field.getType();
    assertEquals(typeName + "<java.lang.String>", fieldType.getIdentifier());
    assertTrue(field.getType() instanceof JvmParameterizedTypeReference);
    JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
    assertSame(type, parameterizedFieldType.getType());
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
@Test
public void testAnnotatedField_01() throws Exception {
    String typeName = TestAnnotation.Annotated.class.getName();
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, TestAnnotation.Annotated.class, "field");
    assertNotNull(field);
    JvmStringAnnotationValue value = (JvmStringAnnotationValue) getDefaultOrExplicitAnnotationValue("value", field);
    assertEquals(1, value.getValues().size());
    String s = value.getValues().get(0);
    assertEquals("MyString", s);
}
项目:xtext-extras    文件:AbstractTypeProviderTest.java   
protected JvmField doTestConstantValue(String fieldName, Object fieldValue) {
    String typeName = "org.eclipse.xtext.common.types.testSetups.TestConstants";
    JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
    JvmField field = getFieldFromType(type, TestConstants.class, fieldName);
    assertTrue(field.isSetConstant());
    assertTrue(field.isConstant());
    assertEquals(fieldValue, field.getConstantValue());
    return field;
}
项目:xtext-extras    文件:JvmTypesBuilderTest.java   
@Test
public void testToFieldWithKeywordCollision() {
  try {
    final XExpression e = this.expression("\'\'");
    final JvmField field = this._jvmTypesBuilder.toField(e, "package", this._jvmTypeReferenceBuilder.typeRef(String.class));
    Assert.assertEquals("package", field.getSimpleName());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
项目:xtext-extras    文件:XbaseValidator.java   
@Check
public void checkNoForwardReferences(XExpression fieldInitializer) {
    JvmIdentifiableElement container = logicalContainerProvider.getLogicalContainer(fieldInitializer);
    if (container instanceof JvmField) {
        JvmField field = (JvmField) container;
        boolean staticField = field.isStatic();
        JvmDeclaredType declaredType = field.getDeclaringType();
        if (declaredType == null) {
            return;
        }
        Collection<JvmField> illegalFields = Sets.newHashSet();
        for(int i = declaredType.getMembers().size() - 1; i>=0; i--) {
            JvmMember member = declaredType.getMembers().get(i);
            if (member instanceof JvmField) {
                if (((JvmField) member).isStatic() == staticField) {
                    illegalFields.add((JvmField) member);
                }
            }
            if (member == field)
                break;
        }
        TreeIterator<EObject> iterator = EcoreUtil2.eAll(fieldInitializer);
        while(iterator.hasNext()) {
            EObject object = iterator.next();
            if (object instanceof XFeatureCall) {
                JvmIdentifiableElement feature = ((XFeatureCall) object).getFeature();
                if (illegalFields.contains(((XFeatureCall) object).getFeature())) {
                    error("Cannot reference the field '" + feature.getSimpleName() + "' before it is defined", 
                            object, null, INSIGNIFICANT_INDEX, ILLEGAL_FORWARD_REFERENCE);
                }
            } else if (isLocalClassSemantics(object)) {
                iterator.prune();
            }
        }
    }
}
项目:xtext-extras    文件:XbaseValidator.java   
protected void checkAssignment(XExpression expression, EStructuralFeature feature, boolean simpleAssignment) {
    if (!(expression instanceof XAbstractFeatureCall)) {
        error("The left-hand side of an assignment must be a variable", expression, null,
                ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ASSIGNMENT_TO_NO_VARIABLE);
        return;
    }
    XAbstractFeatureCall assignment = (XAbstractFeatureCall) expression;
    JvmIdentifiableElement assignmentFeature = assignment.getFeature();
    if (assignmentFeature instanceof XVariableDeclaration) {
        XVariableDeclaration variableDeclaration = (XVariableDeclaration) assignmentFeature;
        if (variableDeclaration.isWriteable()) {
            return;
        }
        error("Assignment to final variable", expression, feature,
            ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ASSIGNMENT_TO_FINAL);
    } else if (assignmentFeature instanceof JvmFormalParameter) {
        error("Assignment to final parameter", expression, feature,
                ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ASSIGNMENT_TO_FINAL);
    } else if (assignmentFeature instanceof JvmField) {
        JvmField field = (JvmField) assignmentFeature;
        if (!field.isFinal()) {
            return;
        }
        if (simpleAssignment) {
            JvmIdentifiableElement container = logicalContainerProvider.getNearestLogicalContainer(assignment);

            // don't issue an error if it's an assignment of a local final field within a constructor.
            if (container != null && container instanceof JvmConstructor) {
                JvmConstructor constructor = (JvmConstructor) container;
                if (field.getDeclaringType() == constructor.getDeclaringType())
                    return;
            }
        }
        error("Assignment to final field", expression, feature,
                ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ASSIGNMENT_TO_FINAL);
    } else if (!simpleAssignment) {
        error("The left-hand side of an assignment must be a variable", expression, null,
                ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ASSIGNMENT_TO_NO_VARIABLE);
    }
}
项目:xtext-extras    文件:LogicalContainerAwareReentrantTypeResolver.java   
protected void computeTypes(Map<JvmIdentifiableElement, ResolvedTypes> preparedResolvedTypes, ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, EObject element) {
    if (element instanceof JvmDeclaredType) {
        _computeTypes(preparedResolvedTypes, resolvedTypes, featureScopeSession, (JvmDeclaredType) element);
    } else if (element instanceof JvmConstructor) {
        _computeTypes(preparedResolvedTypes, resolvedTypes, featureScopeSession, (JvmConstructor) element);
    } else if (element instanceof JvmField) {
        _computeTypes(preparedResolvedTypes, resolvedTypes, featureScopeSession, (JvmField) element);
    } else if (element instanceof JvmOperation) {
        _computeTypes(preparedResolvedTypes, resolvedTypes, featureScopeSession, (JvmOperation) element);
    } else {
        computeTypes(resolvedTypes, featureScopeSession, element);
    }
}
项目:xtext-extras    文件:LogicalContainerAwareReentrantTypeResolver.java   
@Override
protected void computeTypes(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, EObject element) {
    if (element instanceof JvmConstructor) {
        throw new IllegalStateException();
    } else if (element instanceof JvmField) {
        throw new IllegalStateException();
    } else if (element instanceof JvmOperation) {
        throw new IllegalStateException();
    } else if (element instanceof JvmDeclaredType) {
        throw new IllegalStateException();
    } else {
        super.computeTypes(resolvedTypes, featureScopeSession, element);
    }
}
项目:xtext-extras    文件:JvmDeclaredTypeImplCustom.java   
protected void processMembers(Map<String, Set<JvmFeature>> result, Collection<? extends JvmMember> members) {
    for (JvmMember member : members) {
        if (member instanceof JvmOperation || member instanceof JvmField) {
            Set<JvmFeature> knownMembers = result.get(member.getSimpleName());
            if (knownMembers == null) {
                // Sets.newLinkedHashSet(capacity) does not exist
                knownMembers = new LinkedHashSet<JvmFeature>(2);
                result.put(member.getSimpleName(), knownMembers);
            }
            knownMembers.add((JvmFeature) member);
        } 
    }
}
项目:xtext-extras    文件:AbstractLinkingCandidate.java   
protected boolean mustDiscardRefinement() {
    Expression expression = getExpression();
    if (expression instanceof XAssignment) {
        JvmIdentifiableElement feature = getFeature();
        if (feature instanceof XVariableDeclaration) {
            return ((XVariableDeclaration) feature).isWriteable();
        }
        if (feature instanceof JvmField) {
            return !((JvmField) feature).isFinal();
        }
    }
    return false;
}
项目:xtext-extras    文件:JavaReflectAccess.java   
/**
 * @return the {@link java.lang.reflect.Field} corresponding to the given {@link JvmField} or <code>null</code>.
 */
public Field getField(JvmField field) {
    Class<?> class1 = getRawType(field.getDeclaringType());
    if (class1 == null)
        return null;
    try {
        return class1.getDeclaredField(field.getSimpleName());
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug(e.getMessage(), e);
    }
    return null;
}
项目:xtext-extras    文件:JvmTypesBuilder.java   
/**
 * Same as {@link #toField(EObject, String, JvmTypeReference)} but with an initializer passed as the last argument.
 */
/* @Nullable */ 
public JvmField toField(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ JvmTypeReference typeRef, 
        /* @Nullable */ Procedure1<? super JvmField> initializer) {
    if(sourceElement == null || name == null) 
        return null;
    JvmField result = typesFactory.createJvmField();
    result.setSimpleName(name);
    result.setVisibility(JvmVisibility.PRIVATE);
    result.setType(cloneWithProxies(typeRef));
    associate(sourceElement, result);
    return initializeSafely(result, initializer);
}
项目:xtext-extras    文件:JvmTypesBuilder.java   
/**
 * @deprecated build your own :-)
 */
@Deprecated
public /* @Nullable */ JvmOperation toHashCodeMethod(/* @Nullable */ final EObject sourceElement, final boolean extendsSomethingWithProperHashCode, /* @Nullable */ final JvmDeclaredType declaredType) {
    if (sourceElement == null || declaredType == null)
        return null;
    return toHashCodeMethod(sourceElement, extendsSomethingWithProperHashCode, toArray(filter(declaredType.getMembers(), JvmField.class), JvmField.class));
}
项目:xtext-extras    文件:JvmTypesBuilder.java   
/**
 * @deprecated build your own :-)
 */
@Deprecated
public /* @Nullable */ JvmOperation toEqualsMethod(/* @Nullable */ final EObject sourceElement, /* @Nullable */ final JvmDeclaredType declaredType, final boolean isDelegateToSuperEquals) {
    if (sourceElement == null || declaredType == null)
        return null;
    return toEqualsMethod(sourceElement, declaredType, isDelegateToSuperEquals, toArray(filter(declaredType.getMembers(), JvmField.class), JvmField.class));
}
项目:xtext-extras    文件:JvmModelBasedLinkingTest.java   
@Test
public void testLinkToField() {
  try {
    XExpression _expression = this.expression("x", false);
    final XFeatureCall expr = ((XFeatureCall) _expression);
    final Resource resource = expr.eResource();
    resource.eSetDeliver(false);
    EList<EObject> _contents = resource.getContents();
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
      EList<JvmMember> _members = it.getMembers();
      JvmField _field = this._jvmTypesBuilder.toField(expr, "x", this.stringType(expr));
      this._jvmTypesBuilder.<JvmField>operator_add(_members, _field);
      EList<JvmMember> _members_1 = it.getMembers();
      final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> {
        EList<JvmFormalParameter> _parameters = it_1.getParameters();
        JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(expr, "y", this.stringType(expr));
        this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
        this._jvmTypesBuilder.setBody(it_1, expr);
      };
      JvmOperation _method = this._jvmTypesBuilder.toMethod(expr, "doStuff", this.stringType(expr), _function_1);
      this._jvmTypesBuilder.<JvmOperation>operator_add(_members_1, _method);
    };
    JvmGenericType _class = this._jvmTypesBuilder.toClass(expr, "Foo", _function);
    this._jvmTypesBuilder.<JvmGenericType>operator_add(_contents, _class);
    this._validationTestHelper.assertNoErrors(expr);
    JvmIdentifiableElement _feature = expr.getFeature();
    Assert.assertTrue((_feature instanceof JvmField));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}