blob: 82fd7aa800d27ad1f537cdd538ef748487f3f629 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.xtext.oxtype.linking;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.osbp.xtext.oxtype.linking.JvmTypeAwareLinkingHelper.IJvmLinkCrossRefStringEnhancer;
import org.eclipse.xtext.linking.impl.DefaultLinkingService;
import org.eclipse.xtext.linking.impl.IllegalNodeException;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.IScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
public class JvmTypeEnhancingLinkingService extends DefaultLinkingService {
private static final Logger LOGGER = LoggerFactory
.getLogger(JvmTypeEnhancingLinkingService.class);
@Inject
private IQualifiedNameConverter qualifiedNameConverter;
@Inject
private JvmTypeAwareLinkingHelper jvmTypeLinkingHelper;
LinkingMetric metric = LinkingMetric.getInstance();
/**
* @return the first element returned from the injected
* {@link IScopeProvider} which matches the text of the passed
* {@link INode node}
*/
public List<EObject> getLinkedObjects(EObject context, EReference ref,
INode node) throws IllegalNodeException {
final EClass requiredType = ref.getEReferenceType();
if (requiredType == null)
return Collections.<EObject> emptyList();
String crossRefString = getCrossRefNodeAsString(node);
if (crossRefString != null && !crossRefString.equals("")) {
// enhance the cross reference string before scoping
try {
EStructuralFeature containingFeature = context
.eContainingFeature();
if (jvmTypeLinkingHelper.isJvmLink(containingFeature)) {
IJvmLinkCrossRefStringEnhancer enhancer = jvmTypeLinkingHelper
.getEnhancer(containingFeature);
if (enhancer != null) {
crossRefString = enhancer.enhance(context,
containingFeature, crossRefString);
}
if (crossRefString == null || crossRefString.equals("")) {
crossRefString = "LUN__UNDEFINED";
}
}
} catch (Exception e) {
LOGGER.error("{}", e);
}
String scopeKey = context.eClass().getEPackage().getNsPrefix()
+ "." + context.eClass().getName() + "#"
+ ref.getName();
Date startScoping = new Date();
final IScope scope = getScope(context, ref);
Date scopeCreated = new Date();
QualifiedName qualifiedLinkName = qualifiedNameConverter
.toQualifiedName(crossRefString);
// TimeLogger doScopeLog = TimeLogger.start(getClass());
IEObjectDescription eObjectDescription = scope
.getSingleElement(qualifiedLinkName);
Date scopingDone = new Date();
long creatingScopeTime = scopeCreated.getTime()
- startScoping.getTime();
long doScopingTime = scopingDone.getTime() - scopeCreated.getTime();
metric.addMetric(scopeKey, creatingScopeTime, doScopingTime);
// for debugging issues
boolean clear = false;
boolean print = false;
if(clear) {
metric.clear();
}
if(print) {
metric.print();
}
if (eObjectDescription != null) {
EObject result = eObjectDescription.getEObjectOrProxy();
doCheckAndLog(context, ref, eObjectDescription);
return Collections.singletonList(result);
}
}
return Collections.emptyList();
}
/**
* This method can be overridden to do any kind of checks for logging
* issues.
*
* @param context
* @param ref
* @param eObjectDescription
*/
protected void doCheckAndLog(EObject context, EReference ref,
IEObjectDescription eObjectDescription) {
}
private String toRefName(EReference ref) {
EClass type = ref.getEReferenceType();
return type.getName() + "#" + ref.getName();
}
}