Skip to content

Commit

Permalink
Add type arguments on variable declaration of type 'Class'
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Jul 24, 2019
1 parent 41262a9 commit 5ae9e8a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -13,14 +13,17 @@
public class TypeReferenceDotClassExpression implements Expression {
protected int lineNumber;
protected Type typeDotClass;
protected Type type;

public TypeReferenceDotClassExpression(Type typeDotClass) {
this.typeDotClass = typeDotClass;
this.type = ObjectType.TYPE_CLASS.createType(typeDotClass);
}

public TypeReferenceDotClassExpression(int lineNumber, Type typeDotClass) {
this.lineNumber = lineNumber;
this.typeDotClass = typeDotClass;
this.type = ObjectType.TYPE_CLASS.createType(typeDotClass);
}

@Override
Expand All @@ -34,7 +37,7 @@ public Type getTypeDotClass() {

@Override
public Type getType() {
return ObjectType.TYPE_CLASS;
return type;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand Down Expand Up @@ -156,7 +156,7 @@ public void leftReduce(Type otherType) {
} else if ((dimension == 0) && (otherType.getDimension() == 0) && otherType.isObject()) {
ObjectType otherObjectType = (ObjectType) otherType;

if (!toType.getInternalName().equals(otherObjectType.getInternalName()) && objectTypeMaker.isAssignable(otherObjectType, toType)) {
if (toType.getInternalName().equals(otherObjectType.getInternalName()) || objectTypeMaker.isAssignable(otherObjectType, toType)) {
if (toType.getTypeArguments() == null) {
toType = otherObjectType;
} else if (otherObjectType.getTypeArguments() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -26,14 +26,15 @@ public class JavaSyntaxToJavaFragmentProcessor implements Processor {
public void process(Message message) throws Exception {
Loader loader = message.getHeader("loader");
String mainInternalTypeName = message.getHeader("mainInternalTypeName");
int majorVersion = message.getHeader("majorVersion");
CompilationUnit compilationUnit = message.getBody();

SearchImportsVisitor importsVisitor = new SearchImportsVisitor(mainInternalTypeName);
importsVisitor.visit(compilationUnit);
ImportsFragment importsFragment = importsVisitor.getImportsFragment();
message.setHeader("maxLineNumber", importsVisitor.getMaxLineNumber());

CompilationUnitVisitor visitor = new CompilationUnitVisitor(loader, mainInternalTypeName, importsFragment);
CompilationUnitVisitor visitor = new CompilationUnitVisitor(loader, mainInternalTypeName, majorVersion, importsFragment);
visitor.visit(compilationUnit);
message.setBody(visitor.getFragments());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand Down Expand Up @@ -49,8 +49,8 @@ public class CompilationUnitVisitor extends StatementVisitor {
protected SingleLineStatementVisitor singleLineStatementVisitor = new SingleLineStatementVisitor();
protected String mainInternalName;

public CompilationUnitVisitor(Loader loader, String mainInternalTypeName, ImportsFragment importsFragment) {
super(loader, mainInternalTypeName, importsFragment);
public CompilationUnitVisitor(Loader loader, String mainInternalTypeName, int majorVersion, ImportsFragment importsFragment) {
super(loader, mainInternalTypeName, majorVersion, importsFragment);
this.mainInternalName = mainInternalTypeName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class ExpressionVisitor extends TypeVisitor {
protected String currentTypeName;
protected HexaExpressionVisitor hexaExpressionVisitor = new HexaExpressionVisitor();

public ExpressionVisitor(Loader loader, String mainInternalTypeName, ImportsFragment importsFragment) {
super(loader, mainInternalTypeName, importsFragment);
public ExpressionVisitor(Loader loader, String mainInternalTypeName, int majorVersion, ImportsFragment importsFragment) {
super(loader, mainInternalTypeName, majorVersion, importsFragment);
}

public DefaultList<Fragment> getFragments() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand Down Expand Up @@ -43,8 +43,8 @@ public class StatementVisitor extends ExpressionVisitor {
public static final KeywordToken TRY = new KeywordToken("try");
public static final KeywordToken WHILE = new KeywordToken("while");

public StatementVisitor(Loader loader, String mainInternalTypeName, ImportsFragment importsFragment) {
super(loader, mainInternalTypeName, importsFragment);
public StatementVisitor(Loader loader, String mainInternalTypeName, int majorVersion, ImportsFragment importsFragment) {
super(loader, mainInternalTypeName, majorVersion, importsFragment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008-2019 Emmanuel Dupuy.
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
* This project is distributed under the GPLv3 license.
* This is a Copyleft license that gives the user the right to use,
* copy and modify the code freely for non-commercial purposes.
Expand All @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.List;

import static org.jd.core.v1.model.javasyntax.type.ObjectType.TYPE_CLASS;
import static org.jd.core.v1.model.javasyntax.type.PrimitiveType.*;

public class TypeVisitor extends AbstractJavaSyntaxVisitor {
Expand Down Expand Up @@ -50,14 +51,16 @@ public class TypeVisitor extends AbstractJavaSyntaxVisitor {

protected Loader loader;
protected String internalPackageName;
protected int majorVersion;
protected ImportsFragment importsFragment;
protected Tokens tokens;
protected int maxLineNumber = 0;
protected String currentInternalTypeName;
protected HashMap<String, TextToken> textTokenCache = new HashMap<>();

public TypeVisitor(Loader loader, String mainInternalTypeName, ImportsFragment importsFragment) {
public TypeVisitor(Loader loader, String mainInternalTypeName, int majorVersion, ImportsFragment importsFragment) {
this.loader = loader;
this.majorVersion = majorVersion;
this.importsFragment = importsFragment;

int index = mainInternalTypeName.lastIndexOf('/');
Expand Down Expand Up @@ -104,7 +107,17 @@ public void visit(ObjectType type) {
tokens.add(newTypeReferenceToken(type, currentInternalTypeName));

// Build token for type arguments
visitTypeArgumentList(type.getTypeArguments());
BaseTypeArgument typeArguments = type.getTypeArguments();

if (majorVersion >= 49) { // (majorVersion >= Java 5)
if (typeArguments != null) {
visitTypeArgumentList(typeArguments);
} else if (type.equals(TYPE_CLASS)) {
tokens.add(TextToken.LEFTANGLEBRACKET);
tokens.add(TextToken.QUESTIONMARK);
tokens.add(TextToken.RIGHTANGLEBRACKET);
}
}

// Build token for dimension
visitDimension(type.getDimension());
Expand Down

0 comments on commit 5ae9e8a

Please sign in to comment.