Java Code Examples for org.eclipse.xtext.common.types.JvmOperation#isAbstract()

The following examples show how to use org.eclipse.xtext.common.types.JvmOperation#isAbstract() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FunctionTypes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private boolean isValidFunction(JvmOperation op) {
	// TODO we need context here - the op has to be visible
	if (op.isAbstract()) {
		if (Object.class.getName().equals(op.getDeclaringType().getIdentifier()))
			return false;
		final String name = op.getSimpleName();
		if (name.equals("toString") && op.getParameters().isEmpty())
			return false;
		if (name.equals("equals") && op.getParameters().size() == 1)
			return false;
		if (name.equals("hashCode") && op.getParameters().isEmpty())
			return false;
		return true;
	}
	return false;
}
 
Example 2
Source File: OverrideTester.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isConflictingDefaultImplementation(AbstractResolvedOperation overriding, AbstractResolvedOperation overridden) {
	JvmOperation ridingDecl = overriding.getDeclaration();
	if (ridingDecl.isStatic()) {
		return false;
	}
	JvmOperation riddenDecl = overridden.getDeclaration();
	if (riddenDecl.isStatic()) {
		return false;
	}
	if (isInterface(ridingDecl.getDeclaringType()) && isInterface(riddenDecl.getDeclaringType())
			&& (!ridingDecl.isAbstract() || !riddenDecl.isAbstract())) {
		LightweightTypeReference ridingTypeRef = overriding.getResolvedDeclarator();
		LightweightTypeReference riddenTypeRef = overridden.getResolvedDeclarator();
		return !riddenTypeRef.isAssignableFrom(ridingTypeRef);
	}
	return false;
}
 
Example 3
Source File: OverrideTester.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected OverrideCheckDetails getPrimaryValidDetail(IResolvedOperation overriding, JvmOperation overridden) {
	OverrideCheckDetails result = OverrideCheckDetails.IMPLEMENTATION;
	JvmOperation declaration = overriding.getDeclaration();
	if (declaration.isStatic()) {
		result = OverrideCheckDetails.SHADOWED;
	} else if (declaration.isAbstract()) {
		if (overridden.isAbstract()) {
			result = OverrideCheckDetails.REPEATED;
		} else {
			result = OverrideCheckDetails.REDECLARATION;
		}
	} else if (!overridden.isAbstract()) {
		result = OverrideCheckDetails.OVERRIDE;
	}
	return result;
}
 
Example 4
Source File: OverrideProposalUtil.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean isCandidate(LightweightTypeReference type, IResolvedExecutable executable,
		IVisibilityHelper visibilityHelper) {
	JvmDeclaredType declaringType = executable.getDeclaration().getDeclaringType();
	if (type.getType() != declaringType && isVisible(executable, visibilityHelper)) {
		JvmExecutable rawExecutable = executable.getDeclaration();
		if (rawExecutable instanceof JvmOperation) {
			JvmOperation operation = (JvmOperation) rawExecutable;
			if (operation.isFinal() || operation.isStatic()) {
				return false;
			} else {
				if (type.getType() instanceof JvmGenericType && ((JvmGenericType) type.getType()).isInterface()) {
					return  declaringType instanceof JvmGenericType
							&& ((JvmGenericType) declaringType).isInterface() && !operation.isAbstract();
				} else {
					return true;
				}
			}
		} else {
			return true;
		}
	}
	return false;
}
 
Example 5
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void createMethods(Class<?> clazz, JvmDeclaredType result) {
	try {
		Method[] declaredMethods = clazz.getDeclaredMethods();
		if (declaredMethods.length != 0) {
			boolean intf = clazz.isInterface() && !clazz.isAnnotation();
			InternalEList<JvmMember> members = (InternalEList<JvmMember>)result.getMembers();
			for (Method method : declaredMethods) {
				if (!method.isSynthetic()) {
					JvmOperation operation = createOperation(method);
					if (clazz.isAnnotation()) {
						setDefaultValue(operation, method);
					} else if (intf && !operation.isAbstract() && !operation.isStatic()) {
						operation.setDefault(true);
					}
					members.addUnique(operation);
				}
			}
		}
	} catch (NoClassDefFoundError e) {
		logNoClassDefFoundError(e, clazz, "methods");
	}
}
 
Example 6
Source File: JvmDeclaredTypeSignatureHashProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected SignatureHashBuilder appendSignature(JvmOperation operation) {
	appendVisibility(operation.getVisibility()).append(" ");
	if (operation.isAbstract())
		append("abstract ");
	if (operation.isStatic())
		append("static ");
	if (operation.isFinal())
		append("final ");
	appendType(operation.getReturnType()).appendTypeParameters(operation).append(" ")
			.append(operation.getSimpleName()).append("(");
	for (JvmFormalParameter p : operation.getParameters()) {
		appendType(p.getParameterType());
		append(" ");
	}
	append(") ");
	for (JvmTypeReference ex : operation.getExceptions()) {
		appendType(ex).append(" ");
	}
	return this;
}
 
Example 7
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable _generateModifier(final JvmOperation it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    this.generateVisibilityModifier(it, appendable);
    if ((it.isAbstract() && (!this.isDeclaredWithinInterface(it)))) {
      appendable.append("abstract ");
    }
    boolean _isStatic = it.isStatic();
    if (_isStatic) {
      appendable.append("static ");
    }
    if (((((!it.isAbstract()) && (!it.isStatic())) && config.getJavaSourceVersion().isAtLeast(JavaVersion.JAVA8)) && this.isDeclaredWithinInterface(it))) {
      appendable.append("default ");
    }
    boolean _isFinal = it.isFinal();
    if (_isFinal) {
      appendable.append("final ");
    }
    boolean _isSynchronized = it.isSynchronized();
    if (_isSynchronized) {
      appendable.append("synchronized ");
    }
    boolean _isStrictFloatingPoint = it.isStrictFloatingPoint();
    if (_isStrictFloatingPoint) {
      appendable.append("strictfp ");
    }
    ITreeAppendable _xifexpression = null;
    boolean _isNative = it.isNative();
    if (_isNative) {
      _xifexpression = appendable.append("native ");
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example 8
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmOperation 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);
    JvmTypeReference _returnType = it.getReturnType();
    boolean _tripleEquals = (_returnType == null);
    if (_tripleEquals) {
      tracedAppendable.append("void");
    } else {
      this._errorSafeExtensions.serializeSafely(it.getReturnType(), "Object", tracedAppendable);
    }
    tracedAppendable.append(" ");
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
    tracedAppendable.append("(");
    this.generateParameters(it, tracedAppendable, config);
    tracedAppendable.append(")");
    this.generateThrowsClause(it, tracedAppendable, config);
    if ((it.isAbstract() || (!this.hasBody(it)))) {
      tracedAppendable.append(";");
    } else {
      tracedAppendable.append(" ");
      this.generateExecutableBody(it, tracedAppendable, config);
    }
    appendable.closeScope();
    _xblockexpression = appendable;
  }
  return _xblockexpression;
}
 
Example 9
Source File: SARLJvmGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected ITreeAppendable _generateMember(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
	if (Utils.STATIC_CONSTRUCTOR_NAME.equals(it.getSimpleName())) {
		// The constructor name is not the same as the declaring type.
		// We assume that the constructor is a static constructor.
		return generateStaticConstructor(it, appendable, config);
	}
	// The code below is adapted from the code of the Xtend super type.
	appendable.newLine();
	appendable.openScope();
	generateJavaDoc(it, appendable, config);
	final ITreeAppendable tracedAppendable = appendable.trace(it);
	generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
	// Specific case: automatic generation
	if (this.operationHelper.isPureOperation(it)
			&& this.annotations.findAnnotation(it, Pure.class) == null) {
		tracedAppendable.append("@").append(Pure.class).newLine(); //$NON-NLS-1$
	}
	generateModifier(it, tracedAppendable, config);
	generateTypeParameterDeclaration(it, tracedAppendable, config);
	if (it.getReturnType() == null) {
		tracedAppendable.append("void"); //$NON-NLS-1$
	} else {
		this._errorSafeExtensions.serializeSafely(it.getReturnType(), Object.class.getSimpleName(), tracedAppendable);
	}
	tracedAppendable.append(" "); //$NON-NLS-1$
	this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(makeJavaIdentifier(it.getSimpleName()));
	tracedAppendable.append("("); //$NON-NLS-1$
	generateParameters(it, tracedAppendable, config);
	tracedAppendable.append(")"); //$NON-NLS-1$
	generateThrowsClause(it, tracedAppendable, config);
	if (it.isAbstract() || !hasBody(it)) {
		tracedAppendable.append(";"); //$NON-NLS-1$
	} else {
		tracedAppendable.append(" "); //$NON-NLS-1$
		generateExecutableBody(it, tracedAppendable, config);
	}
	appendable.closeScope();
	return appendable;
}
 
Example 10
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void computeFeatureCallHighlighting(XAbstractFeatureCall featureCall, IHighlightedPositionAcceptor acceptor) {
	JvmIdentifiableElement feature = featureCall.getFeature();
	if (feature != null && !feature.eIsProxy()) {
		
		if (feature instanceof XVariableDeclaration) {
			if (!SPECIAL_FEATURE_NAMES.contains(((XVariableDeclaration) feature).getName())) {
				// highlighting of special identifiers is done separately, so it's omitted here 
				highlightFeatureCall(featureCall, acceptor, LOCAL_VARIABLE);
				
				if (!((XVariableDeclaration) feature).isWriteable()) {
					highlightFeatureCall(featureCall, acceptor, LOCAL_FINAL_VARIABLE);
				}
			}
			
		} else if (feature instanceof JvmFormalParameter) {
			if (!SPECIAL_FEATURE_NAMES.contains(((JvmFormalParameter) feature).getName())) {
				// highlighting of special identifiers is done separately, so it's omitted here 
				final EObject eContainingFeature = feature.eContainingFeature();
				
				if (eContainingFeature == TypesPackage.Literals.JVM_EXECUTABLE__PARAMETERS
						|| eContainingFeature == XbasePackage.Literals.XCLOSURE__DECLARED_FORMAL_PARAMETERS) {
					// which is the case for constructors and methods
					highlightFeatureCall(featureCall, acceptor, PARAMETER_VARIABLE);
				} else {
					// covers parameters of for and template expr FOR loops, as well as switch statements
					highlightFeatureCall(featureCall, acceptor, LOCAL_VARIABLE);
					highlightFeatureCall(featureCall, acceptor, LOCAL_FINAL_VARIABLE);
				}
			}
			
		} else if (feature instanceof JvmTypeParameter) {
			highlightFeatureCall(featureCall, acceptor, TYPE_VARIABLE);
			
		} else if (feature instanceof JvmField) {
			highlightFeatureCall(featureCall, acceptor, FIELD);
			
			if (((JvmField) feature).isStatic()) {
				highlightFeatureCall(featureCall, acceptor, STATIC_FIELD);
				
				if (((JvmField) feature).isFinal()) {
					highlightFeatureCall(featureCall, acceptor, STATIC_FINAL_FIELD);
				}
			}
			
		} else if (feature instanceof JvmOperation && !featureCall.isOperation()) {
			JvmOperation jvmOperation = (JvmOperation) feature;
			
			highlightFeatureCall(featureCall, acceptor, METHOD);
			
			if (jvmOperation.isAbstract()) {
				highlightFeatureCall(featureCall, acceptor, ABSTRACT_METHOD_INVOCATION);
			}
			
			if (jvmOperation.isStatic()) {
				highlightFeatureCall(featureCall, acceptor, STATIC_METHOD_INVOCATION);
			}
			
			if (featureCall.isExtension() || isExtensionWithImplicitFirstArgument(featureCall)) {
				highlightFeatureCall(featureCall, acceptor, EXTENSION_METHOD_INVOCATION);
			}
			
		} else if (feature instanceof JvmDeclaredType) {
			highlightReferenceJvmType(acceptor, featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, feature);
		}
		
		if(feature instanceof JvmAnnotationTarget && DeprecationUtil.isTransitivelyDeprecated((JvmAnnotationTarget)feature)){
			highlightFeatureCall(featureCall, acceptor, DEPRECATED_MEMBERS);
		}
	}
}
 
Example 11
Source File: XbaseReferenceProposalCreator.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Image computeImage(JvmFeature feature) {
	int flags = 0;
	int decorator = 0;
	switch(feature.getVisibility()) {
		case PUBLIC: flags = Flags.AccPublic; break;
		case PROTECTED: flags = Flags.AccProtected; break;
		case PRIVATE: flags = Flags.AccPrivate; break;
		case DEFAULT: flags = Flags.AccDefault; break;
	}
	JvmDeclaredType declaringType = feature.getDeclaringType();
	boolean interfaceOrAnnotation = false;
	if (declaringType instanceof JvmGenericType) {
		interfaceOrAnnotation = ((JvmGenericType) declaringType).isInterface();
	} else if (declaringType instanceof JvmAnnotationType) {
		interfaceOrAnnotation = true;
	}
	if (feature instanceof JvmConstructor) {
		decorator = JavaElementImageDescriptor.CONSTRUCTOR;
		if (declaringType.isAbstract()) {
			flags |= Flags.AccAbstract;
			decorator |= JavaElementImageDescriptor.ABSTRACT;
		}
		return computeConstructorImage(declaringType.getDeclaringType() != null, interfaceOrAnnotation, flags, decorator);
	} else if (feature instanceof JvmOperation) {
		JvmOperation operation = (JvmOperation) feature;
		if (operation.isStatic()) {
			flags |= Flags.AccStatic;
			decorator |= JavaElementImageDescriptor.STATIC;
		}
		if (operation.isAbstract()) {
			flags |= Flags.AccAbstract;
			decorator |= JavaElementImageDescriptor.ABSTRACT;
		}
		if (operation.isFinal()) {
			flags |= Flags.AccFinal;
			decorator |= JavaElementImageDescriptor.FINAL;
		}
		return computeMethodImage(interfaceOrAnnotation, flags, decorator);
	} else if (feature instanceof JvmField) {
		JvmField field = (JvmField) feature;
		if (field.isStatic()) {
			flags |= Flags.AccStatic;
			decorator |= JavaElementImageDescriptor.STATIC;
		}
		if (field.isFinal()) {
			flags |= Flags.AccFinal;
			decorator |= JavaElementImageDescriptor.FINAL;
		}
		if (declaringType instanceof JvmEnumerationType)
			flags |= Flags.AccEnum;
		return computeFieldImage(interfaceOrAnnotation, flags, decorator);
	} 
	return null;
}
 
Example 12
Source File: OverrideIndicatorModelListener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isOverwriteIndicator(JvmOperation jvmOperation) {
	return !jvmOperation.isAbstract();
}
 
Example 13
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void transform(XtendFunction source, JvmGenericType container, boolean allowDispatch) {
	JvmOperation operation = typesFactory.createJvmOperation();
	operation.setAbstract(source.isAbstract());
	operation.setNative(source.isNative());
	operation.setSynchronized(source.isSynchonized());
	operation.setStrictFloatingPoint(source.isStrictFloatingPoint());
	if (!source.isAbstract())
		operation.setFinal(source.isFinal());
	container.getMembers().add(operation);
	associator.associatePrimary(source, operation);
	String sourceName = source.getName();
	JvmVisibility visibility = source.getVisibility();
	if (allowDispatch && source.isDispatch()) {
		if (source.getDeclaredVisibility() == null)
			visibility = JvmVisibility.PROTECTED;
		sourceName = "_" + sourceName;
	}
	operation.setSimpleName(sourceName);
	operation.setVisibility(visibility);
	operation.setStatic(source.isStatic());
	if (!operation.isAbstract() && !operation.isStatic() && container.isInterface())
		operation.setDefault(true);
	for (XtendParameter parameter : source.getParameters()) {
		translateParameter(operation, parameter);
	}
	XExpression expression = source.getExpression();
	CreateExtensionInfo createExtensionInfo = source.getCreateExtensionInfo();
	
	JvmTypeReference returnType = null;
	if (source.getReturnType() != null) {
		returnType = jvmTypesBuilder.cloneWithProxies(source.getReturnType());
	} else if (createExtensionInfo != null) {
		returnType = jvmTypesBuilder.inferredType(createExtensionInfo.getCreateExpression());
	} else if (expression != null) {
		returnType = jvmTypesBuilder.inferredType(expression);
	} else {
		returnType = jvmTypesBuilder.inferredType();
	}
	
	operation.setReturnType(returnType);
	copyAndFixTypeParameters(source.getTypeParameters(), operation);
	for (JvmTypeReference exception : source.getExceptions()) {
		operation.getExceptions().add(jvmTypesBuilder.cloneWithProxies(exception));
	}
	translateAnnotationsTo(source.getAnnotations(), operation);
	if (source.isOverride() && typeReferences.findDeclaredType(Override.class, source) != null)
		setOverride(operation);
	if (createExtensionInfo != null) {
		transformCreateExtension(source, createExtensionInfo, container, operation, returnType);
	} else {
		setBody(operation, expression);
	}
	jvmTypesBuilder.copyDocumentationTo(source, operation);
}
 
Example 14
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Create the operations into the SARL feature container.
 *
 * @param codeBuilder the builder of the script.
 * @param methods the operations to create.
 * @param context the context of the actions. If {@code null}, the block of the actions are not generated.
 * @throws JavaModelException if the Java model is invalid.
 * @throws IllegalArgumentException if the signature is not syntactically correct.
 */
public void createActionsWith(
		ActionBuilder codeBuilder,
		Collection<IMethod> methods,
		XtendTypeDeclaration context) throws JavaModelException, IllegalArgumentException {
	if (methods != null) {
		for (final IMethod operation : methods) {
			if (!isGeneratedOperation(operation)) {
				final ISarlActionBuilder action = codeBuilder.addAction(operation.getElementName());
				action.setReturnType(Signature.toString(operation.getReturnType()));
				final IFormalParameterBuilder[] sarlParams = createFormalParametersWith(
					name -> action.addParameter(name), operation);
				if (context != null) {
					final JvmType type = this.typeReferences.findDeclaredType(
							operation.getDeclaringType().getFullyQualifiedName(),
							context);
					final JvmOperation superOperation = getJvmOperation(operation, type);
					// Create the block
					final IBlockExpressionBuilder block = action.getExpression();
					if ((type.eClass() != TypesPackage.Literals.JVM_GENERIC_TYPE
							|| !((JvmGenericType) type).isInterface())
							&& superOperation != null
							&& !superOperation.isAbstract()) {
						// Create the super-call expression
						final IExpressionBuilder superCall = block.addExpression();
						final XMemberFeatureCall call = XbaseFactory.eINSTANCE.createXMemberFeatureCall();
						superCall.setXExpression(call);
						superCall.setDocumentation(block.getAutoGeneratedActionString());
						call.setFeature(superOperation);
						call.setMemberCallTarget(superCall.createReferenceToSuper());
						final List<XExpression> arguments = call.getMemberCallArguments();
						for (final IFormalParameterBuilder currentParam : sarlParams) {
							final XFeatureCall argumentSource = XbaseFactory.eINSTANCE.createXFeatureCall();
							arguments.add(argumentSource);
							currentParam.setReferenceInto(argumentSource);
						}
					} else {
						final JvmTypeReference ret = superOperation != null ? superOperation.getReturnType() : null;
						block.setDefaultAutoGeneratedContent(
								ret == null ? null : ret.getIdentifier());
					}
				}
			}
		}
	}
}