@Override public Iterable<IEObjectDescription> getElements(EObject object) { if (object instanceof JvmConstructor) { JvmConstructor constructor = ((JvmConstructor) object); String qualifiedNameWithDots = constructor.getQualifiedName('.'); String qualifiedNameWithDollar = constructor.getQualifiedName(); if (qualifiedNameWithDollar.equals(qualifiedNameWithDots)) { final Set<IEObjectDescription> result = singleton( EObjectDescription.create(getQualifiedNameConverter().toQualifiedName(qualifiedNameWithDots), object)); return result; } else { return Arrays.asList( EObjectDescription.create(getQualifiedNameConverter().toQualifiedName(qualifiedNameWithDots), object), EObjectDescription.create(getQualifiedNameConverter().toQualifiedName(qualifiedNameWithDollar), object)); } } return emptySet(); }
@Test public void test_staticNestedTypes_constructor() { String typeName = Bug347739.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); List<JvmMember> members = type.getMembers(); for (JvmMember member : members) { if (member instanceof JvmGenericType) { if ("StackItem".equals(member.getSimpleName())) { JvmGenericType stackItem = (JvmGenericType) member; Iterable<JvmConstructor> constructors = stackItem.getDeclaredConstructors(); for (JvmConstructor constructor : constructors) { assertEquals(2, constructor.getParameters().size()); } return; } } } fail("could not find inner class"); }
protected void doTestInnerType_WrappedIterator_02(JvmGenericType wrappedIterator) { assertEquals(3, Iterables.size(wrappedIterator.getDeclaredConstructors())); JvmConstructor constructor = (JvmConstructor) Iterables.find(wrappedIterator.getMembers(), new Predicate<JvmMember>() { @Override public boolean apply(JvmMember input) { return (input instanceof JvmConstructor) && input.getSimpleName().equals("WrappedIterator") && ((JvmConstructor) input).getParameters().size() == 3; } }); assertNotNull(constructor); JvmFormalParameter firstParameter = constructor.getParameters().get(0); assertEquals(1, firstParameter.getAnnotations().size()); assertEquals("java.lang.String", firstParameter.getParameterType().getIdentifier()); assertEquals(TestAnnotationWithDefaults.class.getName(), firstParameter.getAnnotations().get(0).getAnnotation().getQualifiedName()); JvmFormalParameter secondParameter = constructor.getParameters().get(1); assertEquals(0, secondParameter.getAnnotations().size()); assertEquals("int", secondParameter.getParameterType().getIdentifier()); JvmFormalParameter thirdParameter = constructor.getParameters().get(2); assertEquals(1, thirdParameter.getAnnotations().size()); assertEquals("java.util.Iterator<V>", thirdParameter.getParameterType().getIdentifier()); assertEquals(TestAnnotation.NestedAnnotation.class.getName(), thirdParameter.getAnnotations().get(0).getAnnotation().getQualifiedName()); }
@Test public void testEnum_04() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); List<JvmMember> members = type.getMembers(); boolean constructorFound = false; for (JvmMember member : members) { if (member instanceof JvmConstructor) { assertFalse(constructorFound); constructorFound = true; List<JvmFormalParameter> parameters = ((JvmConstructor) member).getParameters(); assertEquals(1, parameters.size()); // synthetic parameters are not returned } } assertTrue(constructorFound); }
@Test public void testNestedEnum_04() throws Exception { String typeName = TestEnum.Nested.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); List<JvmMember> members = type.getMembers(); boolean constructorFound = false; for (JvmMember member : members) { if (member instanceof JvmConstructor) { assertFalse(constructorFound); constructorFound = true; List<JvmFormalParameter> parameters = ((JvmConstructor) member).getParameters(); assertEquals(0, parameters.size()); // synthetic parameters are not returned } } assertTrue(constructorFound); }
public JvmAnnotationValue getConstructorParameterAnnotationValue(String name, boolean defaultValue) { String typeName = TestAnnotation.Annotated.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated(java.lang.String,java.lang.String,java.lang.String)"); JvmAnnotationTarget target = constructor.getParameters().get(0); JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target); if (defaultValue) { if (isDefaultValueSupported()) { assertTrue(result.eContainer() instanceof JvmOperation); } else { assertFalse(result.eContainer() instanceof JvmOperation); } } else { assertFalse(result.eContainer() instanceof JvmOperation); } return result; }
@Check public void checkDelegateConstructorIsFirst(XFeatureCall featureCall) { JvmIdentifiableElement feature = featureCall.getFeature(); if (feature != null && !feature.eIsProxy() && feature instanceof JvmConstructor) { JvmIdentifiableElement container = logicalContainerProvider.getNearestLogicalContainer(featureCall); if (container != null) { if (container instanceof JvmConstructor) { XExpression body = logicalContainerProvider.getAssociatedExpression(container); if (body == featureCall) return; if (body instanceof XBlockExpression) { List<XExpression> expressions = ((XBlockExpression) body).getExpressions(); if (expressions.isEmpty() || expressions.get(0) != featureCall) { error("Constructor call must be the first expression in a constructor", null, INVALID_CONSTRUCTOR_INVOCATION); } } } else { error("Constructor call must be the first expression in a constructor", null, INVALID_CONSTRUCTOR_INVOCATION); } } } }
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); } }
protected IConstructorLinkingCandidate getKnownConstructor(XConstructorCall constructorCall, AbstractTypeComputationState state, ResolvedTypes resolvedTypes) { IConstructorLinkingCandidate result = resolvedTypes.getConstructor(constructorCall); if (result != null) { return result; } EObject proxyOrResolved = (EObject) constructorCall.eGet(XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, false); if (proxyOrResolved == null) { result = new NullConstructorLinkingCandidate(constructorCall, state); return result; } if (!proxyOrResolved.eIsProxy()) { result = state.createResolvedLink(constructorCall, (JvmConstructor) proxyOrResolved); return result; } if (!encoder.isCrossLinkFragment(constructorCall.eResource(), EcoreUtil.getURI(proxyOrResolved).fragment())) { JvmConstructor constructor = constructorCall.getConstructor(); if (!constructor.eIsProxy()) { return state.createResolvedLink(constructorCall, constructor); } } return null; }
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; }
public void complete(JvmIdentifiableElement element) { if (element instanceof JvmGenericType) { completeJvmGenericType((JvmGenericType)element); } if (element instanceof JvmDeclaredType) { JvmDeclaredType declaredType = (JvmDeclaredType) element; complete(declaredType.getMembers()); } if(element instanceof JvmConstructor) { completeJvmConstructor((JvmConstructor) element); } if (element instanceof JvmEnumerationType) { completeJvmEnumerationType((JvmEnumerationType)element); } if (element instanceof JvmEnumerationLiteral) { completeJvmEnumerationLiteral((JvmEnumerationLiteral)element); } if (element instanceof JvmAnnotationType) { completeJvmAnnotationType((JvmAnnotationType)element); } }
protected void completeJvmGenericType(JvmGenericType element) { // if no super type add Object ensureSuperTypeObject(element); addAnnotations(element); if (!element.isInterface()) { // if no constructors have been added, add a default constructor if (isEmpty(element.getDeclaredConstructors())) { JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor(); constructor.setSimpleName(element.getSimpleName()); constructor.setVisibility(JvmVisibility.PUBLIC); typeExtensions.setSynthetic(constructor, true); EObject primarySourceElement = associations.getPrimarySourceElement(element); if (primarySourceElement != null) { associator.associate(primarySourceElement, constructor); } element.getMembers().add(constructor); } } }
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; }
@Override public Iterable<IEObjectDescription> getElements(final QualifiedName name) { IEObjectDescription typeDescription = typeScope.getSingleElement(name); if (typeDescription == null) return emptySet(); JvmType type = (JvmType) typeDescription.getEObjectOrProxy(); if (type.eIsProxy() || !(type instanceof JvmGenericType)) { return emptySet(); } final JvmGenericType castedType = (JvmGenericType) type; if (castedType.isInterface()) { return emptySet(); } Iterable<JvmConstructor> constructors = new Iterable<JvmConstructor>() { @Override public Iterator<JvmConstructor> iterator() { return castedType.getDeclaredConstructors().iterator(); } }; return transform(constructors, new Function<JvmConstructor,IEObjectDescription>(){ @Override public IEObjectDescription apply(JvmConstructor from) { return EObjectDescription.create(name, from); } }); }
protected void addFeatureDescriptions(IEObjectDescription typeDescription, List<IEObjectDescription> result) { EObject proxy = getResolvedProxy(typeDescription); if (!proxy.eIsProxy() && proxy instanceof JvmGenericType) { JvmGenericType type = (JvmGenericType) proxy; if (!type.isInterface()) { for(JvmConstructor constructor: type.getDeclaredConstructors()) { boolean visible = visibilityHelper.isVisible(constructor); ConstructorDescription constructorDescription = createConstructorDescription(typeDescription, constructor, visible); result.add(constructorDescription); } } else if (!strict) { result.add(new SimpleIdentifiableElementDescription(typeDescription)); } } else if (proxy instanceof JvmType) { if (!strict) result.add(new SimpleIdentifiableElementDescription(typeDescription)); } }
protected boolean hasJvmConstructorCall(XExpression obj) { if (!(obj instanceof XBlockExpression)) { return false; } XBlockExpression blockExpression = (XBlockExpression) obj; EList<XExpression> expressions = blockExpression.getExpressions(); if (expressions.isEmpty()) { return false; } XExpression expr = expressions.get(0); if (!(expr instanceof XFeatureCall)) { return false; } XFeatureCall featureCall = (XFeatureCall) expr; return featureCall.getFeature() instanceof JvmConstructor; }
protected Object _doEvaluate(XConstructorCall constructorCall, IEvaluationContext context, CancelIndicator indicator) { JvmConstructor jvmConstructor = constructorCall.getConstructor(); List<Object> arguments = evaluateArgumentExpressions(jvmConstructor, constructorCall.getArguments(), context, indicator); Constructor<?> constructor = javaReflectAccess.getConstructor(jvmConstructor); try { if (constructor == null) throw new NoSuchMethodException("Could not find constructor " + jvmConstructor.getIdentifier()); constructor.setAccessible(true); Object result = constructor.newInstance(arguments.toArray(new Object[arguments.size()])); return result; } catch (InvocationTargetException targetException) { throw new EvaluationException(targetException.getTargetException()); } catch (Exception e) { throw new IllegalStateException("Could not invoke constructor: " + jvmConstructor.getIdentifier(), e); } }
protected ITreeAppendable _generateMember(final JvmConstructor it, final ITreeAppendable appendable, final GeneratorConfig config) { ITreeAppendable _xblockexpression = null; { appendable.newLine(); appendable.openScope(); this.generateJavaDoc(it, appendable, config); final ITreeAppendable tracedAppendable = appendable.trace(it); this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config); this.generateModifier(it, tracedAppendable, config); this.generateTypeParameterDeclaration(it, tracedAppendable, config); this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName())); tracedAppendable.append("("); this.generateParameters(it, tracedAppendable, config); tracedAppendable.append(")"); this.generateThrowsClause(it, tracedAppendable, config); tracedAppendable.append(" "); this.generateExecutableBody(it, tracedAppendable, config); appendable.closeScope(); _xblockexpression = appendable; } return _xblockexpression; }
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()); } }
public ITreeAppendable generateMember(final JvmMember it, final ITreeAppendable appendable, final GeneratorConfig config) { if (it instanceof JvmConstructor) { return _generateMember((JvmConstructor)it, appendable, config); } else if (it instanceof JvmOperation) { return _generateMember((JvmOperation)it, appendable, config); } else if (it instanceof JvmField) { return _generateMember((JvmField)it, appendable, config); } else if (it instanceof JvmDeclaredType) { return _generateMember((JvmDeclaredType)it, appendable, config); } else if (it != null) { return _generateMember(it, appendable, config); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(it, appendable, config).toString()); } }
@Test public void test_nestedInterface_Outer_02() { String typeName = NestedInterfaces.class.getName() + "$Outer"; JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); List<JvmConstructor> constructors = Lists.newArrayList(type.getDeclaredConstructors()); assertEquals(0, constructors.size()); assertEquals(JvmVisibility.DEFAULT, type.getVisibility()); assertTrue(type.isStatic()); assertTrue(type.isAbstract()); }
@Test public void test_nestedInterface_Outer_Inner_02() { String typeName = NestedInterfaces.class.getName() + "$Outer$Inner"; JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); List<JvmConstructor> constructors = Lists.newArrayList(type.getDeclaredConstructors()); assertEquals(0, constructors.size()); assertEquals(JvmVisibility.PUBLIC, type.getVisibility()); assertTrue(type.isStatic()); assertTrue(type.isAbstract()); }
@Test public void test_nestedTypes_Outer_02() { String typeName = NestedTypes.Outer.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); List<JvmConstructor> constructors = Lists.newArrayList(type.getDeclaredConstructors()); assertEquals(1, constructors.size()); JvmConstructor parameterlessConstructor = constructors.get(0); assertEquals(0, parameterlessConstructor.getParameters().size()); }
protected JvmConstructor getConstructorFromType(EObject context, Class<?> type, String constructor) { String methodName = type.getName() + "." + constructor; assertNotNull(context); JvmConstructor result = (JvmConstructor) context.eResource().getEObject(methodName); assertNotNull(methodName, result); return result; }
@Test public void test_nestedTypes_Outer_Inner_02() { String typeName = NestedTypes.Outer.Inner.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); List<JvmConstructor> constructors = Lists.newArrayList(type.getDeclaredConstructors()); assertEquals(1, constructors.size()); JvmConstructor parameterlessConstructor = constructors.get(0); assertEquals(1, parameterlessConstructor.getParameters().size()); }
public JvmAnnotationValue getConstructorParameterAnnotationValue(String name) { String typeName = TestAnnotation.Annotated.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated(java.lang.String,java.lang.String,java.lang.String)"); JvmAnnotationTarget target = constructor.getParameters().get(0); return getExplicitAnnotationValue(name, target); }
@Test public void testAnnotatedConstructor_01() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated()"); assertNotNull(constructor); JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor); assertEquals(1, value.getValues().size()); String s = value.getValues().get(0); assertEquals("constructor", s); }
@Test public void testAnnotatedConstructor_02() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated(java.lang.String)"); assertNotNull(constructor); JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor); assertEquals(1, value.getValues().size()); String s = value.getValues().get(0); assertEquals("secondConstructor", s); }
/** * @return the {@link Constructor} corresponding to the given {@link JvmConstructor} or <code>null</code>. */ public Constructor<?> getConstructor(JvmConstructor constructor) { Class<?> declaringType = getRawType(constructor.getDeclaringType()); if (declaringType == null) return null; Class<?>[] paramTypes = getParamTypes(constructor); try { return declaringType.getDeclaredConstructor(paramTypes); } catch (Exception e) { if (log.isDebugEnabled()) log.debug(e.getMessage(), e); } return null; }
@Test public void testAnnotatedConstructor_04() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated(java.lang.String,java.lang.String)"); assertNotNull(constructor); JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor); assertEquals(1, value.getValues().size()); String s = value.getValues().get(0); assertEquals("thirdConstructorWithBody", s); }
@Test public void testAnnotatedParameter_02() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated(java.lang.String,java.lang.String,java.lang.String)"); JvmAnnotationTarget target = constructor.getParameters().get(1); assertEquals(0, target.getAnnotations().size()); }
@Test public void testAnnotatedParameter_03() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider() .findTypeByName(TestAnnotation.NestedAnnotation.class.getName()); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated(java.lang.String,java.lang.String,java.lang.String)"); JvmAnnotationTarget target = constructor.getParameters().get(2); assertEquals(1, target.getAnnotations().size()); JvmAnnotationReference annotationReference = target.getAnnotations().get(0); assertSame(annotationType, annotationReference.getAnnotation()); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public Iterable<JvmConstructor> getDeclaredConstructors() { // TODO: implement this method // Ensure that you remove @generated or mark it @generated NOT throw new UnsupportedOperationException(); }
protected SignatureHashBuilder appendSignature(JvmConstructor operation) { appendVisibility(operation.getVisibility()).appendTypeParameters(operation).append("("); for (JvmFormalParameter p : operation.getParameters()) { appendType(p.getParameterType()).append(" "); } append(") "); for (JvmTypeReference ex : operation.getExceptions()) { appendType(ex).append(" "); } return this; }
protected boolean isStaticContext(EObject element) { if(element instanceof JvmConstructor) return false; if(element instanceof JvmFeature) return ((JvmFeature) element).isStatic(); if(element instanceof JvmDeclaredType) return ((JvmDeclaredType) element).isStatic() || ( ((JvmDeclaredType)element).getDeclaringType() == null && !isLocalType(element)); return false; }
@Check public void checkConstructorArgumentsAreValid(XFeatureCall featureCall) { JvmIdentifiableElement feature = featureCall.getFeature(); if (feature != null && !feature.eIsProxy() && feature instanceof JvmConstructor) { JvmType containerType = EcoreUtil2.getContainerOfType(logicalContainerProvider.getNearestLogicalContainer(featureCall), JvmType.class); for(XExpression argument: featureCall.getFeatureCallArguments()) { checkIsValidConstructorArgument(argument, containerType); } } }