-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 itemis AG (http://www.itemis.eu) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.xtext.xbase.validation; | ||
|
||
import static org.eclipse.xtext.util.Strings.*; | ||
import static org.eclipse.xtext.xbase.validation.IssueCodes.*; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.EPackage; | ||
import org.eclipse.emf.ecore.EStructuralFeature; | ||
import org.eclipse.xtext.common.types.JvmDeclaredType; | ||
import org.eclipse.xtext.common.types.JvmGenericType; | ||
import org.eclipse.xtext.common.types.JvmTypeReference; | ||
import org.eclipse.xtext.common.types.TypesPackage; | ||
import org.eclipse.xtext.validation.AbstractDeclarativeValidator; | ||
import org.eclipse.xtext.validation.Check; | ||
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations; | ||
import org.eclipse.xtext.xtype.XtypePackage; | ||
|
||
import com.google.inject.Inject; | ||
|
||
/** | ||
* @author Lorenzo Bettini - Initial contribution and API | ||
* @author Sebastian Zarnekow - Author of the original Java code in XtendValidator extracted here. | ||
*/ | ||
public class JvmGenericTypeValidator extends AbstractDeclarativeValidator { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
szarnekow
|
||
@Inject | ||
private IJvmModelAssociations associations; | ||
|
||
@Override | ||
protected List<EPackage> getEPackages() { | ||
return List.of(TypesPackage.eINSTANCE, XtypePackage.eINSTANCE); | ||
} | ||
|
||
@Check | ||
public void checkSuperTypes(JvmGenericType type) { | ||
var primarySourceElement = associations.getPrimarySourceElement(type); | ||
if (primarySourceElement != null && hasCycleInHierarchy(type, new HashSet<>())) { | ||
error("The inheritance hierarchy of " + notNull(type.getSimpleName()) + " contains cycles", | ||
getNameFeature(primarySourceElement), CYCLIC_INHERITANCE); | ||
} | ||
} | ||
|
||
protected boolean hasCycleInHierarchy(JvmGenericType type, Set<JvmGenericType> processedSuperTypes) { | ||
JvmDeclaredType container = type; | ||
do { | ||
if (processedSuperTypes.contains(container)) | ||
return true; | ||
container = container.getDeclaringType(); | ||
} while (container != null); | ||
processedSuperTypes.add(type); | ||
for (JvmTypeReference superTypeRef : type.getSuperTypes()) { | ||
if (superTypeRef.getType() instanceof JvmGenericType) { | ||
if (hasCycleInHierarchy((JvmGenericType) superTypeRef.getType(), processedSuperTypes)) | ||
return true; | ||
} | ||
} | ||
processedSuperTypes.remove(type); | ||
return false; | ||
} | ||
|
||
protected EStructuralFeature getNameFeature(EObject object) { | ||
return object.eClass().getEStructuralFeature("name"); | ||
} | ||
} |
You might need to override
isLanguageSpecific
and return false from it.