"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java" between
apache-groovy-src-3.0.8.zip and apache-groovy-src-3.0.9.zip

About: Groovy is an agile dynamic (scripting) language for the Java Platform. It has features similar to those of Python, Ruby, Perl, and Smalltalk. Source release.

JavaStubGenerator.java  (apache-groovy-src-3.0.8):JavaStubGenerator.java  (apache-groovy-src-3.0.9)
skipping to change at line 21 skipping to change at line 21
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.codehaus.groovy.tools.javac; package org.codehaus.groovy.tools.javac;
import org.apache.groovy.ast.tools.ExpressionUtils;
import org.apache.groovy.io.StringBuilderWriter; import org.apache.groovy.io.StringBuilderWriter;
import org.codehaus.groovy.ast.AnnotatedNode; import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode; import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassHelper; import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ConstructorNode; import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.FieldNode; import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.GenericsType; import org.codehaus.groovy.ast.GenericsType;
import org.codehaus.groovy.ast.ImportNode; import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.InnerClassNode; import org.codehaus.groovy.ast.InnerClassNode;
skipping to change at line 43 skipping to change at line 44
import org.codehaus.groovy.ast.Parameter; import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode; import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.decompiled.DecompiledClassNode; import org.codehaus.groovy.ast.decompiled.DecompiledClassNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression; import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ClassExpression; import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression; import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression; import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression; import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.Expression; import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.ListExpression; import org.codehaus.groovy.ast.expr.ListExpression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression; import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.VariableExpression; import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.BlockStatement; import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement; import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement; import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.classgen.FinalVariableAnalyzer; import org.codehaus.groovy.classgen.FinalVariableAnalyzer;
import org.codehaus.groovy.classgen.Verifier; import org.codehaus.groovy.classgen.Verifier;
import org.codehaus.groovy.classgen.VerifierCodeVisitor; import org.codehaus.groovy.classgen.VerifierCodeVisitor;
import org.codehaus.groovy.control.ResolveVisitor; import org.codehaus.groovy.control.ResolveVisitor;
import org.codehaus.groovy.runtime.InvokerHelper; import org.codehaus.groovy.runtime.InvokerHelper;
skipping to change at line 112 skipping to change at line 114
this(outputPath, false, false, Charset.defaultCharset().name()); this(outputPath, false, false, Charset.defaultCharset().name());
} }
private static void mkdirs(File parent, String relativeFile) { private static void mkdirs(File parent, String relativeFile) {
int index = relativeFile.lastIndexOf('/'); int index = relativeFile.lastIndexOf('/');
if (index == -1) return; if (index == -1) return;
File dir = new File(parent, relativeFile.substring(0, index)); File dir = new File(parent, relativeFile.substring(0, index));
dir.mkdirs(); dir.mkdirs();
} }
private static final int DEFAULT_BUFFER_SIZE = 8 * 1024; // 8K
public void generateClass(ClassNode classNode) throws FileNotFoundException { public void generateClass(ClassNode classNode) throws FileNotFoundException {
// Only attempt to render our self if our super-class is resolved, else wait for it // Only attempt to render our self if our super-class is resolved, else wait for it
if (requireSuperResolved && !classNode.getSuperClass().isResolved()) { if (requireSuperResolved && !classNode.getSuperClass().isResolved()) {
return; return;
} }
// owner should take care for us // owner should take care for us
if (classNode instanceof InnerClassNode) if (classNode instanceof InnerClassNode)
return; return;
skipping to change at line 155 skipping to change at line 156
Charset.forName(encoding) Charset.forName(encoding)
); );
try (PrintWriter out = new PrintWriter(writer)) { try (PrintWriter out = new PrintWriter(writer)) {
out.print(generateStubContent(classNode)); out.print(generateStubContent(classNode));
} }
javaStubCompilationUnitSet.add(new RawJavaFileObject(createJavaStubFile( fileName).toPath().toUri())); javaStubCompilationUnitSet.add(new RawJavaFileObject(createJavaStubFile( fileName).toPath().toUri()));
} }
private static final int DEFAULT_BUFFER_SIZE = 8 * 1024; // 8K
private String generateStubContent(ClassNode classNode) { private String generateStubContent(ClassNode classNode) {
Writer writer = new StringBuilderWriter(DEFAULT_BUFFER_SIZE); Writer writer = new StringBuilderWriter(DEFAULT_BUFFER_SIZE);
try (PrintWriter out = new PrintWriter(writer)) { try (PrintWriter out = new PrintWriter(writer)) {
boolean packageInfo = "package-info".equals(classNode.getNameWithout Package()); boolean packageInfo = "package-info".equals(classNode.getNameWithout Package());
String packageName = classNode.getPackageName(); String packageName = classNode.getPackageName();
if (packageName != null) { if (packageName != null) {
if (packageInfo) { if (packageInfo) {
printAnnotations(out, classNode.getPackage()); printAnnotations(out, classNode.getPackage());
} }
skipping to change at line 433 skipping to change at line 436
} }
} }
return false; return false;
} }
private static boolean sameParameterTypes(final MethodNode firstMethod, fina l MethodNode secondMethod) { private static boolean sameParameterTypes(final MethodNode firstMethod, fina l MethodNode secondMethod) {
return sameParameterTypes(firstMethod.getParameters(), secondMethod.getP arameters()); return sameParameterTypes(firstMethod.getParameters(), secondMethod.getP arameters());
} }
private static boolean sameParameterTypes(final Parameter[] firstParams, fin al Parameter[] secondParams) { private static boolean sameParameterTypes(final Parameter[] firstParams, fin al Parameter[] secondParams) {
boolean sameParams = firstParams.length == secondParams.length; return org.codehaus.groovy.ast.tools.ParameterUtils.parametersEqual(firs
if (sameParams) { tParams, secondParams);
for (int i = 0; i < firstParams.length; i++) {
if (!firstParams[i].getType().equals(secondParams[i].getType()))
{
sameParams = false;
break;
}
}
}
return sameParams;
} }
private void printConstructors(PrintWriter out, ClassNode classNode) { private void printConstructors(PrintWriter out, ClassNode classNode) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<ConstructorNode> constrs = (List<ConstructorNode>) constructors.clo ne(); List<ConstructorNode> constrs = (List<ConstructorNode>) constructors.clo ne();
if (constrs != null) { if (constrs != null) {
constrs.addAll(classNode.getDeclaredConstructors()); constrs.addAll(classNode.getDeclaredConstructors());
for (ConstructorNode constr : constrs) { for (ConstructorNode constr : constrs) {
printConstructor(out, classNode, constr); printConstructor(out, classNode, constr);
} }
skipping to change at line 624 skipping to change at line 618
for (ClassNode stub : stubExceptions) { for (ClassNode stub : stubExceptions) {
if (stub.isDerivedFrom(superExc)) continue outer; if (stub.isDerivedFrom(superExc)) continue outer;
} }
// not found // not found
return false; return false;
} }
return true; return true;
} }
private void printSpecialConstructorArgs(PrintWriter out, ConstructorNode no de, ConstructorCallExpression constrCall) { private void printSpecialConstructorArgs(final PrintWriter out, final Constr uctorNode ctor, final ConstructorCallExpression ctorCall) {
// Select a constructor from our class, or super-class which is legal to call, // Select a constructor from our class, or super-class which is legal to call,
// then write out an invoke w/nulls using casts to avoid ambiguous calls // then write out an invoke w/nulls using casts to avoid ambiguous calls
Parameter[] params = selectAccessibleConstructorFromSuper(ctor);
Parameter[] params = selectAccessibleConstructorFromSuper(node);
if (params != null) { if (params != null) {
out.print("super ("); out.print("super (");
for (int i = 0, n = params.length; i < n; i += 1) { for (int i = 0, n = params.length; i < n; i += 1) {
printDefaultValue(out, params[i].getType()); printDefaultValue(out, params[i].getType());
if (i + 1 < n) { if (i + 1 < n) {
out.print(", "); out.print(", ");
} }
} }
out.println(");"); out.println(");");
return; return;
} }
// Otherwise try the older method based on the constructor's call expres sion // Otherwise try the older method based on the constructor's call expres sion
Expression arguments = constrCall.getArguments(); Expression arguments = ctorCall.getArguments();
if (ctorCall.isSuperCall()) {
if (constrCall.isSuperCall()) {
out.print("super("); out.print("super(");
} else { } else {
out.print("this("); out.print("this(");
} }
// Else try to render some arguments
if (arguments instanceof ArgumentListExpression) { if (arguments instanceof ArgumentListExpression) {
ArgumentListExpression argumentListExpression = (ArgumentListExpress List<Expression> args = ((ArgumentListExpression) arguments).getExpr
ion) arguments; essions();
List<Expression> args = argumentListExpression.getExpressions(); int i = 0, n = args.size();
for (Expression arg : args) { for (Expression arg : args) {
if (arg instanceof ConstantExpression) { if (arg instanceof ConstantExpression) {
ConstantExpression expression = (ConstantExpression) arg; Object value = ((ConstantExpression) arg).getValue();
Object o = expression.getValue(); if (value instanceof String) {
if (o instanceof String) {
out.print("(String)null"); out.print("(String)null");
} else { } else {
out.print(expression.getText()); out.print(arg.getText());
} }
} else { } else {
ClassNode type = getConstructorArgumentType(arg, node); printDefaultValue(out, getConstructorArgumentType(arg, ctor)
printDefaultValue(out, type); );
} }
if (++i < n) {
if (arg != args.get(args.size() - 1)) {
out.print(", "); out.print(", ");
} }
} }
} }
out.println(");"); out.println(");");
} }
private static ClassNode getConstructorArgumentType(Expression arg, Construc private static ClassNode getConstructorArgumentType(final Expression arg, fi
torNode node) { nal ConstructorNode ctor) {
if (!(arg instanceof VariableExpression)) return arg.getType(); if (arg instanceof VariableExpression) {
VariableExpression vexp = (VariableExpression) arg; return ((VariableExpression) arg).getAccessedVariable().getType();
String name = vexp.getName(); }
for (Parameter param : node.getParameters()) { if (arg instanceof MethodCallExpression) { // GROOVY-10122
if (param.getName().equals(name)) { MethodCallExpression mce = (MethodCallExpression) arg;
return param.getType(); if (ExpressionUtils.isThisExpression(mce.getObjectExpression())) {
MethodNode mn = ctor.getDeclaringClass().tryFindPossibleMethod(m
ce.getMethodAsString(), mce.getArguments());
if (mn != null) return mn.getReturnType();
} }
return null;
} }
return vexp.getType(); return arg.getType();
} }
private void printMethod(PrintWriter out, ClassNode clazz, MethodNode method Node) { private void printMethod(PrintWriter out, ClassNode clazz, MethodNode method Node) {
if (methodNode.getName().equals("<clinit>")) return; if (methodNode.getName().equals("<clinit>")) return;
if (methodNode.isPrivate() || !Utilities.isJavaIdentifier(methodNode.get Name())) return; if (methodNode.isPrivate() || !Utilities.isJavaIdentifier(methodNode.get Name())) return;
if (methodNode.isSynthetic() && methodNode.getName().equals("$getStaticM etaClass")) return; if (methodNode.isSynthetic() && methodNode.getName().equals("$getStaticM etaClass")) return;
printAnnotations(out, methodNode); printAnnotations(out, methodNode);
if (!isInterfaceOrTrait(clazz)) { if (!isInterfaceOrTrait(clazz)) {
int modifiers = methodNode.getModifiers(); int modifiers = methodNode.getModifiers();
skipping to change at line 819 skipping to change at line 804
private void printReturn(PrintWriter out, ClassNode retType) { private void printReturn(PrintWriter out, ClassNode retType) {
if (!ClassHelper.VOID_TYPE.equals(retType)) { if (!ClassHelper.VOID_TYPE.equals(retType)) {
out.print("return "); out.print("return ");
printDefaultValue(out, retType); printDefaultValue(out, retType);
out.print(";"); out.print(";");
} }
} }
private void printDefaultValue(final PrintWriter out, final ClassNode type) { private void printDefaultValue(final PrintWriter out, final ClassNode type) {
if (!type.equals(ClassHelper.boolean_TYPE)) { if (type != null && !type.equals(ClassHelper.boolean_TYPE)) {
out.print("("); out.print("(");
printType(out, type); printType(out, type);
out.print(")"); out.print(")");
} }
if (ClassHelper.isPrimitiveType(type)) { if (type != null && ClassHelper.isPrimitiveType(type)) {
if (type.equals(ClassHelper.boolean_TYPE)) { if (type.equals(ClassHelper.boolean_TYPE)) {
out.print("false"); out.print("false");
} else { } else {
out.print("0"); out.print("0");
} }
} else { } else {
out.print("null"); out.print("null");
} }
} }
skipping to change at line 1059 skipping to change at line 1044
javaFileObjectStream.forEach(FileObject::delete); javaFileObjectStream.forEach(FileObject::delete);
javaStubCompilationUnitSet.clear(); javaStubCompilationUnitSet.clear();
} }
private File createJavaStubFile(String path) { private File createJavaStubFile(String path) {
return new File(outputPath, path + ".java"); return new File(outputPath, path + ".java");
} }
private static String escapeSpecialChars(String value) { private static String escapeSpecialChars(String value) {
return InvokerHelper.escapeBackslashes(value).replace("\"", "\\\""); return InvokerHelper.escapeBackslashes(value).replace("\"", "\\\"");
} }
private static boolean isInterfaceOrTrait(ClassNode cn) { private static boolean isInterfaceOrTrait(ClassNode cn) {
return cn.isInterface() || Traits.isTrait(cn); return cn.isInterface() || Traits.isTrait(cn);
} }
private final Set<JavaFileObject> javaStubCompilationUnitSet = new HashSet<> (); private final Set<JavaFileObject> javaStubCompilationUnitSet = new HashSet<> ();
public Set<JavaFileObject> getJavaStubCompilationUnitSet() { public Set<JavaFileObject> getJavaStubCompilationUnitSet() {
return javaStubCompilationUnitSet; return javaStubCompilationUnitSet;
 End of changes. 23 change blocks. 
48 lines changed or deleted 34 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)