Java Code Examples for org.codehaus.groovy.ast.ClassNode#getNameWithoutPackage()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#getNameWithoutPackage() . 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: LazyASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static void addHolderClassIdiomBody(BlockStatement body, FieldNode fieldNode, Expression initExpr) {
    final ClassNode declaringClass = fieldNode.getDeclaringClass();
    final ClassNode fieldType = fieldNode.getType();
    final int visibility = ACC_PRIVATE | ACC_STATIC;
    final String fullName = declaringClass.getName() + "$" + fieldType.getNameWithoutPackage() + "Holder_" + fieldNode.getName().substring(1);
    final InnerClassNode holderClass = new InnerClassNode(declaringClass, fullName, visibility, ClassHelper.OBJECT_TYPE);
    final String innerFieldName = "INSTANCE";

    // we have two options:
    // (1) embed initExpr within holder class but redirect field access/method calls to declaring class members
    // (2) keep initExpr within a declaring class method that is only called by the holder class
    // currently we have gone with (2) for simplicity with only a slight memory footprint increase in the declaring class
    final String initializeMethodName = (fullName + "_initExpr").replace('.', '_');
    addGeneratedMethod(declaringClass, initializeMethodName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType,
            Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(initExpr));
    holderClass.addField(innerFieldName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType,
            callX(declaringClass, initializeMethodName));

    final Expression innerField = propX(classX(holderClass), innerFieldName);
    declaringClass.getModule().addClass(holderClass);
    body.addStatement(returnS(innerField));
}
 
Example 2
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void printConstructor(PrintWriter out, ClassNode clazz, ConstructorNode constructorNode) {
    printAnnotations(out, constructorNode);
    // printModifiers(out, constructorNode.getModifiers());

    out.print("public "); // temporary hack
    String className = clazz.getNameWithoutPackage();
    if (clazz instanceof InnerClassNode)
        className = className.substring(className.lastIndexOf('$') + 1);
    out.println(className);

    printParams(out, constructorNode);

    ClassNode[] exceptions = constructorNode.getExceptions();
    printExceptions(out, exceptions);

    ConstructorCallExpression constrCall = getFirstIfSpecialConstructorCall(constructorNode.getCode());
    if (constrCall == null) {
        out.println(" {}");
    } else {
        out.println(" {");
        printSpecialConstructorArgs(out, constructorNode, constrCall);
        out.println("}");
    }
}
 
Example 3
Source File: RenameProvider.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
private TextEdit createTextEditToRenameClassNode(ClassNode classNode, String newName, String text, Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	String className = classNode.getNameWithoutPackage();
	int dollarIndex = className.indexOf('$');
	if (dollarIndex != 01) {
		// it's an inner class, so remove the outer name prefix
		className = className.substring(dollarIndex + 1);
	}

	Pattern classPattern = Pattern.compile("(class\\s+)" + className + "\\b");
	Matcher classMatcher = classPattern.matcher(text);
	if (!classMatcher.find()) {
		// couldn't find the name!
		return null;
	}
	String prefix = classMatcher.group(1);

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + classMatcher.end());
	start.setCharacter(start.getCharacter() + prefix.length() + classMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 4
Source File: ASTClass.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public ASTClass(ClassNode node, String fqn) {
    super(node, node.getPackageName(), node.getNameWithoutPackage());
    if (fqn != null) {
        this.fqn = fqn;
    } else {
        this.fqn = getName();
    }
}
 
Example 5
Source File: ElementUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static String getDeclaringClassNameWithoutPackage(ASTNode node) {
    ClassNode declaringClass = getDeclaringClass(node);
    if (declaringClass != null) {
        return declaringClass.getNameWithoutPackage();
    }
    return null;
}
 
Example 6
Source File: TraitComposer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static SyntaxException createException(ClassNode trait, ClassNode targetNode, MethodNode forwarder, MethodNode existingMethod) {
    String middle;
    ASTNode errorTarget;
    if (existingMethod.getLineNumber() == -1) {
        // came from a trait
        errorTarget = targetNode;
        List<AnnotationNode> allAnnos = existingMethod.getAnnotations(Traits.TRAITBRIDGE_CLASSNODE);
        AnnotationNode bridgeAnno = allAnnos == null ? null : allAnnos.get(0);
        String fromTrait = null;
        if (bridgeAnno != null) {
            Expression traitClass = bridgeAnno.getMember("traitClass");
            if (traitClass instanceof ClassExpression) {
                ClassExpression ce = (ClassExpression) traitClass;
                fromTrait = ce.getType().getNameWithoutPackage();
            }
        }
        middle = "in '" + targetNode.getNameWithoutPackage();
        if (fromTrait != null) {
            middle += "' from trait '" + fromTrait;
        }
    } else {
        errorTarget = existingMethod;
        middle = "declared in '" + targetNode.getNameWithoutPackage();
    }
    String message = "The static '" + forwarder.getName() + "' method " + middle +
            "' conflicts with the instance method having the same signature from trait '" + trait.getNameWithoutPackage() + "'";
    return new SyntaxException(message, errorTarget);
}
 
Example 7
Source File: ToStringASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static Expression calculateToStringStatements(ClassNode cNode, boolean includeSuper, boolean includeFields, boolean includeSuperFields, List<String> excludes, final List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean includeSuperProperties, boolean allProperties, BlockStatement body, boolean allNames) {
    // def _result = new StringBuilder()
    final Expression result = localVarX("_result");
    body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
    List<ToStringElement> elements = new ArrayList<ToStringElement>();

    // def $toStringFirst = true
    final VariableExpression first = localVarX("$toStringFirst");
    body.addStatement(declS(first, constX(Boolean.TRUE)));

    // <class_name>(
    String className = (includePackage) ? cNode.getName() : cNode.getNameWithoutPackage();
    body.addStatement(appendS(result, constX(className + "(")));

    Set<String> names = new HashSet<String>();
    List<PropertyNode> superList;
    if (includeSuperProperties || includeSuperFields) {
        superList = getAllProperties(names, cNode, cNode.getSuperClass(), includeSuperProperties, includeSuperFields, allProperties, false, true, true, true, allNames, false);
    } else {
        superList = new ArrayList<PropertyNode>();
    }
    List<PropertyNode> list = getAllProperties(names, cNode, cNode,true, includeFields, allProperties, false, false, true, false, allNames, false);
    list.addAll(superList);

    for (PropertyNode pNode : list) {
        String name = pNode.getName();
        if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue;
        FieldNode fNode = pNode.getField();
        if (!cNode.hasProperty(name) && fNode.getDeclaringClass() != null) {
            // it's really just a field
            elements.add(new ToStringElement(varX(fNode), name, canBeSelf(cNode, fNode.getType())));
        } else {
            Expression getter = getterThisX(cNode, pNode);
            elements.add(new ToStringElement(getter, name, canBeSelf(cNode, pNode.getType())));
        }
    }

    // append super if needed
    if (includeSuper) {
        // not through MOP to avoid infinite recursion
        elements.add(new ToStringElement(callSuperX("toString"), "super", false));
    }

    if (includes != null) {
        Comparator<ToStringElement> includeComparator = Comparator.comparingInt(tse -> includes.indexOf(tse.name));
        elements.sort(includeComparator);
    }

    for (ToStringElement el : elements) {
        appendValue(body, result, first, el.value, el.name, includeNames, ignoreNulls, el.canBeSelf);
    }

    // wrap up
    body.addStatement(appendS(result, constX(")")));
    MethodCallExpression toString = callX(result, "toString");
    toString.setImplicitThis(false);
    return toString;
}
 
Example 8
Source File: MemoizedASTTransformation.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static String buildTypeName(ClassNode type) {
    if (type.isArray()) {
        return String.format("%sArray", buildTypeName(type.getComponentType()));
    }
    return type.getNameWithoutPackage();
}