use of org.eclipse.ceylon.model.loader.model.FieldValue in project ceylon by eclipse.
the class ValueImpl method initSetter.
private void initSetter(org.eclipse.ceylon.model.typechecker.model.Value decl, java.lang.Class<?> javaClass, java.lang.Class<?> getterReturnType, Object instance, Type valueType) {
if (!decl.isVariable() && !decl.isLate())
return;
if (ModelUtil.isEnumeratedConstructor(decl)) {
return;
}
if (decl instanceof JavaBeanValue) {
String setterName = ((JavaBeanValue) decl).getSetterName();
try {
Method m = javaClass.getMethod(setterName, getterReturnType);
m.setAccessible(true);
setter = MethodHandles.lookup().unreflect(m);
if (instance != null && !Modifier.isStatic(m.getModifiers()))
setter = setter.bindTo(instance);
setter = setter.asType(MethodType.methodType(void.class, getterReturnType));
setter = MethodHandleUtil.unboxArguments(setter, 0, 0, new java.lang.Class[] { getterReturnType }, Arrays.asList(valueType));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException e) {
throw Metamodel.newModelError("Failed to find setter method " + setterName + " for: " + decl, e);
}
} else if (decl instanceof LazyValue) {
// FIXME: we should really save the getter name in the LazyDecl
String setterName = NamingBase.getSetterName(decl);
try {
Method m = javaClass.getMethod(setterName, getterReturnType);
m.setAccessible(true);
setter = MethodHandles.lookup().unreflect(m);
setter = setter.asType(MethodType.methodType(void.class, getterReturnType));
setter = MethodHandleUtil.unboxArguments(setter, 0, 0, new java.lang.Class[] { getterReturnType }, Arrays.asList(valueType));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException e) {
throw Metamodel.newModelError("Failed to find setter method " + setterName + " for: " + decl, e);
}
} else if (decl instanceof FieldValue) {
String fieldName = ((FieldValue) decl).getRealName();
try {
Field f = javaClass.getField(fieldName);
f.setAccessible(true);
setter = MethodHandles.lookup().unreflectSetter(f);
if (instance != null && !Modifier.isStatic(f.getModifiers()))
setter = setter.bindTo(instance);
setter = setter.asType(MethodType.methodType(void.class, getterReturnType));
setter = MethodHandleUtil.unboxArguments(setter, 0, 0, new java.lang.Class[] { getterReturnType }, Arrays.asList(valueType));
} catch (NoSuchFieldException | SecurityException | IllegalAccessException e) {
throw Metamodel.newModelError("Failed to find field " + fieldName + " for: " + decl, e);
}
} else
throw Metamodel.newModelError("Unsupported attribute type: " + decl);
}
use of org.eclipse.ceylon.model.loader.model.FieldValue in project ceylon by eclipse.
the class NamingBase method getSetterName.
public static String getSetterName(Declaration decl) {
// use the refined decl except when the current declaration is variable and the refined isn't
Declaration refinedDecl = decl.getRefinedDeclaration();
if (isValue(decl) && isValue(refinedDecl)) {
Value v = (Value) decl;
Value rv = (Value) refinedDecl;
if (!v.isVariable() || rv.isVariable()) {
decl = refinedDecl;
}
} else {
decl = refinedDecl;
}
if (decl instanceof FieldValue) {
return ((FieldValue) decl).getRealName();
}
if (decl instanceof JavaBeanValue && // one it will not. This is also used for late setters...
((JavaBeanValue) decl).getSetterName() != null) {
return ((JavaBeanValue) decl).getSetterName();
} else if (decl.isClassOrInterfaceMember() && !isLocalToInitializer(decl) || decl.isStatic()) {
String setterName = getSetterName(decl.getName());
if (decl.isMember() && !decl.isShared()) {
setterName = suffixName(Suffix.$priv$, setterName);
}
return setterName;
} else if (decl instanceof TypedDeclaration && isBoxedVariable((TypedDeclaration) decl)) {
return name(Unfix.ref);
} else {
return name(Unfix.set_);
}
}
use of org.eclipse.ceylon.model.loader.model.FieldValue in project ceylon by eclipse.
the class AbstractModelLoader method completeActual.
@Override
public void completeActual(Declaration decl) {
Scope container = decl.getContainer();
if (container instanceof ClassOrInterface) {
ClassOrInterface klass = (ClassOrInterface) container;
decl.setRefinedDeclaration(decl);
// we never consider Interface and other stuff, since we never register the actualCompleter for them
if (decl instanceof Class) {
// Java member classes are never actual
if (!ModelUtil.isCeylonDeclaration((Class) decl))
return;
// we already set the actual bit for member classes, we just need the refined decl
if (decl.isActual()) {
Declaration refined = klass.getRefinedMember(decl.getName(), getSignature(decl), klass.isVariadic());
decl.setRefinedDeclaration(refined);
}
} else {
// Function or Value
MethodMirror methodMirror;
if (decl instanceof JavaBeanValue)
methodMirror = ((JavaBeanValue) decl).mirror;
else if (decl instanceof JavaMethod)
methodMirror = ((JavaMethod) decl).mirror;
else
throw new ModelResolutionException("Unknown type of declaration: " + decl + ": " + decl.getClass().getName());
decl.setRefinedDeclaration(decl);
// For Ceylon interfaces we rely on annotation
if (klass instanceof LazyInterface && ((LazyInterface) klass).isCeylon()) {
boolean actual = methodMirror.getAnnotation(CEYLON_LANGUAGE_ACTUAL_ANNOTATION) != null;
decl.setActual(actual);
if (actual) {
Declaration refined = klass.getRefinedMember(decl.getName(), getSignature(decl), decl.isVariadic());
decl.setRefinedDeclaration(refined);
}
} else {
if (isOverridingMethod(methodMirror)) {
Declaration refined = klass.getRefinedMember(decl.getName(), getSignature(decl), decl.isVariadic());
if (refined instanceof FieldValue == false) {
decl.setActual(true);
decl.setRefinedDeclaration(refined);
}
}
}
// for Ceylon methods
if (decl instanceof JavaMethod && ModelUtil.isCeylonDeclaration(klass)) {
if (!methodMirror.getTypeParameters().isEmpty() && // lazy loading just to check.
JvmBackendUtil.supportsReified(decl)) {
checkReifiedTypeDescriptors(methodMirror.getTypeParameters().size(), container.getQualifiedNameString(), methodMirror, false);
}
}
}
}
completeVariable(decl);
}
use of org.eclipse.ceylon.model.loader.model.FieldValue in project ceylon by eclipse.
the class NamingBase method getGetterName.
public static String getGetterName(Declaration decl, boolean indirect) {
// always use the refined decl
decl = decl.getRefinedDeclaration();
if (decl instanceof FieldValue) {
return ((FieldValue) decl).getRealName();
}
if (decl instanceof JavaBeanValue && !indirect) {
return ((JavaBeanValue) decl).getGetterName();
}
boolean enumeratedConstructor = false;
if (decl instanceof Value) {
Type type = ((Value) decl).getType();
enumeratedConstructor = type != null && type.getDeclaration() instanceof org.eclipse.ceylon.model.typechecker.model.Constructor;
}
if (decl.isClassOrInterfaceMember() && (!isLocalToInitializer(decl) || enumeratedConstructor || decl.isStatic()) && !indirect) {
if (enumeratedConstructor) {
Class constructedClass = getConstructedClass(decl);
// See CeylonVisitor.transformSingletonConstructor for that logic
if (constructedClass.isToplevel() || constructedClass.isClassMember())
return getGetterName(((Class) decl.getContainer()).getName() + "$" + decl.getName());
return name(Unfix.ref);
}
return getErasedGetterName(decl);
} else if (decl instanceof TypedDeclaration && isBoxedVariable((TypedDeclaration) decl)) {
return name(Unfix.ref);
} else {
return name(Unfix.get_);
}
}
use of org.eclipse.ceylon.model.loader.model.FieldValue in project ceylon by eclipse.
the class ExpressionTransformer method transformMemberExpression.
private JCExpression transformMemberExpression(Tree.StaticMemberOrTypeExpression expr, JCExpression primaryExpr, TermTransformer transformer) {
JCExpression result = null;
// do not throw, an error will already have been reported
Declaration decl = expr.getDeclaration();
if (decl == null) {
return makeErroneous(expr, "compiler bug: expression with no declaration");
}
// creating a tmp variable (in which case we have a substitution for it)
while (decl instanceof TypedDeclaration) {
TypedDeclaration typedDecl = (TypedDeclaration) decl;
if (!naming.isSubstituted(decl) && typedDecl.getOriginalDeclaration() != null) {
decl = ((TypedDeclaration) decl).getOriginalDeclaration();
} else {
break;
}
}
// (the header might look like a field while the implementation is a getter)
if (decl.isNativeHeader()) {
Declaration d = ModelUtil.getNativeDeclaration(decl, Backend.Java);
if (d != null) {
decl = d;
}
}
// Explanation: primaryExpr and qualExpr both specify what is to come before the selector
// but the important difference is that primaryExpr is used for those situations where
// the result comes from the actual Ceylon code while qualExpr is used for those situations
// where we need to refer to synthetic objects (like wrapper classes for toplevel methods)
JCExpression qualExpr = null;
String selector = null;
// true for Java interop using fields, and for super constructor parameters, which must use
// parameters rather than getter methods
boolean mustUseField = false;
// true for default parameter methods
boolean mustUseParameter = false;
if (decl instanceof Functional && (!(decl instanceof Class) || ((Class) decl).getParameterList() != null) && (!(decl instanceof Function) || !decl.isParameter() || functionalParameterRequiresCallable((Function) decl, expr)) && isFunctionalResult(expr.getTypeModel())) {
result = transformFunctional(expr, (Functional) decl, expectedType);
} else if (coerced && decl instanceof Value && isFunctionalResult(expr.getTypeModel()) && checkForFunctionalInterface(expectedType) != null) {
result = transformFunctionalInterfaceBridge(expr, (Value) decl, expectedType);
} else if (coerced && decl instanceof Functional && decl.isParameter() && isFunctionalResult(expr.getTypeModel()) && checkForFunctionalInterface(expectedType) != null) {
result = transformFunctional(expr, (Functional) decl, expectedType);
// } else if (coerced
// && decl instanceof Value
// && isJavaFunctionalInterfaceResult(expr.getTypeModel())
// && expectedType != null
// && isCeylonCallable(expectedType)) {
// result = transformCallableBridge(expr, (Value)decl, expectedType);
} else if (Decl.isGetter(decl)) {
// invoke the getter
if (decl.isToplevel()) {
primaryExpr = null;
qualExpr = naming.makeName((Value) decl, Naming.NA_FQ | Naming.NA_WRAPPER | Naming.NA_MEMBER);
selector = null;
} else if (decl.isClassOrInterfaceMember() && !ModelUtil.isLocalToInitializer(decl)) {
selector = naming.selector((Value) decl);
} else {
// method local attr
if (!isRecursiveReference(expr)) {
primaryExpr = naming.makeQualifiedName(primaryExpr, (Value) decl, Naming.NA_Q_LOCAL_INSTANCE);
}
selector = naming.selector((Value) decl);
}
} else if (Decl.isValueOrSharedOrCapturedParam(decl)) {
if (decl.isToplevel()) {
// ERASURE
if (isNullValue(decl)) {
result = makeNull();
} else if (isBooleanTrue(decl)) {
result = makeBoolean(true);
} else if (isBooleanFalse(decl)) {
result = makeBoolean(false);
} else {
// it's a toplevel attribute
primaryExpr = naming.makeName((TypedDeclaration) decl, Naming.NA_FQ | Naming.NA_WRAPPER);
selector = naming.selector((TypedDeclaration) decl);
}
} else if (Decl.isClassAttribute(decl) || Decl.isClassParameter(decl)) {
mustUseField = Decl.isJavaField(decl) || (isWithinSuperInvocation() && primaryExpr == null && withinSuperInvocation == decl.getContainer());
mustUseParameter = (primaryExpr == null && isWithinDefaultParameterExpression(decl.getContainer()));
if (mustUseField || mustUseParameter) {
if (decl instanceof FieldValue) {
selector = ((FieldValue) decl).getRealName();
} else if (decl.isParameter()) {
selector = Naming.getAliasedParameterName(((Value) decl).getInitializerParameter());
} else {
selector = decl.getName();
}
} else {
// invoke the getter, using the Java interop form of Util.getGetterName because this is the only case
// (Value inside a Class) where we might refer to JavaBean properties
selector = naming.selector((TypedDeclaration) decl);
}
} else if (ModelUtil.isCaptured(decl)) {
TypedDeclaration typedDecl = ((TypedDeclaration) decl);
TypeDeclaration typeDecl = typedDecl.getType().getDeclaration();
mustUseField = Decl.isBoxedVariable((TypedDeclaration) decl);
if (ModelUtil.isLocalNotInitializer(typeDecl) && typeDecl.isAnonymous() && // we need the box if it's a captured object
!typedDecl.isSelfCaptured()) {
// accessing a local 'object' declaration, so don't need a getter
} else if (decl.isCaptured() && !((TypedDeclaration) decl).isVariable() && // captured objects are never variable but need the box
!typedDecl.isSelfCaptured()) {
// accessing a local that is not getter wrapped
} else {
primaryExpr = naming.makeQualifiedName(primaryExpr, (TypedDeclaration) decl, Naming.NA_Q_LOCAL_INSTANCE);
selector = naming.selector((TypedDeclaration) decl);
}
}
} else if (Decl.isMethodOrSharedOrCapturedParam(decl)) {
mustUseParameter = (primaryExpr == null && decl.isParameter() && isWithinDefaultParameterExpression(decl.getContainer()));
if (!decl.isParameter() && (ModelUtil.isLocalNotInitializer(decl) || (ModelUtil.isLocalToInitializer(decl) && ((Function) decl).isDeferred()))) {
primaryExpr = null;
int flags = Naming.NA_MEMBER;
if (!isRecursiveReference(expr)) {
// Only want to quote the method name
// e.g. enum.$enum()
flags |= Naming.NA_WRAPPER_UNQUOTED;
} else if (!isReferenceInSameScope(expr)) {
// always qualify it with this
flags |= Naming.NA_WRAPPER | Naming.NA_WRAPPER_WITH_THIS;
}
qualExpr = naming.makeName((Function) decl, flags);
selector = null;
} else if (decl.isToplevel()) {
primaryExpr = null;
qualExpr = naming.makeName((Function) decl, Naming.NA_FQ | Naming.NA_WRAPPER | Naming.NA_MEMBER);
selector = null;
} else if (!isWithinInvocation()) {
selector = null;
} else if (decl.isClassOrInterfaceMember()) {
selector = naming.selector((Function) decl);
} else {
selector = null;
}
}
boolean isCtor = decl instanceof Function && ((Function) decl).getTypeDeclaration() instanceof Constructor;
if (result == null) {
boolean useGetter = !(decl instanceof Function || isCtor) && !mustUseField && !mustUseParameter;
if (qualExpr == null && selector == null && !(isCtor)) {
useGetter = Decl.isClassAttribute(decl) && CodegenUtil.isErasedAttribute(decl.getName());
if (useGetter) {
selector = naming.selector((TypedDeclaration) decl);
} else {
selector = naming.substitute(decl);
}
}
if (qualExpr == null) {
qualExpr = primaryExpr;
}
// cases
if (!mustUseParameter) {
qualExpr = addQualifierForObjectMembersOfInterface(expr, decl, qualExpr);
qualExpr = addInterfaceImplAccessorIfRequired(qualExpr, expr, decl);
qualExpr = addThisOrObjectQualifierIfRequired(qualExpr, expr, decl);
if (qualExpr == null && expr instanceof Tree.BaseMemberExpression && needDollarThis(expr)) {
qualExpr = makeQualifiedDollarThis((Tree.BaseMemberExpression) expr);
}
}
boolean isEnumeratedConstructorGetter = false;
if ((decl instanceof Value && ModelUtil.isEnumeratedConstructor((Value) decl))) {
Class constructedClass = ModelUtil.getConstructedClass(decl);
// See CeylonVisitor.transformSingletonConstructor for that logic
if (constructedClass.isToplevel() || constructedClass.isClassMember())
isEnumeratedConstructorGetter = true;
else {
// Local enumerated constructor values are boxed
// local class will use a field
useGetter = false;
qualExpr = naming.makeQualifiedName(primaryExpr, (TypedDeclaration) decl, Naming.NA_Q_LOCAL_INSTANCE);
selector = naming.selector((TypedDeclaration) decl);
}
}
if (qualExpr == null && (decl.isStatic() || isEnumeratedConstructorGetter) && // and not classes
decl.getContainer() instanceof TypeDeclaration) {
qualExpr = naming.makeTypeDeclarationExpression(null, (TypeDeclaration) decl.getContainer(), DeclNameFlag.QUALIFIED);
}
if (Decl.isPrivateAccessRequiringUpcast(expr)) {
qualExpr = makePrivateAccessUpcast(expr, qualExpr);
}
if (transformer != null) {
if (decl instanceof TypedDeclaration && ((TypedDeclaration) decl).getType().isTypeConstructor()) {
// This is a bit of a hack, but we're "invoking a type constructor"
// so recurse to get the applied expression.
qualExpr = transformMemberExpression(expr, qualExpr, null);
selector = null;
}
result = transformer.transform(qualExpr, selector);
} else {
Tree.Primary qmePrimary = null;
if (expr instanceof Tree.QualifiedMemberOrTypeExpression) {
qmePrimary = ((Tree.QualifiedMemberOrTypeExpression) expr).getPrimary();
}
boolean safeMemberJavaArray = expr instanceof Tree.QualifiedMemberExpression && ((Tree.QualifiedMemberExpression) expr).getMemberOperator() instanceof Tree.SafeMemberOp && isJavaArray(qmePrimary.getTypeModel());
if ((safeMemberJavaArray || Decl.isValueTypeDecl(qmePrimary)) && // Safe operators always work on boxed things, so don't use value types
(safeMemberJavaArray || (expr instanceof Tree.QualifiedMemberOrTypeExpression == false) || ((Tree.QualifiedMemberOrTypeExpression) expr).getMemberOperator() instanceof Tree.MemberOp) && // We never want to use value types on boxed things, unless they are java arrays
(CodegenUtil.isUnBoxed(qmePrimary) || isJavaArray(qmePrimary.getTypeModel())) && // Java arrays length property does not go via value types
(!isJavaArray(qmePrimary.getTypeModel()) || (!"length".equals(selector) && !"hashCode".equals(selector)))) {
JCExpression primTypeExpr = makeJavaType(qmePrimary.getTypeModel(), JT_NO_PRIMITIVES | JT_VALUE_TYPE);
result = makeQualIdent(primTypeExpr, selector);
result = make().Apply(List.<JCTree.JCExpression>nil(), result, List.<JCTree.JCExpression>of(qualExpr));
} else if (expr instanceof Tree.QualifiedMemberOrTypeExpression && isThrowableMessage((Tree.QualifiedMemberOrTypeExpression) expr)) {
result = utilInvocation().throwableMessage(qualExpr);
} else if (expr instanceof Tree.QualifiedMemberOrTypeExpression && isThrowableSuppressed((Tree.QualifiedMemberOrTypeExpression) expr)) {
result = utilInvocation().suppressedExceptions(qualExpr);
} else {
result = makeQualIdent(qualExpr, selector);
if (useGetter) {
result = make().Apply(List.<JCTree.JCExpression>nil(), result, List.<JCTree.JCExpression>nil());
}
}
}
}
if (transformer == null && decl instanceof TypedDeclaration && ((TypedDeclaration) decl).getType().isTypeConstructor() && !expr.getTypeArguments().getTypeModels().isEmpty()) {
// applying a type constructor
ListBuffer<JCExpression> tds = new ListBuffer<JCExpression>();
for (Type t : expr.getTypeArguments().getTypeModels()) {
tds.add(makeReifiedTypeArgument(t));
}
result = make().Apply(null, makeQualIdent(result, Naming.Unfix.apply.toString()), List.<JCExpression>of(make().NewArray(make().Type(syms().ceylonTypeDescriptorType), List.<JCExpression>nil(), tds.toList())));
}
return result;
}
Aggregations