@Test public void testMethods_publicAbstractMethod_01() { String typeName = Methods.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation method = getMethodFromType(type, Methods.class, "publicAbstractMethod()"); assertSame(type, method.getDeclaringType()); assertTrue(method.isAbstract()); assertFalse(method.isFinal()); assertFalse(method.isStatic()); assertFalse(method.isSynchronized()); assertFalse(method.isStrictFloatingPoint()); assertFalse(method.isNative()); assertEquals(JvmVisibility.PUBLIC, method.getVisibility()); JvmType methodType = method.getReturnType().getType(); assertEquals("void", methodType.getIdentifier()); }
@Test public void testMethods_protectedFinalMethod_01() { String typeName = Methods.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation method = getMethodFromType(type, Methods.class, "protectedFinalMethod()"); assertSame(type, method.getDeclaringType()); assertFalse(method.isAbstract()); assertTrue(method.isFinal()); assertFalse(method.isStatic()); assertFalse(method.isSynchronized()); assertFalse(method.isStrictFloatingPoint()); assertFalse(method.isNative()); assertEquals(JvmVisibility.PROTECTED, method.getVisibility()); JvmType methodType = method.getReturnType().getType(); assertEquals("void", methodType.getIdentifier()); }
@Test public void testMethods_defaultStaticMethod_01() { String typeName = Methods.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation method = getMethodFromType(type, Methods.class, "defaultStaticMethod()"); assertSame(type, method.getDeclaringType()); assertFalse(method.isAbstract()); assertFalse(method.isFinal()); assertTrue(method.isStatic()); assertFalse(method.isSynchronized()); assertFalse(method.isStrictFloatingPoint()); assertFalse(method.isNative()); assertEquals(JvmVisibility.DEFAULT, method.getVisibility()); JvmType methodType = method.getReturnType().getType(); assertEquals("void", methodType.getIdentifier()); }
@Test public void testMethods_privateSynchronizedMethod_01() { String typeName = Methods.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation method = getMethodFromType(type, Methods.class, "privateSynchronizedMethod()"); assertSame(type, method.getDeclaringType()); assertFalse(method.isAbstract()); assertFalse(method.isFinal()); assertFalse(method.isStatic()); assertTrue(method.isSynchronized()); assertFalse(method.isStrictFloatingPoint()); assertFalse(method.isNative()); assertEquals(JvmVisibility.PRIVATE, method.getVisibility()); JvmType methodType = method.getReturnType().getType(); assertEquals("void", methodType.getIdentifier()); }
@Test public void testMethods_publicStrictFpMethod_01() { String typeName = Methods.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation method = getMethodFromType(type, Methods.class, "publicStrictFpMethod()"); assertSame(type, method.getDeclaringType()); assertFalse(method.isAbstract()); assertFalse(method.isFinal()); assertFalse(method.isStatic()); assertFalse(method.isSynchronized()); assertTrue(method.isStrictFloatingPoint()); assertFalse(method.isNative()); assertEquals(JvmVisibility.PUBLIC, method.getVisibility()); JvmType methodType = method.getReturnType().getType(); assertEquals("void", methodType.getIdentifier()); }
@Test public void publicNativeMethod() { String typeName = Methods.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmOperation method = getMethodFromType(type, Methods.class, "publicStrictFpMethod()"); assertSame(type, method.getDeclaringType()); assertFalse(method.isAbstract()); assertFalse(method.isFinal()); assertFalse(method.isStatic()); assertFalse(method.isSynchronized()); assertTrue(method.isStrictFloatingPoint()); assertFalse(method.isNative()); assertEquals(JvmVisibility.PUBLIC, method.getVisibility()); JvmType methodType = method.getReturnType().getType(); assertEquals("void", methodType.getIdentifier()); }
protected boolean isLocallyUsed(EObject target, EObject containerToFindUsage) { if (readAndWriteTracking.isRead(target)) { return true; } Collection<Setting> usages = XbaseUsageCrossReferencer.find(target, containerToFindUsage); // for non-private members it is enough to check that there are usages if (!(target instanceof JvmOperation) || ((JvmOperation)target).getVisibility()!=JvmVisibility.PRIVATE) { return !usages.isEmpty(); } else { // for private members it has to be checked if all usages are within the operation EObject targetSourceElem = associations.getPrimarySourceElement(target); for (Setting s : usages) { if (s.getEObject() instanceof XAbstractFeatureCall) { XAbstractFeatureCall fc = (XAbstractFeatureCall) s.getEObject(); // when the feature call does not call itself or the call is // from another function, then it is locally used if (fc.getFeature() != target || !EcoreUtil.isAncestor(targetSourceElem, fc)) return true; } else { return true; } } return false; } }
/** * Returns <code>null</code> if the given operation declares it's own return type or if it does not override * another operation. */ /* @Nullable */ @SuppressWarnings("unused") protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ResolvedTypes resolvedTypes, IFeatureScopeSession session) { if (operation.getVisibility() == JvmVisibility.PRIVATE) return null; if (InferredTypeIndicator.isInferred(operation.getReturnType())) { LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType()); if (declaringType == null) { throw new IllegalStateException("Cannot determine declaring type of operation: " + operation); } LightweightTypeReference result = overrideHelper.getReturnTypeOfOverriddenOperation(operation, declaringType); return result; } return null; }
protected boolean isMorePrivateThan(JvmVisibility o1, JvmVisibility o2) { if (o1 == o2) { return false; } else { switch (o1) { case DEFAULT: return o2 != JvmVisibility.PRIVATE; case PRIVATE: return true; case PROTECTED: return o2 == JvmVisibility.PUBLIC; case PUBLIC: return false; default: throw new IllegalArgumentException("Unknown JvmVisibility " + o1); } } }
/** * Creates a getter method for the given property name and the field name. * * Example: <code> * public String getPropertyName() { * return this.fieldName; * } * </code> * * @return a getter method for a JavaBeans property, <code>null</code> if sourceElement or name are <code>null</code>. */ /* @Nullable */ public JvmOperation toGetter(/* @Nullable */ final EObject sourceElement, /* @Nullable */ final String propertyName, /* @Nullable */ final String fieldName, /* @Nullable */ JvmTypeReference typeRef) { if(sourceElement == null || propertyName == null || fieldName == null) return null; JvmOperation result = typesFactory.createJvmOperation(); result.setVisibility(JvmVisibility.PUBLIC); String prefix = (isPrimitiveBoolean(typeRef) ? "is" : "get"); result.setSimpleName(prefix + Strings.toFirstUpper(propertyName)); result.setReturnType(cloneWithProxies(typeRef)); setBody(result, new Procedures.Procedure1<ITreeAppendable>() { @Override public void apply(/* @Nullable */ ITreeAppendable p) { if(p != null) { p = p.trace(sourceElement); p.append("return this."); p.append(javaKeywords.isJavaKeyword(fieldName) ? fieldName+"_" : fieldName); p.append(";"); } } }); return associate(sourceElement, result); }
/** * Creates a setter method for the given properties name with the standard implementation assigning the passed * parameter to a similarly named field. * * Example: <code> * public void setFoo(String foo) { * this.foo = foo; * } * </code> * * @return a setter method for a JavaBeans property with the given name, <code>null</code> if sourceElement or name are <code>null</code>. */ /* @Nullable */ public JvmOperation toSetter(/* @Nullable */ final EObject sourceElement, /* @Nullable */ final String propertyName, /* @Nullable */ final String fieldName, /* @Nullable */ JvmTypeReference typeRef) { if(sourceElement == null || propertyName == null || fieldName == null) return null; JvmOperation result = typesFactory.createJvmOperation(); result.setVisibility(JvmVisibility.PUBLIC); result.setReturnType(references.getTypeForName(Void.TYPE,sourceElement)); result.setSimpleName("set" + Strings.toFirstUpper(propertyName)); result.getParameters().add(toParameter(sourceElement, propertyName, typeRef)); setBody(result, new Procedures.Procedure1<ITreeAppendable>() { @Override public void apply(/* @Nullable */ ITreeAppendable p) { if(p != null) { p = p.trace(sourceElement); p.append("this."); p.append(javaKeywords.isJavaKeyword(fieldName) ? fieldName+"_" : fieldName); p.append(" = "); p.append(javaKeywords.isJavaKeyword(propertyName) ? propertyName+"_" : propertyName); p.append(";"); } } }); return associate(sourceElement, result); }
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 void setVisibility(int accessFlags, JvmMember result) { switch (accessFlags & (ACC_PRIVATE | ACC_PROTECTED | ACC_PUBLIC)) { case ACC_PRIVATE: result.setVisibility(JvmVisibility.PRIVATE); return; case ACC_PROTECTED: result.setVisibility(JvmVisibility.PROTECTED); return; case ACC_PUBLIC: result.setVisibility(JvmVisibility.PUBLIC); return; default: result.setVisibility(JvmVisibility.DEFAULT); return; } }
protected void setVisibility(int accessFlags, JvmMember result) { switch (accessFlags & (ACC_PRIVATE | ACC_PROTECTED | ACC_PUBLIC)) { case ACC_PRIVATE: result.setVisibility(JvmVisibility.PRIVATE); return; case ACC_PROTECTED: result.setVisibility(JvmVisibility.PROTECTED); return; case ACC_PUBLIC: result.setVisibility(JvmVisibility.PUBLIC); return; default: result.setVisibility(JvmVisibility.DEFAULT); return; } }
public SignatureHashBuilder appendSignature(JvmDeclaredType type) { if (type.getVisibility() != JvmVisibility.PRIVATE) { appendAnnotationReferences(type); appendVisibility(type.getVisibility()).append(" "); if (type.isAbstract()) append("abstract "); if (type.isStatic()) append("static "); if (type.isFinal()) append("final "); append("class ").append(type.getIdentifier()); if (type instanceof JvmTypeParameterDeclarator) appendTypeParameters((JvmTypeParameterDeclarator) type); append("\n").appendSuperTypeSignatures(type).appendMemberSignatures(type, false); } return this; }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @Override public void eSet(int featureID, Object newValue) { switch (featureID) { case TypesPackage.JVM_MEMBER__DECLARING_TYPE: setDeclaringType((JvmDeclaredType)newValue); return; case TypesPackage.JVM_MEMBER__VISIBILITY: setVisibility((JvmVisibility)newValue); return; case TypesPackage.JVM_MEMBER__SIMPLE_NAME: setSimpleName((String)newValue); return; case TypesPackage.JVM_MEMBER__DEPRECATED: setDeprecated((Boolean)newValue); return; } super.eSet(featureID, newValue); }
@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 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); }
@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()); }
@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()); }
@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()); }
@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()); }
@Test public void testFields_transientInt_01() { String typeName = Fields.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmField field = getFieldFromType(type, Fields.class, "transientInt"); assertSame(type, field.getDeclaringType()); assertTrue(field.isTransient()); assertFalse(field.isVolatile()); assertEquals(JvmVisibility.DEFAULT, field.getVisibility()); JvmType fieldType = field.getType().getType(); assertEquals("int", fieldType.getIdentifier()); }
@Test public void testFields_publicInt_01() { String typeName = Fields.class.getName(); JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName); JvmField field = getFieldFromType(type, Fields.class, "publicInt"); assertSame(type, field.getDeclaringType()); assertEquals(JvmVisibility.PUBLIC, field.getVisibility()); JvmType fieldType = field.getType().getType(); assertEquals("int", fieldType.getIdentifier()); assertTrue(field.getType().getType() instanceof JvmPrimitiveType); }
@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()); }
@Test public void testEnum_03() throws Exception { String typeName = TestEnum.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); assertEquals(2, type.getLiterals().size()); Set<String> expectedLiterals = Sets.newHashSet(TestEnum.FirstValue.name(), TestEnum.SecondValue.name()); for (JvmEnumerationLiteral literal : type.getLiterals()) { assertTrue(expectedLiterals.remove(literal.getSimpleName())); assertSame(type, literal.getEnumType()); assertEquals(JvmVisibility.PUBLIC, literal.getVisibility()); } assertTrue(expectedLiterals.toString(), expectedLiterals.isEmpty()); }
@Test public void testNestedEnum_03() throws Exception { String typeName = TestEnum.Nested.class.getName(); JvmEnumerationType type = (JvmEnumerationType) getTypeProvider().findTypeByName(typeName); assertEquals(1, type.getLiterals().size()); Set<String> expectedLiterals = Sets.newHashSet(TestEnum.Nested.SINGLETON.name()); for (JvmEnumerationLiteral literal : type.getLiterals()) { assertTrue(expectedLiterals.remove(literal.getSimpleName())); assertSame(type, literal.getEnumType()); assertEquals(JvmVisibility.PUBLIC, literal.getVisibility()); } assertTrue(expectedLiterals.toString(), expectedLiterals.isEmpty()); }
@Override public IResolvedOperation getOverriddenMethod() { if (!getDeclaration().isAbstract() && getDeclaration().getVisibility() != JvmVisibility.PRIVATE) { List<IResolvedOperation> overriddenAndImplemented = getOverriddenAndImplementedMethods(); for(IResolvedOperation candidate: overriddenAndImplemented) { if (!candidate.getDeclaration().isAbstract()) { return candidate; } } } return null; }
public JvmOperation findOverriddenOperation(JvmOperation operation) { if (operation.getVisibility() == JvmVisibility.PRIVATE) { return null; } ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, operation.eResource().getResourceSet()); LightweightTypeReference declaringType = owner.toLightweightTypeReference(operation.getDeclaringType()); TypeParameterSubstitutor<?> substitutor = createSubstitutor(owner, declaringType); return findOverriddenOperation(operation, declaringType, substitutor, owner, new ContextualVisibilityHelper(visibilityHelper, declaringType)); }
@Override public boolean isVisible(JvmMember member) { // TODO private visibility? JvmVisibility visibility = member.getVisibility(); if (visibility == JvmVisibility.PUBLIC) { return true; } JvmDeclaredType type = member instanceof JvmDeclaredType ? (JvmDeclaredType) member : member.getDeclaringType(); if (type == rawContextType || EcoreUtil.isAncestor(rawContextType, type)) { return true; } if (type != null && visibility == JvmVisibility.PROTECTED) { if (superTypeNames == null) { this.superTypeNames = computeSuperTypeNames(); } if (superTypeNames.contains(type.getIdentifier())) { return true; } if (type == member) { JvmDeclaredType declaringType = member.getDeclaringType(); if (declaringType != null && superTypeNames.contains(declaringType.getIdentifier())) { return true; } } } if (type != null && (rawContextType == null || rawContextType instanceof JvmDeclaredType) && (visibility == JvmVisibility.DEFAULT || visibility == JvmVisibility.PROTECTED)) { if (Strings.isEmpty(packageName) && Strings.isEmpty(type.getPackageName()) || (packageName != null && packageName.equals(type.getPackageName()))) { return true; } } return parent.isVisible(member); }
/** * Creates a public enum declaration, associated to the given sourceElement. It sets the given name, which might be * fully qualified using the standard Java notation. * * @param sourceElement * the sourceElement the resulting element is associated with. * @param name * the qualified name of the resulting enum type. * @param initializer * the initializer to apply on the created enumeration type. If <code>null</code>, the enum won't be initialized. * * @return a result representing a Java enum type with the given name, <code>null</code> * if sourceElement or name are <code>null</code>. */ /* @Nullable */ public JvmEnumerationType toEnumerationType(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ Procedure1<? super JvmEnumerationType> initializer) { if (sourceElement == null || name == null) return null; Pair<String, String> fullName = splitQualifiedName(name); JvmEnumerationType result = typesFactory.createJvmEnumerationType(); result.setSimpleName(fullName.getSecond()); result.setVisibility(JvmVisibility.PUBLIC); if (fullName.getFirst() != null) result.setPackageName(fullName.getFirst()); associate(sourceElement, result); return initializeSafely(result, initializer); }
/** * Same as {@link #toEnumerationLiteral(EObject, String)} but with an initializer passed as the last argument. */ /* @Nullable */ public JvmEnumerationLiteral toEnumerationLiteral(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name, /* @Nullable */ Procedure1<? super JvmEnumerationLiteral> initializer) { if (sourceElement == null || name == null) return null; JvmEnumerationLiteral result = typesFactory.createJvmEnumerationLiteral(); result.setSimpleName(name); result.setVisibility(JvmVisibility.PUBLIC); associate(sourceElement, result); return initializeSafely(result, initializer); }
protected JvmGenericType createJvmGenericType(/* @Nullable */ EObject sourceElement, /* @Nullable */ String name) { if (sourceElement == null || name == null) return null; Pair<String, String> fullName = splitQualifiedName(name); final JvmGenericType result = typesFactory.createJvmGenericType(); result.setSimpleName(fullName.getSecond()); if (fullName.getFirst() != null) result.setPackageName(fullName.getFirst()); result.setVisibility(JvmVisibility.PUBLIC); return result; }