private AttributeDefinitionBuilder( AbstractTransformer owner, TypedDeclaration attrType, String javaClassName, String attrName, String fieldName, boolean toplevel) { int typeFlags = 0; ProducedTypedReference typedRef = owner.getTypedReference(attrType); ProducedTypedReference nonWideningTypedRef = owner.nonWideningTypeDecl(typedRef); ProducedType nonWideningType = owner.nonWideningType(typedRef, nonWideningTypedRef); if (!CodegenUtil.isUnBoxed(nonWideningTypedRef.getDeclaration())) { typeFlags |= AbstractTransformer.JT_NO_PRIMITIVES; } this.attrType = owner.makeJavaType(nonWideningType, typeFlags); this.attrTypeRaw = owner.makeJavaType(nonWideningType, AbstractTransformer.JT_RAW); this.owner = owner; this.javaClassName = javaClassName; this.attrName = attrName; this.fieldName = fieldName; this.toplevel = toplevel; // Make sure we use the declaration for building the getter/setter names, as we might be trying // to // override a JavaBean property with an "isFoo" getter, or non-Ceylon casing, and we have to // respect that. getterBuilder = MethodDefinitionBuilder.method2(owner, Naming.getGetterName(attrType)) .block(generateDefaultGetterBlock()) .isOverride(attrType.isActual()) .annotations(owner.makeAtAnnotations(attrType.getAnnotations())) .resultType(this.attrType, attrType); setterBuilder = MethodDefinitionBuilder.method2(owner, Naming.getSetterName(attrType)) .block(generateDefaultSetterBlock()) // only actual if the superclass is also variable .isOverride( attrType.isActual() && ((TypedDeclaration) attrType.getRefinedDeclaration()).isVariable()) .parameter( Flags.FINAL, attrName, attrType, nonWideningTypedRef.getDeclaration(), nonWideningType, 0); }
private Entry _sibling(Entry current, N name) throws StaxNavException { if (current != null) { Entry element = current; while (true) { Entry next = element.next(); if (next != null && next.getElement().getDepth() >= current.getElement().getDepth()) { if (next.getElement().getDepth() == current.getElement().getDepth()) { if (name == null) { current = next; return current; } else { N siblingName = naming.getName(next.getElement().getName()); if (name.equals(siblingName)) { current = next; return current; } else { element = next; } } } else { element = next; } } else { break; } } } return null; }
private Entry _current(Entry current, N name) throws StaxNavException { if (current != null) { if (name == null || name.equals(naming.getName(current.getElement().getName()))) { return current; } } return null; }
public boolean find(N name) throws StaxNavException { if (name == null) { throw new NullPointerException("No null name accepted"); } if (name.equals(naming.getName(current.getElement().getName()))) { return true; } else { return navigate(Axis.FOLLOWING, name); } }
public String getMethodDesc(String s) { if (s == null) return s; s = Naming.demangleFortressDescriptor(s); StringBuilder b = new StringBuilder(); maybeVarInMethodSig(s, 0, b); s = b.toString(); return s; }
public String getFieldDesc(String s, boolean flattening_tuples) { if (s == null) return s; s = Naming.demangleFortressDescriptor(s); StringBuilder b = new StringBuilder(); maybeVarInTypeDesc(s, 0, b, flattening_tuples); s = b.toString(); return s; }
private Entry _next(Entry current, N name) throws StaxNavException { if (current != null) { Entry next = current.next(depth); if (next != null && (name == null || name.equals(naming.getName(next.getElement().getName())))) { current = next; return current; } } return null; }
public String getMethodName(String s) { // TODO will need to rewrite into type, desc, and method variants. if (s == null) return s; s = Naming.demangleFortressIdentifier(s); StringBuilder b = new StringBuilder(); maybeBareMethodName(s, b); String s2 = b.toString(); return s2; }
public String getName(String s) { // TODO will need to rewrite into type, desc, and method variants. if (s == null) return s; s = Naming.demangleFortressIdentifier(s); StringBuilder b = new StringBuilder(); maybeBareVar(s, 0, b, false, false, true); s = b.toString(); return s; }
public String getTypeName(String s, boolean unwrap_expanded_tuples_in_arrows) { // TODO will need to rewrite into type, desc, and method variants. String t = s; if (s == null) return s; s = Naming.demangleFortressIdentifier(s); StringBuilder b = new StringBuilder(); maybeBareVar(s, 0, b, false, false, unwrap_expanded_tuples_in_arrows); s = b.toString(); return s; }
public N next(Set<N> names) throws StaxNavException { if (names == null) { throw new NullPointerException(); } Entry next = current.next(depth); if (next != null) { N name = naming.getName(next.getElement().getName()); if (names.contains(name)) { current = next; return name; } } return null; }
public static void main(String[] args) { try { RMISemaphore mutex = (RMISemaphore) Naming.lookup("rmi://" + args[0] + "/RMISemaphore"); for (int i = 1; i <= 10; i++) { mutex.p(); System.out.println("Kritischen Abschnitt betreten"); Thread.sleep((int) (Math.random() * 1000)); System.out.println("Kritischer Abschnitt wird verlassen"); mutex.v(); } } catch (Exception e) { } }
private JCTree generateField() { long flags = Flags.PRIVATE | (modifiers & Flags.STATIC); // only make it final if we have an init, otherwise we still have to initialise it if (!writable && (variableInit != null || valueConstructor)) { flags |= Flags.FINAL; } return owner .make() .VarDef( owner.make().Modifiers(flags), owner.names().fromString(Naming.quoteIfJavaKeyword(fieldName)), (toplevel) ? owner.make().TypeArray(attrType) : attrType, null); }
private JCExpression makeDefaultValueCall(Parameter defaultedParam, int i) { // add the default value List<JCExpression> defaultMethodArgs = List.nil(); // pass all the previous values for (int a = i - 1; a >= 0; a--) { Parameter param = paramLists.getParameters().get(a); JCExpression previousValue = gen.makeUnquotedIdent(getCallableTempVarName(param)); defaultMethodArgs = defaultMethodArgs.prepend(previousValue); } // now call the default value method return gen.make() .Apply( null, gen.makeUnquotedIdent(Naming.getDefaultedParamMethodName(null, defaultedParam)), defaultMethodArgs); }
private Entry _following(Entry current, N name) throws StaxNavException { if (name == null) { throw new UnsupportedOperationException("todo"); } if (current != null) { Entry entry = current.next(); while (entry != null) { N findName = naming.getName(entry.getElement().getName()); if (name.equals(findName)) { current = entry; return current; } else { entry = entry.next(); } } } return null; }
private int _descendant(N name) throws StaxNavException { Entry element = current; while (true) { Entry next = element.next(); if (next != null && next.getElement().getDepth() >= current.getElement().getDepth()) { N descendantName = naming.getName(next.getElement().getName()); if (name.equals(descendantName)) { int diff = next.getElement().getDepth() - current.getElement().getDepth(); current = next; return diff; } else { element = next; } } else { break; } } return -1; }
private JCTree makeCallTypedMethod( List<JCStatement> body, java.util.List<ProducedType> parameterTypes) { // make the method MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.method(gen, false, Naming.getCallableTypedMethodName()); methodBuilder.modifiers(Flags.PRIVATE); ProducedType returnType = gen.getReturnTypeOfCallable(typeModel); methodBuilder.resultType(gen.makeJavaType(returnType, JT_NO_PRIMITIVES), null); // add all parameters int i = 0; for (Parameter param : paramLists.getParameters()) { ParameterDefinitionBuilder parameterBuilder = ParameterDefinitionBuilder.instance(gen, param.getName()); JCExpression paramType = gen.makeJavaType(parameterTypes.get(i)); parameterBuilder.type(paramType, null); methodBuilder.parameter(parameterBuilder); i++; } // Return the call result, or null if a void method methodBuilder.body(body); return methodBuilder.build(); }
public MethodDefinitionBuilder parameter(Parameter param, int flags) { String paramName = param.getName(); String aliasedName = paramName; MethodOrValue mov = CodegenUtil.findMethodOrValueForParam(param); int mods = 0; if (!(mov instanceof Value) || !mov.isVariable() || mov.isCaptured()) { mods |= FINAL; } if (mov instanceof Method || mov instanceof Value && mov.isVariable() && mov.isCaptured()) { aliasedName = Naming.getAliasedParameterName(param); } TypedDeclaration nonWideningDecl; ProducedType nonWideningType; if (mov instanceof Value) { ProducedTypedReference typedRef = gen.getTypedReference(mov); ProducedTypedReference nonWideningTypedRef = gen.nonWideningTypeDecl(typedRef); nonWideningType = gen.nonWideningType(typedRef, nonWideningTypedRef); nonWideningDecl = nonWideningTypedRef.getDeclaration(); } else { nonWideningType = param.getType(); nonWideningDecl = param; } return parameter(mods, paramName, aliasedName, param, nonWideningDecl, nonWideningType, flags); }
protected <N> N getName(Naming<N> naming) { return naming.getName(getName()); }
private JCTree makeDefaultedCall( int i, boolean isVariadic, java.util.List<ProducedType> parameterTypes) { // collect every parameter int a = 0; ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>(); for (Parameter param : paramLists.getParameters()) { // don't read default parameter values for forwarded calls if (forwardCallTo != null && i == a) break; // read the value JCExpression paramExpression = getTypedParameter(param, a, i > 3, parameterTypes); JCExpression varInitialExpression; if (param.isDefaulted() || param.isSequenced()) { if (i > 3) { // must check if it's defined JCExpression test = gen.make() .Binary(JCTree.GT, gen.makeSelect(getParamName(0), "length"), gen.makeInteger(a)); JCExpression elseBranch = makeDefaultValueCall(param, a); varInitialExpression = gen.make().Conditional(test, paramExpression, elseBranch); } else if (a >= i) { // get its default value because we don't have it varInitialExpression = makeDefaultValueCall(param, a); } else { // we must have it varInitialExpression = paramExpression; } } else { varInitialExpression = paramExpression; } // store it in a local var JCStatement var = gen.make() .VarDef( gen.make().Modifiers(Flags.FINAL), gen.naming.makeUnquotedName(getCallableTempVarName(param)), gen.makeJavaType( parameterTypes.get(a), CodegenUtil.isUnBoxed(param) ? 0 : gen.JT_NO_PRIMITIVES), varInitialExpression); stmts.append(var); a++; } if (forwardCallTo != null) { InvocationBuilder invocationBuilder = InvocationBuilder.forCallableInvocation(gen, forwardCallTo, paramLists, i); boolean prevCallableInv = gen.expressionGen().withinCallableInvocation(true); try { stmts.append(gen.make().Return(invocationBuilder.build())); } finally { gen.expressionGen().withinCallableInvocation(prevCallableInv); } } else if (isVariadic) { // chain to n param typed method List<JCExpression> args = List.nil(); // pass along the parameters for (a = paramLists.getParameters().size() - 1; a >= 0; a--) { Parameter param = paramLists.getParameters().get(a); args = args.prepend(gen.makeUnquotedIdent(getCallableTempVarName(param))); } JCMethodInvocation chain = gen.make().Apply(null, gen.makeUnquotedIdent(Naming.getCallableTypedMethodName()), args); stmts.append(gen.make().Return(chain)); } else { // insert the method body directly stmts.appendList(this.body); } List<JCStatement> body = stmts.toList(); return makeCallMethod(body, i); }
private String getCallableTempVarName(Parameter param) { // prefix them with $$ if we only forward, otherwise we need them to have the proper names return forwardCallTo != null ? Naming.getCallableTempVarName(param) : param.getName(); }
public static MethodDefinitionBuilder method( AbstractTransformer gen, boolean isMember, String name) { return new MethodDefinitionBuilder( gen, false, isMember ? Naming.getErasedMethodName(name) : Naming.getMethodName(name)); }
public static void testNaming() throws Exception { Map<String, Object> map = Create.map(); map.put("_secret", "_secret"); map.put("_secret", "_secret"); map.put(".secret", ".secret"); map.put("$new", "$new"); map.put("new", "new"); map.put("secret", "secret"); map.put("a_b_c", "a_b_c"); map.put("a.b.c", "a.b.c"); map.put(".a_b", ".a_b"); map.put("$$$$a_b", "$$$$a_b"); map.put("$$$$a.b", "$$$$a.b"); map.put("a", "a"); map.put("a$", "a$"); map.put("a$$", "a$$"); map.put("a$.$", "a$.$"); map.put("a$_$", "a$_$"); map.put("a..", "a.."); map.put("noid", "noid"); map.put("nullid", "nullid"); Naming trt = Configurable.createConfigurable(Naming.class, map); // By name assertEquals("secret", trt.secret()); assertEquals("_secret", trt.__secret()); assertEquals(".secret", trt._secret()); assertEquals("new", trt.$new()); assertEquals("$new", trt.$$new()); assertEquals("a.b.c", trt.a_b_c()); assertEquals("a_b_c", trt.a__b__c()); assertEquals(".a_b", trt._a__b()); assertEquals("$$$$a.b", trt.$$$$$$$$a_b()); assertEquals("$$$$a_b", trt.$$$$$$$$a__b()); assertEquals("a", trt.a$()); assertEquals("a$", trt.a$$()); assertEquals("a$", trt.a$$$()); assertEquals("a$.$", trt.a$$_$$()); assertEquals("a$_$", trt.a$$__$$()); assertEquals("a..", trt.a_$_()); assertEquals("noid", trt.noid()); assertEquals("nullid", trt.nullid()); // By AD assertEquals("secret", trt.xsecret()); assertEquals("_secret", trt.x__secret()); assertEquals(".secret", trt.x_secret()); assertEquals("new", trt.x$new()); assertEquals("$new", trt.x$$new()); assertEquals("a.b.c", trt.xa_b_c()); assertEquals("a_b_c", trt.xa__b__c()); assertEquals(".a_b", trt.x_a__b()); assertEquals("$$$$a.b", trt.x$$$$$$$$a_b()); assertEquals("$$$$a_b", trt.x$$$$$$$$a__b()); assertEquals("a", trt.xa$()); assertEquals("a$", trt.xa$$()); assertEquals("a$", trt.xa$$$()); assertEquals("a$.$", trt.xa$$_$$()); Builder b = new Builder(); b.addClasspath(new File("bin")); b.setProperty("Export-Package", "test.metatype"); b.setProperty("-metatype", "*"); b.build(); assertEquals(0, b.getErrors().size()); assertEquals(0, b.getWarnings().size()); Resource r = b.getJar().getResource("OSGI-INF/metatype/test.metatype.MetatypeTest$Naming.xml"); IO.copy(r.openInputStream(), System.err); Document d = db.parse(r.openInputStream(), "UTF-8"); assertEquals( "http://www.osgi.org/xmlns/metatype/v1.1.0", d.getDocumentElement().getNamespaceURI()); }